Skip to content

Make this crate into a library as well. #2

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

Merged
merged 4 commits into from
Sep 22, 2020
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
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ repository = "https://github.com/ksceriath/json-diff"
keywords = ["cli", "diff", "json"]
categories = ["command-line-utilities"]

[lib]
name = "json_diff"
path = "src/lib.rs"
crate-type = ["lib"]

[[bin]]
name = "json_diff"
path = "src/main.rs"

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

[dependencies]
serde_json = "1.0.41"
maplit = "1.0.2"
colored = "1.9.0"
structopt = "0.3.5"
structopt = "0.3.5"
5 changes: 3 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt;
use colored::*;
use std::fmt;

#[derive(Debug)]
// PartialEq is added for the sake of Test case that uses assert_eq
#[derive(Debug, PartialEq)]
pub enum Message {
BadOption,
SOURCE1,
Expand Down
172 changes: 172 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
pub mod constants;
pub mod ds;
mod process;
use constants::Message;
use ds::mismatch::Mismatch;

pub fn compare_jsons(a: &str, b: &str) -> Result<Mismatch, Message> {
let value1 = match serde_json::from_str(a) {
Ok(val1) => val1,
Err(_) => return Err(Message::JSON1),
};
let value2 = match serde_json::from_str(b) {
Ok(val2) => val2,
Err(_) => return Err(Message::JSON2),
};
Ok(process::match_json(&value1, &value2))
}

#[cfg(test)]
mod tests {
use super::ds::{key_node::KeyNode, mismatch::Mismatch};
use super::*;
use maplit::hashmap;
use serde_json::json;

#[test]
fn nested_diff() {
let data1 = r#"{
"a":"b",
"b":{
"c":{
"d":true,
"e":5,
"f":9,
"h":{
"i":true,
"j":false
}
}
}
}"#;
let data2 = r#"{
"a":"b",
"b":{
"c":{
"d":true,
"e":6,
"g":0,
"h":{
"i":false,
"k":false
}
}
}
}"#;

let expected_left = KeyNode::Node(hashmap! {
"b".to_string() => KeyNode::Node(hashmap! {
"c".to_string() => KeyNode::Node(hashmap! {
"f".to_string() => KeyNode::Nil,
"h".to_string() => KeyNode::Node( hashmap! {
"j".to_string() => KeyNode::Nil,
}
),
}
),
}),
});
let expected_right = KeyNode::Node(hashmap! {
"b".to_string() => KeyNode::Node(hashmap! {
"c".to_string() => KeyNode::Node(hashmap! {
"g".to_string() => KeyNode::Nil,
"h".to_string() => KeyNode::Node(hashmap! {
"k".to_string() => KeyNode::Nil,
}
)
}
)
}
)
});
let expected_uneq = KeyNode::Node(hashmap! {
"b".to_string() => KeyNode::Node(hashmap! {
"c".to_string() => KeyNode::Node(hashmap! {
"e".to_string() => KeyNode::Value(json!(5), json!(6)),
"h".to_string() => KeyNode::Node(hashmap! {
"i".to_string() => KeyNode::Value(json!(true), json!(false)),
}
)
}
)
}
)
});
let expected = Mismatch::new(expected_left, expected_right, expected_uneq);

let mismatch = compare_jsons(data1, data2).unwrap();
assert_eq!(mismatch, expected, "Diff was incorrect.");
}

#[test]
fn no_diff() {
let data1 = r#"{
"a":"b",
"b":{
"c":{
"d":true,
"e":5,
"f":9,
"h":{
"i":true,
"j":false
}
}
}
}"#;
let data2 = r#"{
"a":"b",
"b":{
"c":{
"d":true,
"e":5,
"f":9,
"h":{
"i":true,
"j":false
}
}
}
}"#;

assert_eq!(
compare_jsons(data1, data2).unwrap(),
Mismatch::new(KeyNode::Nil, KeyNode::Nil, KeyNode::Nil)
);
}

#[test]
fn no_json() {
let data1 = r#"{}"#;
let data2 = r#"{}"#;

assert_eq!(
compare_jsons(data1, data2).unwrap(),
Mismatch::new(KeyNode::Nil, KeyNode::Nil, KeyNode::Nil)
);
}

#[test]
fn parse_err_source_one() {
let invalid_json1 = r#"{invalid: json}"#;
let valid_json2 = r#"{"a":"b"}"#;
match compare_jsons(invalid_json1, valid_json2) {
Ok(_) => panic!("This shouldn't be an Ok"),
Err(err) => {
assert_eq!(Message::JSON1, err);
}
};
}

#[test]
fn parse_err_source_two() {
let valid_json1 = r#"{"a":"b"}"#;
let invalid_json2 = r#"{invalid: json}"#;
match compare_jsons(valid_json1, invalid_json2) {
Ok(_) => panic!("This shouldn't be an Ok"),
Err(err) => {
assert_eq!(Message::JSON2, err);
}
};
}
}
Loading