@@ -63,14 +63,26 @@ pub fn compare(actual: &Value, expected: &Value, mode: MatchMode) -> Result<Comp
6363 let mut errors = Vec :: new ( ) ;
6464 walk ( & mut path, actual, expected, mode, & mut errors) ;
6565 if errors. is_empty ( ) {
66- Ok ( Comparison { passed : true , diff : String :: new ( ) } )
66+ Ok ( Comparison {
67+ passed : true ,
68+ diff : String :: new ( ) ,
69+ } )
6770 } else {
6871 let diff = format_diff ( & errors, actual, expected, mode) ;
69- Ok ( Comparison { passed : false , diff } )
72+ Ok ( Comparison {
73+ passed : false ,
74+ diff,
75+ } )
7076 }
7177}
7278
73- fn walk ( path : & mut String , actual : & Value , expected : & Value , mode : MatchMode , errors : & mut Vec < String > ) {
79+ fn walk (
80+ path : & mut String ,
81+ actual : & Value ,
82+ expected : & Value ,
83+ mode : MatchMode ,
84+ errors : & mut Vec < String > ,
85+ ) {
7486 // Tolerance sentinel: `expected` is `{ "__tol__": { value, tol } }`.
7587 // Recognised at every depth, every match mode.
7688 if let Some ( band) = parse_tol ( expected) {
@@ -99,13 +111,7 @@ fn walk(path: &mut String, actual: &Value, expected: &Value, mode: MatchMode, er
99111 path. push_str ( k) ;
100112 match a. get ( k) {
101113 None => errors. push ( format ! ( "{}: missing key" , path) ) ,
102- Some ( av) => {
103- if mode == MatchMode :: CountOnly {
104- walk ( path, av, ev, mode, errors) ;
105- } else {
106- walk ( path, av, ev, mode, errors) ;
107- }
108- }
114+ Some ( av) => walk ( path, av, ev, mode, errors) ,
109115 }
110116 path. truncate ( len) ;
111117 }
@@ -121,12 +127,7 @@ fn walk(path: &mut String, actual: &Value, expected: &Value, mode: MatchMode, er
121127 ( Value :: Array ( a) , Value :: Array ( e) ) => match mode {
122128 MatchMode :: Exact | MatchMode :: Structural | MatchMode :: CountOnly => {
123129 if a. len ( ) != e. len ( ) {
124- errors. push ( format ! (
125- "{}: array length {} != {}" ,
126- path,
127- a. len( ) ,
128- e. len( )
129- ) ) ;
130+ errors. push ( format ! ( "{}: array length {} != {}" , path, a. len( ) , e. len( ) ) ) ;
130131 return ;
131132 }
132133 if mode == MatchMode :: CountOnly {
@@ -242,7 +243,10 @@ fn format_diff(errors: &[String], actual: &Value, expected: &Value, mode: MatchM
242243}
243244
244245fn prefix_lines ( s : & str , prefix : & str ) -> String {
245- s. lines ( ) . map ( |l| format ! ( "{}{}" , prefix, l) ) . collect :: < Vec < _ > > ( ) . join ( "\n " )
246+ s. lines ( )
247+ . map ( |l| format ! ( "{}{}" , prefix, l) )
248+ . collect :: < Vec < _ > > ( )
249+ . join ( "\n " )
246250}
247251
248252#[ cfg( test) ]
@@ -258,12 +262,7 @@ mod tests {
258262
259263 #[ test]
260264 fn exact_flags_extra_actual_key ( ) {
261- let r = compare (
262- & json ! ( { "a" : 1 , "b" : 2 } ) ,
263- & json ! ( { "a" : 1 } ) ,
264- MatchMode :: Exact ,
265- )
266- . unwrap ( ) ;
265+ let r = compare ( & json ! ( { "a" : 1 , "b" : 2 } ) , & json ! ( { "a" : 1 } ) , MatchMode :: Exact ) . unwrap ( ) ;
267266 assert ! ( !r. passed) ;
268267 assert ! ( r. diff. contains( "unexpected key" ) ) ;
269268 }
@@ -413,7 +412,9 @@ mod tests {
413412 assert ! ( parse_tol( & json!( "x" ) ) . is_none( ) ) ;
414413 // Object but not a single key.
415414 assert ! ( parse_tol( & json!( { } ) ) . is_none( ) ) ;
416- assert ! ( parse_tol( & json!( { TOL_SENTINEL : { "value" : 1.0 , "tol" : 0.1 } , "extra" : 1 } ) ) . is_none( ) ) ;
415+ assert ! (
416+ parse_tol( & json!( { TOL_SENTINEL : { "value" : 1.0 , "tol" : 0.1 } , "extra" : 1 } ) ) . is_none( )
417+ ) ;
417418 }
418419
419420 #[ test]
0 commit comments