@@ -3,7 +3,7 @@ extern crate clap;
3
3
4
4
use std:: fs:: File ;
5
5
use std:: io:: Write ;
6
- use std:: path:: PathBuf ;
6
+ use std:: path:: { Path , PathBuf } ;
7
7
use std:: process;
8
8
9
9
use clap:: { App , Arg } ;
@@ -22,10 +22,10 @@ struct Config {
22
22
fn act_on_file (
23
23
path1 : & PathBuf ,
24
24
path2 : & PathBuf ,
25
- output_path : & Option < PathBuf > ,
25
+ output_path : Option < & PathBuf > ,
26
26
cfg : & Config ,
27
27
) -> std:: io:: Result < ( ) > {
28
- let buffer1 = std:: fs:: read ( & path1) . unwrap ( ) ;
28
+ let buffer1 = std:: fs:: read ( path1) . unwrap ( ) ;
29
29
let buffer2 = std:: fs:: read ( path2) . unwrap ( ) ;
30
30
31
31
if let ( Ok ( json1) , Ok ( json2) ) = (
@@ -43,7 +43,7 @@ fn act_on_file(
43
43
if let Some ( output_path) = output_path {
44
44
let output_filename = path1. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
45
45
let mut output_file = File :: create ( output_path. join ( output_filename) ) ?;
46
- writeln ! ( & mut output_file, "{}" , json_string ) ?;
46
+ writeln ! ( & mut output_file, "{json_string}" ) ?;
47
47
} else {
48
48
let mut term = Term :: stdout ( ) ;
49
49
term. write_all ( json_string. as_bytes ( ) ) ?;
@@ -57,21 +57,15 @@ fn is_hidden(entry: &DirEntry) -> bool {
57
57
entry
58
58
. file_name ( )
59
59
. to_str ( )
60
- . map ( |s| s. starts_with ( '.' ) )
61
- . unwrap_or ( false )
60
+ . is_some_and ( |s| s. starts_with ( '.' ) )
62
61
}
63
62
64
- fn explore (
65
- path1 : & PathBuf ,
66
- path2 : & PathBuf ,
67
- output_path : & Option < PathBuf > ,
68
- cfg : & Config ,
69
- ) -> std:: io:: Result < ( ) > {
70
- WalkDir :: new ( & path1)
63
+ fn explore ( path1 : & PathBuf , path2 : & PathBuf , output_path : Option < & PathBuf > , cfg : & Config ) {
64
+ WalkDir :: new ( path1)
71
65
. into_iter ( )
72
66
. filter_entry ( |e| !is_hidden ( e) )
73
67
. zip (
74
- WalkDir :: new ( & path2)
68
+ WalkDir :: new ( path2)
75
69
. into_iter ( )
76
70
. filter_entry ( |e| !is_hidden ( e) ) ,
77
71
)
@@ -86,15 +80,12 @@ fn explore(
86
80
&& path1_file. extension ( ) . unwrap ( ) == "json"
87
81
&& path2_file. extension ( ) . unwrap ( ) == "json"
88
82
{
89
- act_on_file ( & path1_file, & path2_file, & output_path, & cfg) . unwrap ( ) ;
83
+ act_on_file ( & path1_file, & path2_file, output_path, cfg) . unwrap ( ) ;
90
84
}
91
85
} ) ;
92
-
93
- Ok ( ( ) )
94
86
}
95
87
96
- #[ inline( always) ]
97
- fn exist_or_exit ( path : & PathBuf , which_path : & str ) {
88
+ fn exist_or_exit ( path : & Path , which_path : & str ) {
98
89
if !( path. exists ( ) ) {
99
90
eprintln ! (
100
91
"The {} path `{}` is not correct" ,
@@ -178,11 +169,11 @@ fn main() {
178
169
} ;
179
170
180
171
if path1. is_dir ( ) && path2. is_dir ( ) {
181
- explore ( & path1, & path2, & output_path, & cfg) . unwrap ( ) ;
172
+ explore ( & path1, & path2, output_path. as_ref ( ) , & cfg) ;
182
173
} else if ( path1. is_dir ( ) && !path2. is_dir ( ) ) || ( !path1. is_dir ( ) && path2. is_dir ( ) ) {
183
174
eprintln ! ( "Both paths should be a directory or a file" , ) ;
184
175
process:: exit ( 1 ) ;
185
176
} else {
186
- act_on_file ( & path1, & path2, & output_path, & cfg) . unwrap ( ) ;
177
+ act_on_file ( & path1, & path2, output_path. as_ref ( ) , & cfg) . unwrap ( ) ;
187
178
}
188
179
}
0 commit comments