Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Add feature for CLI #9

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -19,14 +19,21 @@ 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

[dependencies]
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"
2 changes: 2 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> for Error {
Expand Down
24 changes: 18 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,
}

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 } => {
Expand All @@ -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::<Result<Vec<regex::Regex>>>()
.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);
Expand Down
Loading