From aaf53b5fedeff3fb2f666af0067edfa444529163 Mon Sep 17 00:00:00 2001 From: Christopher Regali Date: Thu, 30 May 2024 22:22:04 +0200 Subject: [PATCH] - Add feature for CLI - Make it a default feature - Add regex option to CLI --- Cargo.toml | 21 ++++++++++++++------- src/enums.rs | 2 ++ src/main.rs | 24 ++++++++++++++++++------ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c478c32..56b6355 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,15 @@ [package] name = "json_diff_ng" -version = "0.6.0-RC3" -authors = ["ksceriath", "ChrisRega"] +version = "0.6.0" +authors = ["ChrisRega", "ksceriath"] edition = "2021" license = "Unlicense" -description = "A JSON diff library and CLI." +description = "A JSON diff library, featuring deep-sorting and key exclusion by regex. CLI is included." readme = "README.md" homepage = "https://github.com/ChrisRega/json-diff" repository = "https://github.com/ChrisRega/json-diff" -keywords = ["cli", "diff", "json"] -categories = ["command-line-utilities"] +categories = ["command-line-utilities", "development-tools"] +keywords = ["json-structural-diff", "json-diff", "diff", "json", "cli"] [lib] name = "json_diff_ng" @@ -19,6 +19,11 @@ crate-type = ["lib"] [[bin]] name = "json_diff_ng" path = "src/main.rs" +required-features = ["CLI"] + +[features] +default = ["CLI"] +CLI = ["dep:clap"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -26,7 +31,9 @@ path = "src/main.rs" thiserror = "1.0" vg_errortools = "0.1" serde_json = { version = "1.0", features = ["preserve_order"] } -maplit = "1.0" -clap = { version = "4.5", features = ["derive"] } diffs = "0.5" regex = "1.10" +clap = { version = "4.5", features = ["derive"], optional = true } + +[dev-dependencies] +maplit = "1.0" diff --git a/src/enums.rs b/src/enums.rs index 00728fe..2e95d9e 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -13,6 +13,8 @@ pub enum Error { IOError(#[from] FatIOError), #[error("Error parsing first json: {0}")] JSON(#[from] serde_json::Error), + #[error("Regex compilation error: {0}")] + Regex(#[from] regex::Error), } impl From for Error { diff --git a/src/main.rs b/src/main.rs index d5b4d14..31b0497 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,13 +23,14 @@ struct Args { /// deep-sort arrays before comparing sort_arrays: bool, - #[clap(short, long, default_value_t = 20)] - /// truncate keys with more chars then this parameter - truncation_length: usize, + #[clap(short, long)] + /// Exclude a given list of keys by regex. + exclude_keys: Option>, } fn main() -> Result<()> { let args = Args::parse(); + println!("Getting input"); let (json_1, json_2) = match args.cmd { Mode::Direct { json_2, json_1 } => (json_1, json_2), Mode::File { file_2, file_1 } => { @@ -38,9 +39,20 @@ fn main() -> Result<()> { (d1, d2) } }; - - let mismatch = compare_strs(&json_1, &json_2, args.sort_arrays, &[])?; - + println!("Evaluation exclusion regex list"); + let exclusion_keys = args + .exclude_keys + .as_ref() + .map(|v| { + v.iter() + .map(|k| regex::Regex::new(k).map_err(|e| e.into())) + .collect::>>() + .unwrap_or_default() + }) + .unwrap_or_default(); + println!("Comparing"); + let mismatch = compare_strs(&json_1, &json_2, args.sort_arrays, &exclusion_keys)?; + println!("Printing results"); let comparison_result = check_diffs(mismatch)?; if !comparison_result { std::process::exit(1);