11use clap:: { Args , Parser , Subcommand } ;
22use env_logger:: Env ;
33use pecos:: prelude:: * ;
4- use std:: error:: Error ;
54
65mod engine_setup;
76use engine_setup:: setup_cli_engine;
@@ -228,8 +227,10 @@ fn parse_general_noise_probabilities(noise_str_opt: Option<&String>) -> (f64, f6
228227/// This function sets up the appropriate engines and noise models based on
229228/// the command line arguments, then runs the specified program and outputs
230229/// the results.
231- fn run_program ( args : & RunArgs ) -> Result < ( ) , Box < dyn Error > > {
230+ fn run_program ( args : & RunArgs ) -> Result < ( ) , PecosError > {
231+ // get_program_path now includes proper context in its errors
232232 let program_path = get_program_path ( & args. program ) ?;
233+
233234 let classical_engine =
234235 setup_cli_engine ( & program_path, Some ( args. shots . div_ceil ( args. workers ) ) ) ?;
235236
@@ -295,12 +296,15 @@ fn run_program(args: &RunArgs) -> Result<(), Box<dyn Error>> {
295296 // Ensure parent directory exists
296297 if let Some ( parent) = std:: path:: Path :: new ( file_path) . parent ( ) {
297298 if !parent. exists ( ) {
298- std:: fs:: create_dir_all ( parent) ?;
299+ std:: fs:: create_dir_all ( parent) . map_err ( |e| {
300+ PecosError :: Resource ( format ! ( "Failed to create directory: {e}" ) )
301+ } ) ?;
299302 }
300303 }
301304
302305 // Write results to file
303- std:: fs:: write ( file_path, results_str) ?;
306+ std:: fs:: write ( file_path, results_str)
307+ . map_err ( |e| PecosError :: Resource ( format ! ( "Failed to write output file: {e}" ) ) ) ?;
304308 println ! ( "Results written to {file_path}" ) ;
305309 }
306310 None => {
@@ -312,18 +316,23 @@ fn run_program(args: &RunArgs) -> Result<(), Box<dyn Error>> {
312316 Ok ( ( ) )
313317}
314318
315- fn main ( ) -> Result < ( ) , Box < dyn Error > > {
319+ fn main ( ) -> Result < ( ) , PecosError > {
316320 // Initialize logger with default "info" level if not specified
317321 env_logger:: Builder :: from_env ( Env :: default ( ) . default_filter_or ( "info" ) ) . init ( ) ;
318322
319323 let cli = Cli :: parse ( ) ;
320324
321325 match & cli. command {
322326 Commands :: Compile ( args) => {
327+ // get_program_path and detect_program_type now include proper error context
323328 let program_path = get_program_path ( & args. program ) ?;
324- match detect_program_type ( & program_path) ? {
329+
330+ let program_type = detect_program_type ( & program_path) ?;
331+
332+ match program_type {
325333 ProgramType :: QIR => {
326334 let engine = setup_cli_engine ( & program_path, None ) ?;
335+ // The compile method should already return a properly formatted PecosError::Compilation
327336 engine. compile ( ) ?;
328337 }
329338 ProgramType :: PHIR => {
0 commit comments