@@ -5,6 +5,7 @@ use crate::colorize::colorize_to_array;
55
66/// Auxiliary structure to encapsulate data about the structural difference
77/// of two JSON files.
8+ #[ allow( clippy:: module_name_repetitions) ]
89pub struct JsonDiff {
910 /// Quantifies the difference between two JSON files.
1011 ///
@@ -35,13 +36,15 @@ impl BestMatch {
3536
3637impl JsonDiff {
3738 /// Finds the JSON structural difference of two JSON files.
38- #[ must_use] pub fn diff ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Self {
39+ #[ must_use]
40+ pub fn diff ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Self {
3941 Self :: diff_with_score ( json1, json2, keys_only)
4042 }
4143
4244 /// Finds the JSON structural difference of two JSON files and
4345 /// returns it as a formatted string.
44- #[ must_use] pub fn diff_string ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Option < String > {
46+ #[ must_use]
47+ pub fn diff_string ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Option < String > {
4548 let Self { score : _, diff } = Self :: diff ( json1, json2, keys_only) ;
4649 diff. map ( |value| colorize_to_array ( & value) . join ( "\n " ) + "\n " )
4750 }
@@ -81,6 +84,7 @@ impl JsonDiff {
8184 }
8285
8386 if result. is_empty ( ) {
87+ #[ allow( clippy:: cast_precision_loss) ]
8488 Self {
8589 score : 100. * ( obj1. len ( ) as f64 ) . max ( 0.5 ) ,
8690 diff : None ,
@@ -94,7 +98,6 @@ impl JsonDiff {
9498 }
9599 }
96100
97- #[ inline( always) ]
98101 fn check_type ( item1 : & Value , item2 : & Value ) -> bool {
99102 item1. is_null ( ) == item2. is_null ( )
100103 || item1. is_boolean ( ) == item2. is_boolean ( )
@@ -113,7 +116,7 @@ impl JsonDiff {
113116
114117 for ( match_index, ( key, candidate) ) in fuzzy_originals. into_iter ( ) . enumerate ( ) {
115118 if key != "__next" {
116- let index_distance = ( match_index as isize - index as isize ) . unsigned_abs ( ) ;
119+ let index_distance = ( match_index) . wrapping_sub ( index ) ;
117120 if Self :: check_type ( item, candidate) {
118121 let Self { score, diff : _ } = Self :: diff ( item, candidate, false ) ;
119122 if best_match. as_ref ( ) . map_or ( true , |v| score > v. score )
@@ -173,17 +176,14 @@ impl JsonDiff {
173176 output_array
174177 }
175178
176- #[ inline( always) ]
177179 fn is_scalarized ( key : & str , originals : & Map < String , Value > ) -> bool {
178180 originals. contains_key ( key)
179181 }
180182
181- #[ inline( always) ]
182183 fn get_scalar ( key : & str , scalar_values : & Map < String , Value > ) -> Value {
183184 scalar_values. get ( key) . unwrap ( ) . clone ( )
184185 }
185186
186- #[ inline( always) ]
187187 fn descalarize (
188188 key : & str ,
189189 scalar_values : & Map < String , Value > ,
@@ -196,6 +196,7 @@ impl JsonDiff {
196196 }
197197 }
198198
199+ #[ allow( clippy:: too_many_lines) ]
199200 fn array_diff ( array1 : & [ Value ] , array2 : & [ Value ] , keys_only : bool ) -> Self {
200201 let mut originals1 = Map :: new ( ) ;
201202 let mut scalar_values1 = Map :: new ( ) ;
@@ -228,7 +229,7 @@ impl JsonDiff {
228229 "equal" => {
229230 for key in seq1. iter ( ) . take ( opcode. first_end ) . skip ( opcode. first_start ) {
230231 let is_scalarized1 = Self :: is_scalarized ( key, & originals1) ;
231- assert ! ( !( is_scalarized1 && ! ( Self :: is_scalarized( key, & originals2) ) ) ,
232+ assert ! ( !is_scalarized1 || ( Self :: is_scalarized( key, & originals2) ) ,
232233 "Internal bug: the items associated to the key {} are different in the two dictionaries" ,
233234 key
234235 ) ;
@@ -275,26 +276,7 @@ impl JsonDiff {
275276 }
276277 }
277278 "replace" => {
278- if !keys_only {
279- for key in seq1. iter ( ) . take ( opcode. first_end ) . skip ( opcode. first_start ) {
280- result. push ( json ! ( [
281- json!( '-' ) ,
282- Self :: descalarize( key, & scalar_values1, & originals1)
283- ] ) ) ;
284- score -= 5. ;
285- }
286- for key in seq2
287- . iter ( )
288- . take ( opcode. second_end )
289- . skip ( opcode. second_start )
290- {
291- result. push ( json ! ( [
292- json!( '+' ) ,
293- Self :: descalarize( key, & scalar_values2, & originals2)
294- ] ) ) ;
295- score -= 5. ;
296- }
297- } else {
279+ if keys_only {
298280 for ( key1, key2) in seq1
299281 . iter ( )
300282 . take ( opcode. first_end )
@@ -322,6 +304,25 @@ impl JsonDiff {
322304 result. push ( json ! ( ' ' ) ) ;
323305 }
324306 }
307+ } else {
308+ for key in seq1. iter ( ) . take ( opcode. first_end ) . skip ( opcode. first_start ) {
309+ result. push ( json ! ( [
310+ json!( '-' ) ,
311+ Self :: descalarize( key, & scalar_values1, & originals1)
312+ ] ) ) ;
313+ score -= 5. ;
314+ }
315+ for key in seq2
316+ . iter ( )
317+ . take ( opcode. second_end )
318+ . skip ( opcode. second_start )
319+ {
320+ result. push ( json ! ( [
321+ json!( '+' ) ,
322+ Self :: descalarize( key, & scalar_values2, & originals2)
323+ ] ) ) ;
324+ score -= 5. ;
325+ }
325326 }
326327 }
327328 _ => all_equal = true ,
0 commit comments