calcit_cli/cli_handlers/
program_diff.rs1use crate::cli_args::ProgramDiffCommand;
2
3use super::structured_output::{StructuredOutputFormat, format_json_value_as_edn};
4
5pub fn handle_program_diff_command(cmd: &ProgramDiffCommand, snapshot_file: &str) -> Result<(), String> {
6 let format = StructuredOutputFormat::parse(&cmd.format, "analyze program-diff")?;
7 if let Some(def) = &cmd.def {
8 if cmd.fail_on_widening {
9 return Err("`--fail-on-widening` gates a whole Snapshot; omit `--def` so every definition's schema is compared.".to_owned());
10 }
11 if format != StructuredOutputFormat::Human {
12 return Err(
13 "Structured program-diff evidence currently requires a whole Snapshot; omit `--def` or use `--format human`.".to_owned(),
14 );
15 }
16 let result = crate::def_diff::analyze_def_diff(def, &cmd.git_ref, cmd.base.as_deref(), snapshot_file)?;
17 println!("{}", crate::def_diff::format_def_diff(&result));
18 } else {
19 let result = crate::program_diff::analyze_program_diff(&cmd.git_ref, cmd.base.as_deref(), snapshot_file)?;
20 let widening = result.evidence.schema_widening.clone();
21 match format {
22 StructuredOutputFormat::Human => println!("{}", crate::program_diff::format_program_diff(&result)),
23 StructuredOutputFormat::Edn => {
24 let value = serde_json::json!({
25 "schema_version": 1,
26 "kind": "program-diff",
27 "data": {
28 "git_ref": result.git_ref,
29 "file_path": result.file_path,
30 "evidence": result.evidence,
31 },
32 });
33 println!("{}", format_json_value_as_edn(&value)?);
34 }
35 StructuredOutputFormat::Json => {
36 let value = serde_json::json!({
37 "schema_version": 1,
38 "kind": "program-diff",
39 "data": {
40 "git_ref": result.git_ref,
41 "file_path": result.file_path,
42 "evidence": result.evidence,
43 },
44 });
45 println!(
46 "{}",
47 serde_json::to_string_pretty(&value).map_err(|error| format!("Failed to encode program-diff JSON: {error}"))?
48 );
49 }
50 }
51 if cmd.fail_on_widening && !widening.is_empty() {
52 return Err(format!(
53 "Schema widening in {}; review each widened slot instead of relaxing the contract.",
54 widening.join(", ")
55 ));
56 }
57 }
58 Ok(())
59}