Skip to content

Commit b1f0aa5

Browse files
feat(apollo_infra_utils): add function to format Cairo0 source files
1 parent 7fa7428 commit b1f0aa5

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_infra_utils/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license-file.workspace = true
77
description = "Infrastructure utility."
88

99
[features]
10-
testing = ["cached", "colored", "dep:assert-json-diff", "socket2", "toml"]
10+
testing = ["cached", "colored", "dep:assert-json-diff", "socket2", "tempfile", "toml"]
1111

1212
[lints]
1313
workspace = true
@@ -20,6 +20,7 @@ num_enum.workspace = true
2020
serde = { workspace = true, features = ["derive"] }
2121
serde_json.workspace = true
2222
socket2 = { workspace = true, optional = true }
23+
tempfile = { workspace = true, optional = true }
2324
thiserror.workspace = true
2425
tokio = { workspace = true, features = ["process", "rt", "time"] }
2526
toml = { workspace = true, optional = true }
@@ -33,6 +34,7 @@ nix.workspace = true
3334
pretty_assertions.workspace = true
3435
rstest.workspace = true
3536
socket2.workspace = true
37+
tempfile.workspace = true
3638
tokio = { workspace = true, features = ["macros", "rt", "signal", "sync"] }
3739
toml.workspace = true
3840
tracing-subscriber = { workspace = true, features = ["env-filter"] }

crates/apollo_infra_utils/src/cairo0_compiler.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#[cfg(any(test, feature = "testing"))]
2+
use std::io::Write;
3+
#[cfg(any(test, feature = "testing"))]
24
use std::path::PathBuf;
35
use std::process::Command;
46
#[cfg(any(test, feature = "testing"))]
@@ -80,3 +82,23 @@ pub fn verify_cairo0_compiler_deps() {
8082
PIP_REQUIREMENTS_FILE.to_str().expect("Path to requirements.txt is valid unicode.")
8183
);
8284
}
85+
86+
/// Runs the Cairo0 formatter on the input source code.
87+
#[cfg(any(test, feature = "testing"))]
88+
pub fn cairo0_format(unformatted: &String) -> String {
89+
verify_cairo0_compiler_deps();
90+
91+
// Dump string to temporary file.
92+
let mut temp_file = tempfile::NamedTempFile::new().unwrap();
93+
temp_file.write_all(unformatted.as_bytes()).unwrap();
94+
95+
// Run formatter.
96+
let mut command = Command::new("cairo-format");
97+
command.arg(temp_file.path().to_str().unwrap());
98+
let format_output = command.output().unwrap();
99+
let stderr_output = String::from_utf8(format_output.stderr).unwrap();
100+
assert!(format_output.status.success(), "{stderr_output}");
101+
102+
// Return formatted file.
103+
String::from_utf8_lossy(format_output.stdout.as_slice()).to_string()
104+
}

0 commit comments

Comments
 (0)