@@ -35,19 +35,15 @@ impl BestMatch {
35
35
36
36
impl JsonDiff {
37
37
/// Finds the JSON structural difference of two JSON files.
38
- pub fn diff ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Self {
38
+ # [ must_use ] pub fn diff ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Self {
39
39
Self :: diff_with_score ( json1, json2, keys_only)
40
40
}
41
41
42
42
/// Finds the JSON structural difference of two JSON files and
43
43
/// returns it as a formatted string.
44
- pub fn diff_string ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Option < String > {
44
+ # [ must_use ] pub fn diff_string ( json1 : & Value , json2 : & Value , keys_only : bool ) -> Option < String > {
45
45
let Self { score : _, diff } = Self :: diff ( json1, json2, keys_only) ;
46
- if let Some ( value) = diff {
47
- Some ( colorize_to_array ( & value) . join ( "\n " ) + "\n " )
48
- } else {
49
- None
50
- }
46
+ diff. map ( |value| colorize_to_array ( & value) . join ( "\n " ) + "\n " )
51
47
}
52
48
53
49
fn object_diff ( obj1 : & Map < String , Value > , obj2 : & Map < String , Value > , keys_only : bool ) -> Self {
@@ -56,15 +52,15 @@ impl JsonDiff {
56
52
57
53
for ( key, value1) in obj1 {
58
54
if !obj2. contains_key ( key) {
59
- let key_deleted = format ! ( "{}__deleted" , key ) ;
55
+ let key_deleted = format ! ( "{key }__deleted" ) ;
60
56
result. insert ( key_deleted, value1. clone ( ) ) ;
61
57
score -= 30. ;
62
58
}
63
59
}
64
60
65
61
for ( key, value2) in obj2 {
66
62
if !obj1. contains_key ( key) {
67
- let key_added = format ! ( "{}__added" , key ) ;
63
+ let key_added = format ! ( "{key }__added" ) ;
68
64
result. insert ( key_added, value2. clone ( ) ) ;
69
65
score -= 30. ;
70
66
}
@@ -76,11 +72,11 @@ impl JsonDiff {
76
72
let Self {
77
73
score : subscore,
78
74
diff : change,
79
- } = Self :: diff_with_score ( & value1, & value2, keys_only) ;
75
+ } = Self :: diff_with_score ( value1, value2, keys_only) ;
80
76
if let Some ( change) = change {
81
77
result. insert ( key. clone ( ) , change) ;
82
78
}
83
- score += ( ( subscore / 5. ) . max ( -10. ) ) . min ( 20. ) ;
79
+ score += ( subscore / 5. ) . clamp ( -10. , 20. ) ;
84
80
}
85
81
}
86
82
@@ -117,7 +113,7 @@ impl JsonDiff {
117
113
118
114
for ( match_index, ( key, candidate) ) in fuzzy_originals. into_iter ( ) . enumerate ( ) {
119
115
if key != "__next" {
120
- let index_distance = ( match_index as isize - index as isize ) . abs ( ) as usize ;
116
+ let index_distance = ( match_index as isize - index as isize ) . unsigned_abs ( ) ;
121
117
if Self :: check_type ( item, candidate) {
122
118
let Self { score, diff : _ } = Self :: diff ( item, candidate, false ) ;
123
119
if best_match. as_ref ( ) . map_or ( true , |v| score > v. score )
@@ -232,12 +228,10 @@ impl JsonDiff {
232
228
"equal" => {
233
229
for key in seq1. iter ( ) . take ( opcode. first_end ) . skip ( opcode. first_start ) {
234
230
let is_scalarized1 = Self :: is_scalarized ( key, & originals1) ;
235
- if is_scalarized1 && !( Self :: is_scalarized ( key, & originals2) ) {
236
- panic ! (
231
+ assert ! ( !( is_scalarized1 && !( Self :: is_scalarized( key, & originals2) ) ) ,
237
232
"Internal bug: the items associated to the key {} are different in the two dictionaries" ,
238
233
key
239
234
) ;
240
- }
241
235
if is_scalarized1 {
242
236
let item1 = Self :: descalarize ( key, & scalar_values1, & originals1) ;
243
237
let item2 = Self :: descalarize ( key, & scalar_values2, & originals2) ;
0 commit comments