Skip to content

Commit 1db999c

Browse files
committed
Tweak object representation in display output
1 parent 102911a commit 1db999c

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

src/main.rs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde_json::Value;
44
use std::collections::HashMap;
55
use std::collections::HashSet;
66
use std::env;
7+
use std::fmt;
78
use std::fs;
89

910
fn main() {
@@ -28,27 +29,40 @@ fn display_output(result: Mismatch) {
2829
if no_mismatch == result {
2930
println!("No mismatch was found.");
3031
} else {
32+
match result.keys_in_both {
33+
KeyNode::Node(_) => {
34+
let mut keys = Vec::new();
35+
result.keys_in_both.absolute_keys(&mut keys, None);
36+
println!("Mismatched:");
37+
for key in keys {
38+
println!("{}", key);
39+
}
40+
}
41+
KeyNode::Value(_, _) => println!("Mismatch at root."),
42+
KeyNode::Nil => (),
43+
}
3144
match result.left_only_keys {
32-
KeyNode::Node(_) => println!(
33-
"Following keys are not found in second object: {:?}",
34-
result.left_only_keys
35-
),
45+
KeyNode::Node(_) => {
46+
let mut keys = Vec::new();
47+
result.left_only_keys.absolute_keys(&mut keys, None);
48+
println!("Extra on left:");
49+
for key in keys {
50+
println!("{}", key);
51+
}
52+
}
3653
KeyNode::Value(_, _) => (), // TODO left_only_keys should never be Value type => Throw an error
3754
KeyNode::Nil => (),
3855
}
3956
match result.right_only_keys {
40-
KeyNode::Node(_) => println!(
41-
"Following keys are not found in first object: {:?}",
42-
result.right_only_keys
43-
),
44-
KeyNode::Value(_, _) => (), // TODO right_only_keys should never be Value type => Throw an error
45-
KeyNode::Nil => (),
46-
}
47-
match result.keys_in_both {
4857
KeyNode::Node(_) => {
49-
println!("Following values are not equal: {:?}", result.keys_in_both)
58+
let mut keys = Vec::new();
59+
result.right_only_keys.absolute_keys(&mut keys, None);
60+
println!("Extra on right:");
61+
for key in keys {
62+
println!("{}", key);
63+
}
5064
}
51-
KeyNode::Value(_, _) => println!("Mismatch at root."),
65+
KeyNode::Value(_, _) => (), // TODO right_only_keys should never be Value type => Throw an error
5266
KeyNode::Nil => (),
5367
}
5468
}
@@ -61,6 +75,33 @@ enum KeyNode {
6175
Node(HashMap<String, KeyNode>),
6276
}
6377

78+
impl KeyNode {
79+
fn absolute_keys(&self, keys: &mut Vec<String>, key_from_root: Option<String>) {
80+
let val_key = |key: Option<String>| {
81+
key.map(|mut s| {
82+
s.push_str(" ->");
83+
s
84+
})
85+
.unwrap_or(String::new())
86+
};
87+
let nil_key = |key: Option<String>| key.unwrap_or(String::new());
88+
match self {
89+
KeyNode::Nil => keys.push(nil_key(key_from_root)),
90+
KeyNode::Value(a, b) => {
91+
keys.push(format!("{} [ {} :: {} ]", val_key(key_from_root), a, b))
92+
}
93+
KeyNode::Node(map) => {
94+
for (key, value) in map {
95+
value.absolute_keys(
96+
keys,
97+
Some(format!("{} {}", val_key(key_from_root.clone()), key)),
98+
)
99+
}
100+
}
101+
}
102+
}
103+
}
104+
64105
#[derive(Debug, PartialEq)]
65106
struct Mismatch {
66107
left_only_keys: KeyNode,

0 commit comments

Comments
 (0)