@@ -35,19 +35,15 @@ impl BestMatch {
3535
3636impl JsonDiff {
3737 /// 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 {
3939 Self :: diff_with_score ( json1, json2, keys_only)
4040 }
4141
4242 /// Finds the JSON structural difference of two JSON files and
4343 /// 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 > {
4545 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 " )
5147 }
5248
5349 fn object_diff ( obj1 : & Map < String , Value > , obj2 : & Map < String , Value > , keys_only : bool ) -> Self {
@@ -56,15 +52,15 @@ impl JsonDiff {
5652
5753 for ( key, value1) in obj1 {
5854 if !obj2. contains_key ( key) {
59- let key_deleted = format ! ( "{}__deleted" , key ) ;
55+ let key_deleted = format ! ( "{key }__deleted" ) ;
6056 result. insert ( key_deleted, value1. clone ( ) ) ;
6157 score -= 30. ;
6258 }
6359 }
6460
6561 for ( key, value2) in obj2 {
6662 if !obj1. contains_key ( key) {
67- let key_added = format ! ( "{}__added" , key ) ;
63+ let key_added = format ! ( "{key }__added" ) ;
6864 result. insert ( key_added, value2. clone ( ) ) ;
6965 score -= 30. ;
7066 }
@@ -76,11 +72,11 @@ impl JsonDiff {
7672 let Self {
7773 score : subscore,
7874 diff : change,
79- } = Self :: diff_with_score ( & value1, & value2, keys_only) ;
75+ } = Self :: diff_with_score ( value1, value2, keys_only) ;
8076 if let Some ( change) = change {
8177 result. insert ( key. clone ( ) , change) ;
8278 }
83- score += ( ( subscore / 5. ) . max ( -10. ) ) . min ( 20. ) ;
79+ score += ( subscore / 5. ) . clamp ( -10. , 20. ) ;
8480 }
8581 }
8682
@@ -117,7 +113,7 @@ impl JsonDiff {
117113
118114 for ( match_index, ( key, candidate) ) in fuzzy_originals. into_iter ( ) . enumerate ( ) {
119115 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 ( ) ;
121117 if Self :: check_type ( item, candidate) {
122118 let Self { score, diff : _ } = Self :: diff ( item, candidate, false ) ;
123119 if best_match. as_ref ( ) . map_or ( true , |v| score > v. score )
@@ -232,12 +228,10 @@ impl JsonDiff {
232228 "equal" => {
233229 for key in seq1. iter ( ) . take ( opcode. first_end ) . skip ( opcode. first_start ) {
234230 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) ) ) ,
237232 "Internal bug: the items associated to the key {} are different in the two dictionaries" ,
238233 key
239234 ) ;
240- }
241235 if is_scalarized1 {
242236 let item1 = Self :: descalarize ( key, & scalar_values1, & originals1) ;
243237 let item2 = Self :: descalarize ( key, & scalar_values2, & originals2) ;
0 commit comments