use std::process::ExitCode;
use super::{Shape, print_report, run};
struct ShadowArgs {
old: Option<String>,
new: Option<String>,
old_shape: Shape,
new_shape: Shape,
samples: u64,
}
fn parse_shadow_flags(args: &[String]) -> Result<ShadowArgs, String> {
let mut a = ShadowArgs {
old: None,
new: None,
old_shape: Shape::Flat,
new_shape: Shape::Paged,
samples: 1,
};
let mut scan = crate::tools::argscan::Scan::new(args);
while let Some(word) = scan.next() {
match word {
"--old" => a.old = Some(scan.value("--old")?.to_string()),
"--new" => a.new = Some(scan.value("--new")?.to_string()),
"--old-pairs" => a.old_shape = Shape::Pairs,
"--new-flat" => a.new_shape = Shape::Flat,
"--samples" => a.samples = scan.number("--samples")?,
other => return Err(crate::tools::argscan::unexpected(other)),
}
}
Ok(a)
}
pub(crate) fn run_on(client: &mut dyn crate::link::Link, args: &[String]) -> ExitCode {
let parsed = parse_shadow_flags(args);
let Ok(ShadowArgs { old: Some(old), new: Some(new), old_shape, new_shape, samples }) = parsed
else {
if let Err(msg) = parsed {
eprintln!("kevy-cli shadow: {msg}");
}
eprintln!(
"usage: kevy-cli --kevy shadow --old \"<command>\" --new \"<command>\" \
[--old-pairs] [--new-flat] [--samples n]"
);
return ExitCode::FAILURE;
};
let split =
|s: &str| -> Vec<Vec<u8>> { s.split_whitespace().map(|t| t.as_bytes().to_vec()).collect() };
match run(client, &split(&old), &split(&new), old_shape, new_shape, samples) {
Ok(report) => {
print_report(&report);
if report.diverged > 0 { ExitCode::FAILURE } else { ExitCode::SUCCESS }
}
Err(e) => {
eprintln!("kevy-cli shadow: {e}");
ExitCode::FAILURE
}
}
}