Skip to content

Commit aaf53b5

Browse files
committed
- Add feature for CLI
- Make it a default feature - Add regex option to CLI
1 parent 4fbf3c2 commit aaf53b5

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Cargo.toml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[package]
22
name = "json_diff_ng"
3-
version = "0.6.0-RC3"
4-
authors = ["ksceriath", "ChrisRega"]
3+
version = "0.6.0"
4+
authors = ["ChrisRega", "ksceriath"]
55
edition = "2021"
66
license = "Unlicense"
7-
description = "A JSON diff library and CLI."
7+
description = "A JSON diff library, featuring deep-sorting and key exclusion by regex. CLI is included."
88
readme = "README.md"
99
homepage = "https://github.com/ChrisRega/json-diff"
1010
repository = "https://github.com/ChrisRega/json-diff"
11-
keywords = ["cli", "diff", "json"]
12-
categories = ["command-line-utilities"]
11+
categories = ["command-line-utilities", "development-tools"]
12+
keywords = ["json-structural-diff", "json-diff", "diff", "json", "cli"]
1313

1414
[lib]
1515
name = "json_diff_ng"
@@ -19,14 +19,21 @@ crate-type = ["lib"]
1919
[[bin]]
2020
name = "json_diff_ng"
2121
path = "src/main.rs"
22+
required-features = ["CLI"]
23+
24+
[features]
25+
default = ["CLI"]
26+
CLI = ["dep:clap"]
2227

2328
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2429

2530
[dependencies]
2631
thiserror = "1.0"
2732
vg_errortools = "0.1"
2833
serde_json = { version = "1.0", features = ["preserve_order"] }
29-
maplit = "1.0"
30-
clap = { version = "4.5", features = ["derive"] }
3134
diffs = "0.5"
3235
regex = "1.10"
36+
clap = { version = "4.5", features = ["derive"], optional = true }
37+
38+
[dev-dependencies]
39+
maplit = "1.0"

src/enums.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub enum Error {
1313
IOError(#[from] FatIOError),
1414
#[error("Error parsing first json: {0}")]
1515
JSON(#[from] serde_json::Error),
16+
#[error("Regex compilation error: {0}")]
17+
Regex(#[from] regex::Error),
1618
}
1719

1820
impl From<String> for Error {

src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ struct Args {
2323
/// deep-sort arrays before comparing
2424
sort_arrays: bool,
2525

26-
#[clap(short, long, default_value_t = 20)]
27-
/// truncate keys with more chars then this parameter
28-
truncation_length: usize,
26+
#[clap(short, long)]
27+
/// Exclude a given list of keys by regex.
28+
exclude_keys: Option<Vec<String>>,
2929
}
3030

3131
fn main() -> Result<()> {
3232
let args = Args::parse();
33+
println!("Getting input");
3334
let (json_1, json_2) = match args.cmd {
3435
Mode::Direct { json_2, json_1 } => (json_1, json_2),
3536
Mode::File { file_2, file_1 } => {
@@ -38,9 +39,20 @@ fn main() -> Result<()> {
3839
(d1, d2)
3940
}
4041
};
41-
42-
let mismatch = compare_strs(&json_1, &json_2, args.sort_arrays, &[])?;
43-
42+
println!("Evaluation exclusion regex list");
43+
let exclusion_keys = args
44+
.exclude_keys
45+
.as_ref()
46+
.map(|v| {
47+
v.iter()
48+
.map(|k| regex::Regex::new(k).map_err(|e| e.into()))
49+
.collect::<Result<Vec<regex::Regex>>>()
50+
.unwrap_or_default()
51+
})
52+
.unwrap_or_default();
53+
println!("Comparing");
54+
let mismatch = compare_strs(&json_1, &json_2, args.sort_arrays, &exclusion_keys)?;
55+
println!("Printing results");
4456
let comparison_result = check_diffs(mismatch)?;
4557
if !comparison_result {
4658
std::process::exit(1);

0 commit comments

Comments
 (0)