@@ -37,26 +37,38 @@ pub enum Cairo0CompilerVersionError {
3737 "{0}. Are you in the venv? If not, run the following commands:\n {}" ,
3838 * ENTER_VENV_INSTRUCTIONS
3939 ) ]
40- NotFound ( String ) ,
40+ CompilerNotFound ( String ) ,
41+ }
42+
43+ #[ derive( thiserror:: Error , Debug ) ]
44+ pub enum Cairo0CompilerError {
45+ #[ error( transparent) ]
46+ Cairo0CompilerVersion ( #[ from] Cairo0CompilerVersionError ) ,
47+ #[ error( "Cairo root path not found at {0:?}." ) ]
48+ CairoRootNotFound ( PathBuf ) ,
49+ #[ error( "Failed to compile the program. Error: {0}." ) ]
50+ CompileError ( String ) ,
51+ #[ error( "Invalid path unicode: {0:?}." ) ]
52+ InvalidPath ( PathBuf ) ,
53+ #[ error( transparent) ]
54+ Io ( #[ from] std:: io:: Error ) ,
55+ #[ error( "No file found at path {0:?}." ) ]
56+ SourceFileNotFound ( PathBuf ) ,
4157}
4258
4359pub fn cairo0_compilers_correct_version ( ) -> Result < ( ) , Cairo0CompilerVersionError > {
4460 for compiler in [ CAIRO0_COMPILE , STARKNET_COMPILE_DEPRECATED ] {
4561 let version = match Command :: new ( compiler) . arg ( "--version" ) . output ( ) {
4662 Ok ( output) => String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ,
4763 Err ( error) => {
48- return Err ( Cairo0CompilerVersionError :: NotFound ( format ! (
64+ return Err ( Cairo0CompilerVersionError :: CompilerNotFound ( format ! (
4965 "Failed to get {compiler} version: {error}."
5066 ) ) ) ;
5167 }
5268 } ;
53- if version
54- . trim ( )
55- . replace ( "==" , " " )
56- . split ( " " )
57- . nth ( 1 )
58- . ok_or ( Cairo0CompilerVersionError :: NotFound ( "No compiler version found." . to_string ( ) ) ) ?
59- != EXPECTED_CAIRO0_VERSION
69+ if version. trim ( ) . replace ( "==" , " " ) . split ( " " ) . nth ( 1 ) . ok_or (
70+ Cairo0CompilerVersionError :: CompilerNotFound ( "No compiler version found." . to_string ( ) ) ,
71+ ) ? != EXPECTED_CAIRO0_VERSION
6072 {
6173 return Err ( Cairo0CompilerVersionError :: IncorrectVersion {
6274 compiler : compiler. to_string ( ) ,
@@ -69,6 +81,39 @@ pub fn cairo0_compilers_correct_version() -> Result<(), Cairo0CompilerVersionErr
6981 Ok ( ( ) )
7082}
7183
84+ /// Compile a Cairo0 program.
85+ pub fn compile_cairo0_program (
86+ path_to_main : PathBuf ,
87+ cairo_root_path : PathBuf ,
88+ ) -> Result < Vec < u8 > , Cairo0CompilerError > {
89+ cairo0_compilers_correct_version ( ) ?;
90+ if !path_to_main. exists ( ) {
91+ return Err ( Cairo0CompilerError :: SourceFileNotFound ( path_to_main) ) ;
92+ }
93+ if !cairo_root_path. exists ( ) {
94+ return Err ( Cairo0CompilerError :: CairoRootNotFound ( cairo_root_path) ) ;
95+ }
96+ let mut compile_command = Command :: new ( CAIRO0_COMPILE ) ;
97+ compile_command. args ( [
98+ path_to_main. to_str ( ) . ok_or ( Cairo0CompilerError :: InvalidPath ( path_to_main. clone ( ) ) ) ?,
99+ "--debug_info_with_source" ,
100+ "--cairo_path" ,
101+ cairo_root_path
102+ . to_str ( )
103+ . ok_or ( Cairo0CompilerError :: InvalidPath ( cairo_root_path. clone ( ) ) ) ?,
104+ ] ) ;
105+ let compile_output = compile_command. output ( ) ?;
106+
107+ // Verify output.
108+ if !compile_output. status . success ( ) {
109+ return Err ( Cairo0CompilerError :: CompileError (
110+ String :: from_utf8_lossy ( & compile_output. stderr ) . trim ( ) . to_string ( ) ,
111+ ) ) ;
112+ }
113+
114+ Ok ( compile_output. stdout )
115+ }
116+
72117/// Verifies that the required Cairo0 compiler is available; panics if unavailable.
73118/// For use in tests only. If cairo0 compiler verification is required in business logic, use
74119/// `crate::cairo0_compiler::cairo0_compilers_correct_version` instead.
@@ -78,7 +123,9 @@ pub fn verify_cairo0_compiler_deps() {
78123 Ok ( _) => {
79124 return ;
80125 }
81- Err ( Cairo0CompilerVersionError :: NotFound ( _) ) => "no installed cairo-lang found" . to_string ( ) ,
126+ Err ( Cairo0CompilerVersionError :: CompilerNotFound ( _) ) => {
127+ "no installed cairo-lang found" . to_string ( )
128+ }
82129 Err ( Cairo0CompilerVersionError :: IncorrectVersion { existing, .. } ) => {
83130 format ! ( "installed version: {existing}" )
84131 }
0 commit comments