1
1
use std:: env;
2
2
use std:: fs;
3
3
use std:: error:: Error ;
4
- use std:: io;
5
4
use std:: process;
6
5
use std:: path:: Path ;
7
6
@@ -218,51 +217,6 @@ fn handle_file(path:&Path) -> Result<(), Box<dyn Error>> {
218
217
Ok ( ( ) )
219
218
}
220
219
221
- fn handle_dir ( path : & Path ) -> io:: Result < ( ) > {
222
- // https://users.rust-lang.org/t/testable-way-to-iterate-over-a-directory/81440
223
- let mut dirs = Vec :: new ( ) ;
224
- let mut dir_index = 0 ;
225
- let mut dir_reader = fs:: read_dir ( path) ?;
226
- let mut had_files = false ;
227
- loop {
228
- match dir_reader. next ( ) {
229
- Some ( entry) => {
230
- let cur_path = entry?. path ( ) ;
231
- had_files = true ;
232
- if cur_path. is_dir ( ) {
233
- dirs. push ( cur_path) ;
234
- continue ;
235
- }
236
-
237
- if cur_path. is_file ( ) {
238
- if let Err ( err) = handle_file ( & cur_path) {
239
- println ! ( "error processing {}: {}" , cur_path. display( ) , err) ;
240
- process:: exit ( 1 ) ;
241
- }
242
- continue ;
243
- }
244
- }
245
- _ => {
246
- if !had_files && !dirs. is_empty ( ) {
247
- let target = dirs[ ( dir_index - 1 ) . max ( 0 ) ] . to_owned ( ) ;
248
- if let Err ( err) = handle_file ( & target) {
249
- println ! ( "error processing {}: {}" , target. display( ) , err) ;
250
- process:: exit ( 1 ) ;
251
- }
252
- }
253
- if dir_index == dirs. len ( ) {
254
- break ;
255
- }
256
- dir_reader = dirs[ dir_index] . read_dir ( ) ?;
257
- had_files = false ;
258
- dir_index += 1 ;
259
- }
260
- }
261
- }
262
-
263
- Ok ( ( ) )
264
- }
265
-
266
220
fn main ( ) {
267
221
let mut args = env:: args ( ) ;
268
222
let _ = args. next ( ) ; // executable name
@@ -274,16 +228,10 @@ fn main() {
274
228
}
275
229
276
230
for arg in args {
277
- let path = Path :: new ( & arg) ;
278
- if path. is_file ( ) {
279
- if let Err ( err) = handle_file ( & path) {
280
- eprintln ! ( "error processing {}: {}" , arg, err) ;
281
- process:: exit ( 1 ) ;
282
- }
283
- } else if path. is_dir ( ) {
284
- handle_dir ( & path) . unwrap ( ) ;
285
- } else {
286
- eprintln ! ( "could not open {}" , arg) ;
231
+ let path = Path :: new ( & arg) ;
232
+ if let Err ( err) = handle_file ( & path) {
233
+ eprintln ! ( "error processing {}: {}" , arg, err) ;
234
+ process:: exit ( 1 ) ;
287
235
}
288
236
}
289
237
}
0 commit comments