Skip to content

Commit e3082f2

Browse files
feat(apollo_infra_utils): add function to format Cairo0 source files
1 parent ae11937 commit e3082f2

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,3 +1,5 @@
1+
#[cfg(any(test, feature = "testing"))]
2+
use std::io::Write;
13
use std::path::PathBuf;
24
use std::process::Command;
35
use std::sync::LazyLock;
@@ -138,3 +140,23 @@ pub fn verify_cairo0_compiler_deps() {
138140
*ENTER_VENV_INSTRUCTIONS
139141
);
140142
}
143+
144+
/// Runs the Cairo0 formatter on the input source code.
145+
#[cfg(any(test, feature = "testing"))]
146+
pub fn cairo0_format(unformatted: &String) -> String {
147+
verify_cairo0_compiler_deps();
148+
149+
// Dump string to temporary file.
150+
let mut temp_file = tempfile::NamedTempFile::new().unwrap();
151+
temp_file.write_all(unformatted.as_bytes()).unwrap();
152+
153+
// Run formatter.
154+
let mut command = Command::new("cairo-format");
155+
command.arg(temp_file.path().to_str().unwrap());
156+
let format_output = command.output().unwrap();
157+
let stderr_output = String::from_utf8(format_output.stderr).unwrap();
158+
assert!(format_output.status.success(), "{stderr_output}");
159+
160+
// Return formatted file.
161+
String::from_utf8_lossy(format_output.stdout.as_slice()).to_string()
162+
}

0 commit comments

Comments
 (0)