Files
leleka/crates/native-cpu-expert/examples/kernel_ab/command_dispatch.rs
T

50 lines
1.8 KiB
Rust

//! The command table of `kernel_ab`: which command line runs which command, and how a refusal of the directory ends
//! one.
#[allow(unused_imports)]
use super::*;
/// `time` and `alternate`: flags only (the command line of `kab time`); a failure is printed with the command's name.
pub(super) fn run_flag_command(cmd: &str, args: &[String]) -> ExitCode {
let result = if cmd == "time" { cmd_time(args) } else { cmd_alternate(args) };
result.unwrap_or_else(|e| {
eprintln!("kernel_ab {cmd}: {e}");
ExitCode::from(1)
})
}
/// `verify`, `compare` and `run-tree`, which take the directory first; None when the command line is not one of them.
pub(super) fn run_dir_command(cmd: &str, dir: &Path, opts: &Options) -> Option<ExitCode> {
let result = match cmd {
"verify" => cmd_verify(dir),
"compare" => cmd_compare(dir, opts),
"run-tree" => match (&opts.tree, &opts.out) {
(Some(t), Some(o)) => cmd_run_tree(dir, t, o, opts.pin, opts.case.as_deref()),
_ => return None,
},
_ => return None,
};
Some(result.unwrap_or_else(|e| {
eprintln!("kernel_ab: {e}");
ExitCode::from(1)
}))
}
/// The directory verified, or the exit code of its refusal (2: incomplete, 1: refused) after the refusal is printed.
pub(super) fn verify_or_exit(dir: &Path) -> Result<Verified, ExitCode> {
verify_dir(dir).map_err(|e| {
println!("{e}");
ExitCode::from(if e.incomplete { 2 } else { 1 })
})
}
fn cmd_verify(dir: &Path) -> Result<ExitCode, String> {
Ok(match verify_or_exit(dir) {
Ok(v) => {
println!("OK {} files (manifest sha256 {})", v.file_count, v.manifest_sha256);
ExitCode::SUCCESS
}
Err(code) => code,
})
}