|
| 1 | +//! `spar insight verify-trace` subcommand — Track G v0.9.0. |
| 2 | +//! |
| 3 | +//! Wraps `spar_insight::analyze` over a parsed AADL model + a textual |
| 4 | +//! Zephyr CTF trace and prints the [`DiscrepancyReport`] as JSON |
| 5 | +//! (default) or text. |
| 6 | +
|
| 7 | +use std::fs; |
| 8 | +use std::process; |
| 9 | + |
| 10 | +use spar_hir_def::instance::SystemInstance; |
| 11 | +use spar_hir_def::{GlobalScope, HirDefDatabase, Name, file_item_tree}; |
| 12 | +use spar_insight::{analyze, parse_ctf}; |
| 13 | + |
| 14 | +use crate::parse_root_ref; |
| 15 | + |
| 16 | +pub fn cmd_insight(args: &[String]) { |
| 17 | + if args.is_empty() { |
| 18 | + eprintln!("Usage: spar insight <subcommand> [options] <file...>"); |
| 19 | + eprintln!(); |
| 20 | + eprintln!("Subcommands:"); |
| 21 | + eprintln!( |
| 22 | + " verify-trace --root Pkg::Type.Impl --trace trace.ctf [--format text|json] <file...>" |
| 23 | + ); |
| 24 | + process::exit(1); |
| 25 | + } |
| 26 | + match args[0].as_str() { |
| 27 | + "verify-trace" => cmd_verify_trace(&args[1..]), |
| 28 | + other => { |
| 29 | + eprintln!("Unknown insight subcommand: {other}"); |
| 30 | + process::exit(1); |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fn cmd_verify_trace(args: &[String]) { |
| 36 | + let mut root: Option<String> = None; |
| 37 | + let mut trace_path: Option<String> = None; |
| 38 | + let mut format: Option<String> = None; |
| 39 | + let mut files: Vec<String> = Vec::new(); |
| 40 | + |
| 41 | + let mut i = 0; |
| 42 | + while i < args.len() { |
| 43 | + match args[i].as_str() { |
| 44 | + "--root" => { |
| 45 | + i += 1; |
| 46 | + if i >= args.len() { |
| 47 | + eprintln!("--root requires a value (Package::Type.Impl)"); |
| 48 | + process::exit(1); |
| 49 | + } |
| 50 | + root = Some(args[i].clone()); |
| 51 | + } |
| 52 | + "--trace" => { |
| 53 | + i += 1; |
| 54 | + if i >= args.len() { |
| 55 | + eprintln!("--trace requires a path"); |
| 56 | + process::exit(1); |
| 57 | + } |
| 58 | + trace_path = Some(args[i].clone()); |
| 59 | + } |
| 60 | + "--format" => { |
| 61 | + i += 1; |
| 62 | + if i >= args.len() { |
| 63 | + eprintln!("--format requires a value (text|json)"); |
| 64 | + process::exit(1); |
| 65 | + } |
| 66 | + format = Some(args[i].clone()); |
| 67 | + } |
| 68 | + s if s.starts_with('-') => { |
| 69 | + eprintln!("Unknown option: {s}"); |
| 70 | + process::exit(1); |
| 71 | + } |
| 72 | + s => files.push(s.to_string()), |
| 73 | + } |
| 74 | + i += 1; |
| 75 | + } |
| 76 | + |
| 77 | + let root = root.unwrap_or_else(|| { |
| 78 | + eprintln!("--root Package::Type.Impl is required"); |
| 79 | + process::exit(1); |
| 80 | + }); |
| 81 | + let trace_path = trace_path.unwrap_or_else(|| { |
| 82 | + eprintln!("--trace <path> is required"); |
| 83 | + process::exit(1); |
| 84 | + }); |
| 85 | + if files.is_empty() { |
| 86 | + eprintln!("Missing AADL file argument(s)"); |
| 87 | + process::exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + let (pkg_name, type_name, impl_name) = parse_root_ref(&root); |
| 91 | + |
| 92 | + let db = HirDefDatabase::default(); |
| 93 | + let mut trees = Vec::new(); |
| 94 | + for file_path in &files { |
| 95 | + let source = fs::read_to_string(file_path).unwrap_or_else(|e| { |
| 96 | + eprintln!("Cannot read {file_path}: {e}"); |
| 97 | + process::exit(1); |
| 98 | + }); |
| 99 | + let parsed = spar_syntax::parse(&source); |
| 100 | + if !parsed.ok() { |
| 101 | + for err in parsed.errors() { |
| 102 | + let (line, col) = spar_base_db::offset_to_line_col(&source, err.offset); |
| 103 | + eprintln!("{file_path}:{line}:{col}: {}", err.msg); |
| 104 | + } |
| 105 | + eprintln!("Cannot verify-trace: parse errors in {file_path}"); |
| 106 | + process::exit(1); |
| 107 | + } |
| 108 | + let sf = spar_base_db::SourceFile::new(&db, file_path.clone(), source); |
| 109 | + trees.push(file_item_tree(&db, sf)); |
| 110 | + } |
| 111 | + let scope = GlobalScope::from_trees(trees); |
| 112 | + let instance = SystemInstance::instantiate( |
| 113 | + &scope, |
| 114 | + &Name::new(&pkg_name), |
| 115 | + &Name::new(&type_name), |
| 116 | + &Name::new(&impl_name), |
| 117 | + ); |
| 118 | + |
| 119 | + let trace_src = fs::read_to_string(&trace_path).unwrap_or_else(|e| { |
| 120 | + eprintln!("Cannot read trace {trace_path}: {e}"); |
| 121 | + process::exit(1); |
| 122 | + }); |
| 123 | + let events = match parse_ctf(&trace_src) { |
| 124 | + Ok(e) => e, |
| 125 | + Err(e) => { |
| 126 | + eprintln!("Cannot parse trace {trace_path}: {e}"); |
| 127 | + process::exit(1); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + let report = analyze(&events, &instance); |
| 132 | + let want_text = format.as_deref() == Some("text"); |
| 133 | + if want_text { |
| 134 | + print!("{}", report.to_text()); |
| 135 | + } else { |
| 136 | + println!("{}", report.to_json()); |
| 137 | + } |
| 138 | + if report.has_errors() { |
| 139 | + process::exit(1); |
| 140 | + } |
| 141 | +} |
0 commit comments