Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6b6453

Browse files
Merge pull request #2 from nohupped/master
Make this crate into a library as well.
2 parents e153b46 + 34f270f commit b6b6453

File tree

4 files changed

+219
-171
lines changed

4 files changed

+219
-171
lines changed

‎Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@ repository = "https://github.com/ksceriath/json-diff"
1111
keywords = ["cli", "diff", "json"]
1212
categories = ["command-line-utilities"]
1313

14+
[lib]
15+
name = "json_diff"
16+
path = "src/lib.rs"
17+
crate-type = ["lib"]
18+
19+
[[bin]]
20+
name = "json_diff"
21+
path = "src/main.rs"
22+
1423
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1524

1625
[dependencies]
1726
serde_json = "1.0.41"
1827
maplit = "1.0.2"
1928
colored = "1.9.0"
20-
structopt = "0.3.5"
29+
structopt = "0.3.5"

‎src/constants.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::fmt;
21
use colored::*;
2+
use std::fmt;
33

4-
#[derive(Debug)]
4+
// PartialEq is added for the sake of Test case that uses assert_eq
5+
#[derive(Debug, PartialEq)]
56
pub enum Message {
67
BadOption,
78
SOURCE1,

‎src/lib.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
pub mod constants;
2+
pub mod ds;
3+
mod process;
4+
use constants::Message;
5+
use ds::mismatch::Mismatch;
6+
7+
pub fn compare_jsons(a: &str, b: &str) -> Result<Mismatch, Message> {
8+
let value1 = match serde_json::from_str(a) {
9+
Ok(val1) => val1,
10+
Err(_) => return Err(Message::JSON1),
11+
};
12+
let value2 = match serde_json::from_str(b) {
13+
Ok(val2) => val2,
14+
Err(_) => return Err(Message::JSON2),
15+
};
16+
Ok(process::match_json(&value1, &value2))
17+
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use super::ds::{key_node::KeyNode, mismatch::Mismatch};
22+
use super::*;
23+
use maplit::hashmap;
24+
use serde_json::json;
25+
26+
#[test]
27+
fn nested_diff() {
28+
let data1 = r#"{
29+
"a":"b",
30+
"b":{
31+
"c":{
32+
"d":true,
33+
"e":5,
34+
"f":9,
35+
"h":{
36+
"i":true,
37+
"j":false
38+
}
39+
}
40+
}
41+
}"#;
42+
let data2 = r#"{
43+
"a":"b",
44+
"b":{
45+
"c":{
46+
"d":true,
47+
"e":6,
48+
"g":0,
49+
"h":{
50+
"i":false,
51+
"k":false
52+
}
53+
}
54+
}
55+
}"#;
56+
57+
let expected_left = KeyNode::Node(hashmap! {
58+
"b".to_string() => KeyNode::Node(hashmap! {
59+
"c".to_string() => KeyNode::Node(hashmap! {
60+
"f".to_string() => KeyNode::Nil,
61+
"h".to_string() => KeyNode::Node( hashmap! {
62+
"j".to_string() => KeyNode::Nil,
63+
}
64+
),
65+
}
66+
),
67+
}),
68+
});
69+
let expected_right = KeyNode::Node(hashmap! {
70+
"b".to_string() => KeyNode::Node(hashmap! {
71+
"c".to_string() => KeyNode::Node(hashmap! {
72+
"g".to_string() => KeyNode::Nil,
73+
"h".to_string() => KeyNode::Node(hashmap! {
74+
"k".to_string() => KeyNode::Nil,
75+
}
76+
)
77+
}
78+
)
79+
}
80+
)
81+
});
82+
let expected_uneq = KeyNode::Node(hashmap! {
83+
"b".to_string() => KeyNode::Node(hashmap! {
84+
"c".to_string() => KeyNode::Node(hashmap! {
85+
"e".to_string() => KeyNode::Value(json!(5), json!(6)),
86+
"h".to_string() => KeyNode::Node(hashmap! {
87+
"i".to_string() => KeyNode::Value(json!(true), json!(false)),
88+
}
89+
)
90+
}
91+
)
92+
}
93+
)
94+
});
95+
let expected = Mismatch::new(expected_left, expected_right, expected_uneq);
96+
97+
let mismatch = compare_jsons(data1, data2).unwrap();
98+
assert_eq!(mismatch, expected, "Diff was incorrect.");
99+
}
100+
101+
#[test]
102+
fn no_diff() {
103+
let data1 = r#"{
104+
"a":"b",
105+
"b":{
106+
"c":{
107+
"d":true,
108+
"e":5,
109+
"f":9,
110+
"h":{
111+
"i":true,
112+
"j":false
113+
}
114+
}
115+
}
116+
}"#;
117+
let data2 = r#"{
118+
"a":"b",
119+
"b":{
120+
"c":{
121+
"d":true,
122+
"e":5,
123+
"f":9,
124+
"h":{
125+
"i":true,
126+
"j":false
127+
}
128+
}
129+
}
130+
}"#;
131+
132+
assert_eq!(
133+
compare_jsons(data1, data2).unwrap(),
134+
Mismatch::new(KeyNode::Nil, KeyNode::Nil, KeyNode::Nil)
135+
);
136+
}
137+
138+
#[test]
139+
fn no_json() {
140+
let data1 = r#"{}"#;
141+
let data2 = r#"{}"#;
142+
143+
assert_eq!(
144+
compare_jsons(data1, data2).unwrap(),
145+
Mismatch::new(KeyNode::Nil, KeyNode::Nil, KeyNode::Nil)
146+
);
147+
}
148+
149+
#[test]
150+
fn parse_err_source_one() {
151+
let invalid_json1 = r#"{invalid: json}"#;
152+
let valid_json2 = r#"{"a":"b"}"#;
153+
match compare_jsons(invalid_json1, valid_json2) {
154+
Ok(_) => panic!("This shouldn't be an Ok"),
155+
Err(err) => {
156+
assert_eq!(Message::JSON1, err);
157+
}
158+
};
159+
}
160+
161+
#[test]
162+
fn parse_err_source_two() {
163+
let valid_json1 = r#"{"a":"b"}"#;
164+
let invalid_json2 = r#"{invalid: json}"#;
165+
match compare_jsons(valid_json1, invalid_json2) {
166+
Ok(_) => panic!("This shouldn't be an Ok"),
167+
Err(err) => {
168+
assert_eq!(Message::JSON2, err);
169+
}
170+
};
171+
}
172+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /