Skip to content

Commit 2a5e8c8

Browse files
feat(apollo_infra_utils): add cairo-format to verified cairo0 scripts
1 parent 2ba8c01 commit 2a5e8c8

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

crates/apollo_infra_utils/src/cairo0_compiler.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod test;
1010

1111
pub const STARKNET_COMPILE_DEPRECATED: &str = "starknet-compile-deprecated";
1212
pub const CAIRO0_COMPILE: &str = "cairo-compile";
13+
pub const CAIRO0_FORMAT: &str = "cairo-format";
1314
pub const EXPECTED_CAIRO0_VERSION: &str = "0.14.0a1";
1415

1516
/// The local python requirements used to determine the cairo0 compiler version.
@@ -27,12 +28,12 @@ pip install -r {:#?}"#,
2728
});
2829

2930
#[derive(thiserror::Error, Debug)]
30-
pub enum Cairo0CompilerVersionError {
31+
pub enum Cairo0ScriptVersionError {
3132
#[error(
32-
"{compiler} version is not correct: required {required}, got {existing}. Are you in the \
33+
"{script} version is not correct: required {required}, got {existing}. Are you in the \
3334
venv? If not, run the following commands:\n{}", *ENTER_VENV_INSTRUCTIONS
3435
)]
35-
IncorrectVersion { compiler: String, existing: String, required: String },
36+
IncorrectVersion { script: String, existing: String, required: String },
3637
#[error(
3738
"{0}. Are you in the venv? If not, run the following commands:\n{}",
3839
*ENTER_VENV_INSTRUCTIONS
@@ -43,7 +44,7 @@ pub enum Cairo0CompilerVersionError {
4344
#[derive(thiserror::Error, Debug)]
4445
pub enum Cairo0CompilerError {
4546
#[error(transparent)]
46-
Cairo0CompilerVersion(#[from] Cairo0CompilerVersionError),
47+
Cairo0CompilerVersion(#[from] Cairo0ScriptVersionError),
4748
#[error("Cairo root path not found at {0:?}.")]
4849
CairoRootNotFound(PathBuf),
4950
#[error("Failed to compile the program. Error: {0}.")]
@@ -56,22 +57,22 @@ pub enum Cairo0CompilerError {
5657
SourceFileNotFound(PathBuf),
5758
}
5859

59-
pub fn cairo0_compilers_correct_version() -> Result<(), Cairo0CompilerVersionError> {
60-
for compiler in [CAIRO0_COMPILE, STARKNET_COMPILE_DEPRECATED] {
61-
let version = match Command::new(compiler).arg("--version").output() {
60+
pub fn cairo0_scripts_correct_version() -> Result<(), Cairo0ScriptVersionError> {
61+
for script in [CAIRO0_COMPILE, CAIRO0_FORMAT, STARKNET_COMPILE_DEPRECATED] {
62+
let version = match Command::new(script).arg("--version").output() {
6263
Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(),
6364
Err(error) => {
64-
return Err(Cairo0CompilerVersionError::CompilerNotFound(format!(
65-
"Failed to get {compiler} version: {error}."
65+
return Err(Cairo0ScriptVersionError::CompilerNotFound(format!(
66+
"Failed to get {script} version: {error}."
6667
)));
6768
}
6869
};
6970
if version.trim().replace("==", " ").split(" ").nth(1).ok_or(
70-
Cairo0CompilerVersionError::CompilerNotFound("No compiler version found.".to_string()),
71+
Cairo0ScriptVersionError::CompilerNotFound("No script version found.".to_string()),
7172
)? != EXPECTED_CAIRO0_VERSION
7273
{
73-
return Err(Cairo0CompilerVersionError::IncorrectVersion {
74-
compiler: compiler.to_string(),
74+
return Err(Cairo0ScriptVersionError::IncorrectVersion {
75+
script: script.to_string(),
7576
existing: version,
7677
required: EXPECTED_CAIRO0_VERSION.to_string(),
7778
});
@@ -86,7 +87,7 @@ pub fn compile_cairo0_program(
8687
path_to_main: PathBuf,
8788
cairo_root_path: PathBuf,
8889
) -> Result<Vec<u8>, Cairo0CompilerError> {
89-
cairo0_compilers_correct_version()?;
90+
cairo0_scripts_correct_version()?;
9091
if !path_to_main.exists() {
9192
return Err(Cairo0CompilerError::SourceFileNotFound(path_to_main));
9293
}
@@ -116,17 +117,17 @@ pub fn compile_cairo0_program(
116117

117118
/// Verifies that the required Cairo0 compiler is available; panics if unavailable.
118119
/// For use in tests only. If cairo0 compiler verification is required in business logic, use
119-
/// `crate::cairo0_compiler::cairo0_compilers_correct_version` instead.
120+
/// `crate::cairo0_compiler::cairo0_scripts_correct_version` instead.
120121
#[cfg(any(test, feature = "testing"))]
121122
pub fn verify_cairo0_compiler_deps() {
122-
let specific_error = match cairo0_compilers_correct_version() {
123+
let specific_error = match cairo0_scripts_correct_version() {
123124
Ok(_) => {
124125
return;
125126
}
126-
Err(Cairo0CompilerVersionError::CompilerNotFound(_)) => {
127+
Err(Cairo0ScriptVersionError::CompilerNotFound(_)) => {
127128
"no installed cairo-lang found".to_string()
128129
}
129-
Err(Cairo0CompilerVersionError::IncorrectVersion { existing, .. }) => {
130+
Err(Cairo0ScriptVersionError::IncorrectVersion { existing, .. }) => {
130131
format!("installed version: {existing}")
131132
}
132133
};

0 commit comments

Comments
 (0)