Skip to content
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
171 changes: 1 addition & 170 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,172 +1,3 @@
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);
}
};
}
}
pub mod process;
14 changes: 8 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use colored::*;
use json_diff::{
compare_jsons,
constants::Message,
ds::{key_node::KeyNode, mismatch::Mismatch},
};
use std::{
fmt, fs,
io::{self, Write},
process as proc,
str::FromStr,
};

use colored::*;
use structopt::StructOpt;

use json_diff::{
constants::Message,
ds::{key_node::KeyNode, mismatch::Mismatch},
process::compare_jsons,
};

const HELP: &str = r#"
Example:
json_diff f source1.json source2.json
Expand Down
177 changes: 173 additions & 4 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
use crate::ds::key_node::KeyNode;
use crate::ds::mismatch::Mismatch;
use serde_json::Map;
use serde_json::Value;
use std::collections::HashMap;
use std::collections::HashSet;

use serde_json::Map;
use serde_json::Value;

use crate::constants::Message;
use crate::ds::key_node::KeyNode;
use crate::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(match_json(&value1, &value2))
}

pub fn match_json(value1: &Value, value2: &Value) -> Mismatch {
match (value1, value2) {
(Value::Object(a), Value::Object(b)) => {
Expand Down Expand Up @@ -102,3 +117,157 @@ fn intersect_maps(
};
(left, right, intersection)
}

#[cfg(test)]
mod tests {
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);
}
};
}
}