Skip to content

Commit c520362

Browse files
committed
Replace the BTreeSet with HashMap based implementation
1 parent a266c9f commit c520362

File tree

3 files changed

+84
-136
lines changed

3 files changed

+84
-136
lines changed

Cargo.lock

Lines changed: 1 addition & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ edition = "2018"
88

99
[dependencies]
1010
serde_json = "1.0.41"
11-
sugar = "0.2.0"
11+
maplit = "1.0.2"

src/main.rs

Lines changed: 82 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde_json;
22
use serde_json::Map;
33
use serde_json::Value;
4-
use std::collections::BTreeSet;
4+
use std::collections::HashMap;
5+
use std::collections::HashSet;
56
use std::env;
67
use std::fs;
78

@@ -53,21 +54,16 @@ fn display_output(result: Mismatch) {
5354
};
5455
}
5556

56-
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug)]
57+
#[derive(Debug, PartialEq)]
5758
struct KeyMap {
58-
key: String,
59-
children: Option<BTreeSet<KeyMap>>,
59+
keys: HashMap<String, Option<KeyMap>>,
6060
}
6161

62-
#[derive(Debug, PartialEq)]
62+
#[derive(Debug)]
6363
enum Mismatch {
6464
NoMismatch,
6565
ValueMismatch,
66-
ObjectMismatch(
67-
Option<BTreeSet<KeyMap>>,
68-
Option<BTreeSet<KeyMap>>,
69-
Option<BTreeSet<KeyMap>>,
70-
),
66+
ObjectMismatch(Option<KeyMap>, Option<KeyMap>, Option<KeyMap>),
7167
}
7268

