main.rs (2867B)
1 mod check; 2 mod contracts; 3 mod coverage; 4 mod coverage_policy; 5 #[allow(dead_code)] 6 mod dto_render; 7 #[allow(dead_code)] 8 mod dto_roots; 9 mod fs; 10 mod generate; 11 mod manifest; 12 mod output; 13 mod package_matrix; 14 mod ts; 15 mod wasm; 16 17 enum CommandAction<'a> { 18 GenerateTs, 19 GenerateWasm(&'a [String]), 20 Coverage(&'a [String]), 21 Check, 22 } 23 24 fn main() { 25 if let Err(error) = run(std::env::args().skip(1)) { 26 eprintln!("{error}"); 27 std::process::exit(1); 28 } 29 } 30 31 fn run(args: impl IntoIterator<Item = String>) -> Result<(), String> { 32 let args = args.into_iter().collect::<Vec<_>>(); 33 match command_action(&args)? { 34 CommandAction::GenerateTs => generate::generate_ts(), 35 CommandAction::GenerateWasm(rest) => wasm::generate(rest), 36 CommandAction::Coverage(rest) => coverage::run(rest), 37 CommandAction::Check => check::check(), 38 } 39 } 40 41 fn command_action(args: &[String]) -> Result<CommandAction<'_>, String> { 42 match args { 43 [command, target] if command == "generate" && target == "ts" => { 44 Ok(CommandAction::GenerateTs) 45 } 46 [command, target, rest @ ..] if command == "generate" && target == "wasm" => { 47 Ok(CommandAction::GenerateWasm(rest)) 48 } 49 [command, rest @ ..] if command == "coverage" => Ok(CommandAction::Coverage(rest)), 50 [command] if command == "check" => Ok(CommandAction::Check), 51 [] => Err(usage()), 52 _ => Err(usage()), 53 } 54 } 55 56 fn usage() -> String { 57 "usage: cargo xtask generate ts | cargo xtask generate wasm [--package <key>] | cargo xtask check | cargo xtask coverage run" 58 .to_owned() 59 } 60 61 #[cfg(test)] 62 mod tests { 63 use super::{CommandAction, command_action}; 64 65 #[test] 66 fn accepts_generate_ts() { 67 let args = ["generate".to_owned(), "ts".to_owned()]; 68 assert!(matches!( 69 command_action(&args).expect("action"), 70 CommandAction::GenerateTs 71 )); 72 } 73 74 #[test] 75 fn accepts_generate_wasm() { 76 let args = ["generate".to_owned(), "wasm".to_owned()]; 77 assert!(matches!( 78 command_action(&args).expect("action"), 79 CommandAction::GenerateWasm(rest) if rest.is_empty() 80 )); 81 } 82 83 #[test] 84 fn accepts_check() { 85 let args = ["check".to_owned()]; 86 assert!(matches!( 87 command_action(&args).expect("action"), 88 CommandAction::Check 89 )); 90 } 91 92 #[test] 93 fn accepts_coverage_run() { 94 let args = ["coverage".to_owned(), "run".to_owned()]; 95 assert!(matches!( 96 command_action(&args).expect("action"), 97 CommandAction::Coverage(rest) if rest == ["run"] 98 )); 99 } 100 101 #[test] 102 fn rejects_unknown_command() { 103 let args = ["generate".to_owned(), "swift".to_owned()]; 104 assert!(command_action(&args).is_err()); 105 } 106 }