@@ -35,25 +35,22 @@ fn main() {
35
35
let value: Value = serde_json:: from_str ( data) . unwrap ( ) ;
36
36
let value2: Value = serde_json:: from_str ( data2) . unwrap ( ) ;
37
37
38
- if value == value2 {
39
- println ! ( "JSONs match." ) ;
40
- } else {
41
- match match_json ( & value, & value2) {
42
- Mismatch :: NoMismatch => println ! ( "No mismatch was found." ) ,
43
- Mismatch :: ValueMismatch => println ! ( "Mismatch at root." ) ,
44
- Mismatch :: ObjectMismatch ( a, b, c) => {
45
- if let Some ( left_keys) = a {
46
- println ! ( "Following keys were not found in second object: {:?}" , left_keys) ;
47
- }
48
- if let Some ( right_keys) = b {
49
- println ! ( "Following keys were not found in first object: {:?}" , right_keys) ;
50
- }
51
- if let Some ( unequal_keys) = c {
52
- println ! ( "Following keys were not found to be equal: {:?}" , unequal_keys) ;
53
- }
38
+ match match_json ( & value, & value2) {
39
+ Mismatch :: NoMismatch => println ! ( "No mismatch was found." ) ,
40
+ Mismatch :: ValueMismatch => println ! ( "Mismatch at root." ) ,
41
+ Mismatch :: ObjectMismatch ( None , None , None ) => println ! ( "No mismatch was found." ) ,
42
+ Mismatch :: ObjectMismatch ( a, b, c) => {
43
+ if let Some ( left_keys) = a {
44
+ println ! ( "Following keys were not found in second object: {:?}" , left_keys) ;
54
45
}
55
- } ;
56
- }
46
+ if let Some ( right_keys) = b {
47
+ println ! ( "Following keys were not found in first object: {:?}" , right_keys) ;
48
+ }
49
+ if let Some ( unequal_keys) = c {
50
+ println ! ( "Following keys were not found to be equal: {:?}" , unequal_keys) ;
51
+ }
52
+ }
53
+ } ;
57
54
}
58
55
59
56
#[ derive( Eq , PartialEq , Ord , PartialOrd , Debug ) ]
@@ -62,6 +59,7 @@ struct KeyMap {
62
59
children : Option < BTreeSet < KeyMap > > ,
63
60
}
64
61
62
+ #[ derive( Debug ) ]
65
63
enum Mismatch {
66
64
NoMismatch ,
67
65
ValueMismatch ,
@@ -72,34 +70,35 @@ fn match_json(value: &Value, value1: &Value) -> Mismatch {
72
70
match ( value, value1) {
73
71
( Value :: Object ( a) , Value :: Object ( b) ) => {
74
72
let ( left, right, intersection) = intersect_maps ( & a, & b) ;
75
- let mut unequal_keys = BTreeSet :: new ( ) ;
73
+ let mut unequal_keys = None ;
76
74
77
- let mut left = left. iter ( ) . map ( |x| KeyMap { key : String :: from ( x) , children : None } ) . collect :: < BTreeSet < KeyMap > > ( ) ;
78
- let mut right = right. iter ( ) . map ( |x| KeyMap { key : String :: from ( x) , children : None } ) . collect :: < BTreeSet < KeyMap > > ( ) ;
75
+ let mut left = left. map ( |l| l . iter ( ) . map ( |x| KeyMap { key : String :: from ( x) , children : None } ) . collect :: < BTreeSet < KeyMap > > ( ) ) ;
76
+ let mut right = right. map ( |r| r . iter ( ) . map ( |x| KeyMap { key : String :: from ( x) , children : None } ) . collect :: < BTreeSet < KeyMap > > ( ) ) ;
79
77
80
- for key in intersection {
81
- let x = match_json ( & a. get ( & key) . unwrap ( ) , & b. get ( & key) . unwrap ( ) ) ;
82
- if let Some ( keys) = match x {
83
- Mismatch :: NoMismatch => None ,
84
- Mismatch :: ValueMismatch => Some ( KeyMap { key, children : None } ) ,
85
- Mismatch :: ObjectMismatch ( left_keys, right_keys, mismatch_keys) => {
86
- if let Some ( left_keys) = left_keys {
87
- left. insert ( KeyMap { key : String :: from ( & key) , children : Some ( left_keys) } ) ;
88
- }
89
- if let Some ( right_keys) = right_keys {
90
- right. insert ( KeyMap { key : String :: from ( & key) , children : Some ( right_keys) } ) ;
91
- }
92
- if let Some ( mismatch_keys) = mismatch_keys {
93
- Some ( KeyMap { key : String :: from ( & key) , children : Some ( mismatch_keys) } )
94
- } else {
95
- None
96
- }
97
- } ,
98
- } {
99
- unequal_keys. insert ( keys) ;
78
+ if let Some ( intersection) = intersection {
79
+ for key in intersection {
80
+ if let Some ( keys) = match match_json ( & a. get ( & key) . unwrap ( ) , & b. get ( & key) . unwrap ( ) ) {
81
+ Mismatch :: NoMismatch => None ,
82
+ Mismatch :: ValueMismatch => Some ( KeyMap { key, children : None } ) ,
83
+ Mismatch :: ObjectMismatch ( left_keys, right_keys, mismatch_keys) => {
84
+ if let Some ( left_keys) = left_keys {
85
+ left. get_or_insert ( BTreeSet :: new ( ) ) . insert ( KeyMap { key : String :: from ( & key) , children : Some ( left_keys) } ) ;
86
+ }
87
+ if let Some ( right_keys) = right_keys {
88
+ right. get_or_insert ( BTreeSet :: new ( ) ) . insert ( KeyMap { key : String :: from ( & key) , children : Some ( right_keys) } ) ;
89
+ }
90
+ if let Some ( mismatch_keys) = mismatch_keys {
91
+ Some ( KeyMap { key : String :: from ( & key) , children : Some ( mismatch_keys) } )
92
+ } else {
93
+ None
94
+ }
95
+ } ,
96
+ } {
97
+ unequal_keys. get_or_insert ( BTreeSet :: new ( ) ) . insert ( keys) ;
98
+ }
100
99
}
101
100
}
102
- Mismatch :: ObjectMismatch ( Some ( left) , Some ( right) , Some ( unequal_keys) )
101
+ Mismatch :: ObjectMismatch ( left, right, unequal_keys)
103
102
} ,
104
103
( a, b) => {
105
104
if a == b {
@@ -112,8 +111,8 @@ fn match_json(value: &Value, value1: &Value) -> Mismatch {
112
111
}
113
112
114
113
fn intersect_maps < ' a > ( a : & Map < String , Value > ,
115
- b : & Map < String , Value > ) -> ( BTreeSet < String > ,
116
- BTreeSet < String > , BTreeSet < String > ) {
114
+ b : & Map < String , Value > ) -> ( Option < BTreeSet < String > > ,
115
+ Option < BTreeSet < String > > , Option < BTreeSet < String > > ) {
117
116
let mut intersection = BTreeSet :: new ( ) ;
118
117
let mut left = BTreeSet :: new ( ) ;
119
118
let mut right = BTreeSet :: new ( ) ;
@@ -129,5 +128,8 @@ fn intersect_maps<'a>(a: &Map<String, Value>,
129
128
right. insert ( String :: from ( b_key) ) ;
130
129
}
131
130
}
131
+ let left = if left. len ( ) == 0 { None } else { Some ( left) } ;
132
+ let right = if right. len ( ) == 0 { None } else { Some ( right) } ;
133
+ let intersection = if intersection. len ( ) == 0 { None } else { Some ( intersection) } ;
132
134
( left, right, intersection)
133
135
}
0 commit comments