7369
fn match_json(value: &Value, value1: &Value) -> Mismatch {
@@ -76,57 +72,58 @@ fn match_json(value: &Value, value1: &Value) -> Mismatch {
7672
let (left, right, intersection) = intersect_maps(&a, &b);
7773
let mut unequal_keys = None;
7874

79-
let mut left = left.map(|l| {
80-
l.iter()
81-
.map(|x| KeyMap {
82-
key: String::from(x),
83-
children: None,
84-
})
85-
.collect::<BTreeSet<KeyMap>>()
75+
let mut left = left.map(|l| KeyMap {
76+
keys: l
77+
.iter()
78+
.map(|x| (String::from(x), None))
79+
.collect::<HashMap<String, Option<KeyMap>>>(),
8680
});
87-
let mut right = right.map(|r| {
88-
r.iter()
89-
.map(|x| KeyMap {
90-
key: String::from(x),
91-
children: None,
92-
})
93-
.collect::<BTreeSet<KeyMap>>()
81+
82+
let mut right = right.map(|r| KeyMap {
83+
keys: r
84+
.iter()
85+
.map(|x| (String::from(x), None))
86+
.collect::<HashMap<String, Option<KeyMap>>>(),
9487
});
9588

9689
if let Some(intersection) = intersection {
9790
for key in intersection {
98-
if let Some(keys) =
91+
if let Some((key, value)) =
9992
match match_json(&a.get(&key).unwrap(), &b.get(&key).unwrap()) {
10093
Mismatch::NoMismatch => None,
101-
Mismatch::ValueMismatch => Some(KeyMap {
102-
key,
103-
children: None,
104-
}),
94+
95+
Mismatch::ValueMismatch => Some((key, None)),
96+
10597
Mismatch::ObjectMismatch(left_keys, right_keys, mismatch_keys) => {
10698
if let Some(left_keys) = left_keys {
107-
left.get_or_insert(BTreeSet::new()).insert(KeyMap {
108-
key: String::from(&key),
109-
children: Some(left_keys),
110-
});
99+
left.get_or_insert(KeyMap {
100+
keys: HashMap::new(),
101+
})
102+
.keys
103+
.insert(String::from(&key), Some(left_keys));
111104
}
112105
if let Some(right_keys) = right_keys {
113-
right.get_or_insert(BTreeSet::new()).insert(KeyMap {
114-
key: String::from(&key),
115-
children: Some(right_keys),
116-
});
106+
right
107+
.get_or_insert(KeyMap {
108+
keys: HashMap::new(),
109+
})
110+
.keys
111+
.insert(String::from(&key), Some(right_keys));
117112
}
118113
if let Some(mismatch_keys) = mismatch_keys {
119-
Some(KeyMap {
120-
key: String::from(&key),
121-
children: Some(mismatch_keys),
122-
})
114+
Some((String::from(&key), Some(mismatch_keys)))
123115
} else {
124116
None
125117
}
126118
}
127119
}
128120
{
129-
unequal_keys.get_or_insert(BTreeSet::new()).insert(keys);
121+
unequal_keys
122+
.get_or_insert(KeyMap {
123+
keys: HashMap::new(),
124+
})
125+
.keys
126+
.insert(key, value);
130127
}
131128
}
132129
}
@@ -146,13 +143,13 @@ fn intersect_maps(
146143
a: &Map<String, Value>,
147144
b: &Map<String, Value>,
148145
) -> (
149-
Option<BTreeSet<String>>,
150-
Option<BTreeSet<String>>,
151-
Option<BTreeSet<String>>,
146+
Option<HashSet<String>>,
147+
Option<HashSet<String>>,
148+
Option<HashSet<String>>,
152149
) {
153-
let mut intersection = BTreeSet::new();
154-
let mut left = BTreeSet::new();
155-
let mut right = BTreeSet::new();
150+
let mut intersection = HashSet::new();
151+
let mut left = HashSet::new();
152+
let mut right = HashSet::new();
156153
for a_key in a.keys() {
157154
if b.contains_key(a_key) {
158155
intersection.insert(String::from(a_key));
@@ -178,7 +175,7 @@ fn intersect_maps(
178175
#[cfg(test)]
179176
mod tests {
180177
use super::*;
181-
use sugar::btreeset;
178+
use maplit::hashmap;
182179

183180
#[test]
184181
fn nested_diff() {
@@ -210,84 +207,65 @@ mod tests {
210207
}
211208
}
212209
}"#;
213-
210+
214211
let mismatch = compare_jsons(data1, data2);
215212
match mismatch {
216213
Mismatch::ObjectMismatch(Some(a), Some(b), Some(c)) => {
217-
let expected_left = btreeset! {
218-
KeyMap {
219-
key: "b".to_string(),
220-
children: Some(btreeset! {
221-
KeyMap {
222-
key: "c".to_string(),
223-
children: Some(btreeset! {
224-
KeyMap {
225-
key: "f".to_string(),
226-
children: None,
227-
},
228-
KeyMap {
229-
key: "h".to_string(),
230-
children: Some(btreeset! {
231-
KeyMap {
232-
key: "j".to_string(),
233-
children: None,
214+
let expected_left = KeyMap {
215+
keys: hashmap! {
216+
"b".to_string() => Some(KeyMap {
217+
keys: hashmap! {
218+
"c".to_string() => Some(KeyMap {
219+
keys: hashmap! {
220+
"f".to_string() => None,
221+
"h".to_string() => Some(KeyMap {
222+
keys: hashmap! {
223+
"j".to_string() => None,
234224
}
235225
})
236226
}
237227
})
238228
}
239229
})
240-
}
230+
},
241231
};
242-
let expected_right = btreeset! {
243-
KeyMap {
244-
key: "b".to_string(),
245-
children: Some(btreeset! {
246-
KeyMap {
247-
key: "c".to_string(),
248-
children: Some(btreeset! {
249-
KeyMap {
250-
key: "g".to_string(),
251-
children: None,
252-
},
253-
KeyMap {
254-
key: "h".to_string(),
255-
children: Some(btreeset! {
256-
KeyMap {
257-
key: "k".to_string(),
258-
children: None,
232+
233+
let expected_right = KeyMap {
234+
keys: hashmap! {
235+
"b".to_string() => Some(KeyMap {
236+
keys: hashmap! {
237+
"c".to_string() => Some(KeyMap {
238+
keys: hashmap! {
239+
"g".to_string() => None,
240+
"h".to_string() => Some(KeyMap {
241+
keys: hashmap! {
242+
"k".to_string() => None,
259243
}
260244
})
261245
}
262246
})
263247
}
264248
})
265-
}
249+
},
266250
};
267-
let expected_uneq = btreeset! {
268-
KeyMap {
269-
key: "b".to_string(),
270-
children: Some(btreeset! {
271-
KeyMap {
272-
key: "c".to_string(),
273-
children: Some(btreeset! {
274-
KeyMap {
275-
key: "e".to_string(),
276-
children: None,
277-
},
278-
KeyMap {
279-
key: "h".to_string(),
280-
children: Some(btreeset! {
281-
KeyMap {
282-
key: "i".to_string(),
283-
children: None,
251+
252+
let expected_uneq = KeyMap {
253+
keys: hashmap! {
254+
"b".to_string() => Some(KeyMap {
255+
keys: hashmap! {
256+
"c".to_string() => Some(KeyMap {
257+
keys: hashmap! {
258+
"e".to_string() => None,
259+
"h".to_string() => Some(KeyMap {
260+
keys: hashmap! {
261+
"i".to_string() => None,
284262
}
285263
})
286264
}
287265
})
288266
}
289267
})
290-
}
268+
},
291269
};
292270

293271
assert_eq!(a, expected_left, "Left was incorrect.");

0 commit comments

Comments
 (0)