@@ -5,6 +5,7 @@ use crate::colorize::colorize_to_array;
5
5
6
6
/// Auxiliary structure to encapsulate data about the structural difference
7
7
/// of two JSON files.
8
+ #[ allow( clippy:: module_name_repetitions) ]
8
9
pub struct JsonDiff {
9
10
/// Quantifies the difference between two JSON files.
10
11
///
@@ -35,13 +36,15 @@ impl BestMatch {
35
36
36
37
impl JsonDiff {
37
38
/// 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 {
39
41
Self :: diff_with_score ( json1, json2, keys_only)
40
42
}
41
43
42
44
/// Finds the JSON structural difference of two JSON files and
43
45
/// 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 > {
45
48
let Self { score : _, diff } = Self :: diff ( json1, json2, keys_only) ;
46
49
diff. map ( |value| colorize_to_array ( & value) . join ( "\n " ) + "\n " )
47
50
}
@@ -81,6 +84,7 @@ impl JsonDiff {
81
84
}
82
85
83
86
if result. is_empty ( ) {
87
+ #[ allow( clippy:: cast_precision_loss) ]
84
88
Self {
85
89
score : 100. * ( obj1. len ( ) as f64 ) . max ( 0.5 ) ,
86
90
diff : None ,
@@ -94,7 +98,6 @@ impl JsonDiff {
94
98
}
95
99
}
96
100
97
- #[ inline( always) ]
98
101
fn check_type ( item1 : & Value , item2 : & Value ) -> bool {
99
102
item1. is_null ( ) == item2. is_null ( )
100
103
|| item1. is_boolean ( ) == item2. is_boolean ( )
@@ -113,7 +116,7 @@ impl JsonDiff {
113
116
114
117
for ( match_index, ( key, candidate) ) in fuzzy_originals. into_iter ( ) . enumerate ( ) {
115
118
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 ) ;
117
120
if Self :: check_type ( item, candidate) {
118
121
let Self { score, diff : _ } = Self :: diff ( item, candidate, false ) ;
119
122
if best_match. as_ref ( ) . map_or ( true , |v| score > v. score )
@@ -173,17 +176,14 @@ impl JsonDiff {
173
176
output_array
174
177
}
175
178
176
- #[ inline( always) ]
177
179
fn is_scalarized ( key : & str , originals : & Map < String , Value > ) -> bool {
178
180
originals. contains_key ( key)
179
181
}
180
182
181
- #[ inline( always) ]
182
183
fn get_scalar ( key : & str , scalar_values : & Map < String , Value > ) -> Value {
183
184
scalar_values. get ( key) . unwrap ( ) . clone ( )
184
185
}
185
186
186
- #[ inline( always) ]
187
187
fn descalarize (
188
188
key : & str ,
189
189
scalar_values : & Map < String , Value > ,
@@ -196,6 +196,7 @@ impl JsonDiff {
196
196
}
197
197
}
198
198
199
+ #[ allow( clippy:: too_many_lines) ]
199
200
fn array_diff ( array1 : & [ Value ] , array2 : & [ Value ] , keys_only : bool ) -> Self {
200
201
let mut originals1 = Map :: new ( ) ;
201
202
let mut scalar_values1 = Map :: new ( ) ;
@@ -228,7 +229,7 @@ impl JsonDiff {
228
229
"equal" => {
229
230
for key in seq1. iter ( ) . take ( opcode. first_end ) . skip ( opcode. first_start ) {
230
231
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) ) ,
232
233
"Internal bug: the items associated to the key {} are different in the two dictionaries" ,
233
234
key
234
235
) ;
@@ -275,26 +276,7 @@ impl JsonDiff {
275
276
}
276
277
}
277
278
"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 {
298
280
for ( key1, key2) in seq1
299
281
. iter ( )
300
282
. take ( opcode. first_end )
@@ -322,6 +304,25 @@ impl JsonDiff {
322
304
result. push ( json ! ( ' ' ) ) ;
323
305
}
324
306
}
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
+ }
325
326
}
326
327
}
327
328
_ => all_equal = true ,
0 commit comments