@@ -9,7 +9,6 @@ use std::process;
9
9
use clap:: { App , Arg } ;
10
10
use console:: Term ;
11
11
use rayon:: prelude:: * ;
12
- use serde_json:: Value ;
13
12
use walkdir:: { DirEntry , WalkDir } ;
14
13
15
14
use json_structural_diff:: { colorize, JsonDiff } ;
@@ -27,25 +26,28 @@ fn act_on_file(
27
26
cfg : & Config ,
28
27
) -> std:: io:: Result < ( ) > {
29
28
let buffer1 = std:: fs:: read ( & path1) . unwrap ( ) ;
30
- let json1: Value = serde_json:: from_slice ( & buffer1) . unwrap ( ) ;
31
29
let buffer2 = std:: fs:: read ( path2) . unwrap ( ) ;
32
- let json2: Value = serde_json:: from_slice ( & buffer2) . unwrap ( ) ;
33
-
34
- if json1 != json2 {
35
- let json_diff = JsonDiff :: diff ( & json1, & json2, cfg. only_keys ) ;
36
- let result = json_diff. diff . unwrap ( ) ;
37
- let json_string = if cfg. raw {
38
- serde_json:: to_string_pretty ( & result) ?
39
- } else {
40
- colorize ( & result, cfg. color )
41
- } ;
42
- if let Some ( output_path) = output_path {
43
- let output_filename = path1. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
44
- let mut output_file = File :: create ( output_path. join ( output_filename) ) ?;
45
- writeln ! ( & mut output_file, "{}" , json_string) ?;
46
- } else {
47
- let mut term = Term :: stdout ( ) ;
48
- term. write_all ( json_string. as_bytes ( ) ) ?;
30
+
31
+ if let ( Ok ( json1) , Ok ( json2) ) = (
32
+ serde_json:: from_slice ( & buffer1) ,
33
+ serde_json:: from_slice ( & buffer2) ,
34
+ ) {
35
+ if json1 != json2 {
36
+ let json_diff = JsonDiff :: diff ( & json1, & json2, cfg. only_keys ) ;
37
+ let result = json_diff. diff . unwrap ( ) ;
38
+ let json_string = if cfg. raw {
39
+ serde_json:: to_string_pretty ( & result) ?
40
+ } else {
41
+ colorize ( & result, cfg. color )
42
+ } ;
43
+ if let Some ( output_path) = output_path {
44
+ let output_filename = path1. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
45
+ let mut output_file = File :: create ( output_path. join ( output_filename) ) ?;
46
+ writeln ! ( & mut output_file, "{}" , json_string) ?;
47
+ } else {
48
+ let mut term = Term :: stdout ( ) ;
49
+ term. write_all ( json_string. as_bytes ( ) ) ?;
50
+ }
49
51
}
50
52
}
51
53
Ok ( ( ) )
0 commit comments