diff --git a/AGENTS.md b/AGENTS.md index 9deeba5..1f2a5a7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -124,3 +124,5 @@ Copy Nix fixtures into `TEST_TMPDIR` before passing their paths to CLI tests. - The vendored `rustversion-1.0.23/build/` files are currently ignored and untracked. Fresh worktrees cannot build Xatu tests until those upstream files are restored. +- `status::ResultExt` requires errors that implement `Into`, not arbitrary `std::error::Error` values. + Map other errors explicitly with a status code and `with_source`. diff --git a/src/xatu/xatu_main.rs b/src/xatu/xatu_main.rs index 2383f04..1d8c901 100644 --- a/src/xatu/xatu_main.rs +++ b/src/xatu/xatu_main.rs @@ -5,45 +5,46 @@ use std::process::ExitCode; use xatu::SourceFile; fn main() -> ExitCode { - let args = Args::parse(); - - let debug = args.debug; - let files = match collect_files(args) { - Ok(files) => files, + match run(Args::parse()) { + Ok(exit_code) => exit_code, Err(error) => { eprintln!("{error}"); - return ExitCode::from(2); - } - }; - if debug { - let report = xatu::debug(files); - match serde_json::to_string_pretty(&report) { - Ok(json) => println!("{json}"), - Err(error) => { - eprintln!("{error}"); - return ExitCode::from(2); - } - } - return if report.has_parse_errors() { ExitCode::from(2) - } else { - ExitCode::SUCCESS - }; + } } +} - match xatu::future_sight(files) { - Ok(report) if report.is_clean() => { - println!("No findings."); - ExitCode::SUCCESS - } - Ok(report) => { - print!("{report}"); - ExitCode::from(1) - } - Err(error) => { - eprintln!("{error}"); - ExitCode::from(2) - } +fn run(args: Args) -> status::Result { + let files = collect_files(args.paths)?; + if args.debug { + dump_parsed_files(files) + } else { + check(files) + } +} + +fn dump_parsed_files(files: Vec) -> status::Result { + let report = xatu::debug(files); + let json = serde_json::to_string_pretty(&report).map_err(|error| { + status::Status::internal("failed to serialize parser output").with_source(error) + })?; + println!("{json}"); + + Ok(if report.has_parse_errors() { + ExitCode::from(2) + } else { + ExitCode::SUCCESS + }) +} + +fn check(files: Vec) -> status::Result { + let report = xatu::future_sight(files)?; + if report.is_clean() { + println!("No findings."); + Ok(ExitCode::SUCCESS) + } else { + print!("{report}"); + Ok(ExitCode::from(1)) } } @@ -56,21 +57,12 @@ struct Args { paths: Vec, } -fn collect_files(args: Args) -> status::Result> { - let paths: Vec = args - .paths - .into_iter() - .map(monorepo::workspace::resolve_workspace_path) - .collect(); - - let files: Vec = paths - .iter() - .map(|path| collect_source_files(path)) - .collect::>>()? - .into_iter() - .flatten() - .collect(); - +fn collect_files(paths: Vec) -> status::Result> { + let mut files = Vec::new(); + for path in paths { + let path = monorepo::workspace::resolve_workspace_path(path); + files.extend(collect_source_files(&path)?); + } Ok(files) }