@@ -71,18 +71,40 @@ impl FileIO {
71
71
}
72
72
73
73
/// Deletes file.
74
+ ///
75
+ /// # Arguments
76
+ ///
77
+ /// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
74
78
pub async fn delete ( & self , path : impl AsRef < str > ) -> Result < ( ) > {
75
79
let ( op, relative_path) = self . inner . create_operator ( & path) ?;
76
80
Ok ( op. delete ( relative_path) . await ?)
77
81
}
78
82
83
+ /// Remove the path and all nested dirs and files recursively.
84
+ ///
85
+ /// # Arguments
86
+ ///
87
+ /// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
88
+ pub async fn remove_all ( & self , path : impl AsRef < str > ) -> Result < ( ) > {
89
+ let ( op, relative_path) = self . inner . create_operator ( & path) ?;
90
+ Ok ( op. remove_all ( relative_path) . await ?)
91
+ }
92
+
79
93
/// Check file exists.
94
+ ///
95
+ /// # Arguments
96
+ ///
97
+ /// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
80
98
pub async fn is_exist ( & self , path : impl AsRef < str > ) -> Result < bool > {
81
99
let ( op, relative_path) = self . inner . create_operator ( & path) ?;
82
100
Ok ( op. is_exist ( relative_path) . await ?)
83
101
}
84
102
85
103
/// Creates input file.
104
+ ///
105
+ /// # Arguments
106
+ ///
107
+ /// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
86
108
pub fn new_input ( & self , path : impl AsRef < str > ) -> Result < InputFile > {
87
109
let ( op, relative_path) = self . inner . create_operator ( & path) ?;
88
110
let path = path. as_ref ( ) . to_string ( ) ;
@@ -95,6 +117,10 @@ impl FileIO {
95
117
}
96
118
97
119
/// Creates output file.
120
+ ///
121
+ /// # Arguments
122
+ ///
123
+ /// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
98
124
pub fn new_output ( & self , path : impl AsRef < str > ) -> Result < OutputFile > {
99
125
let ( op, relative_path) = self . inner . create_operator ( & path) ?;
100
126
let path = path. as_ref ( ) . to_string ( ) ;
@@ -338,7 +364,7 @@ impl OutputFile {
338
364
339
365
#[ cfg( test) ]
340
366
mod tests {
341
- use std:: fs:: File ;
367
+ use std:: fs:: { create_dir_all , File } ;
342
368
use std:: io:: Write ;
343
369
use std:: path:: Path ;
344
370
@@ -353,6 +379,7 @@ mod tests {
353
379
}
354
380
355
381
fn write_to_file < P : AsRef < Path > > ( s : & str , path : P ) {
382
+ create_dir_all ( path. as_ref ( ) . parent ( ) . unwrap ( ) ) . unwrap ( ) ;
356
383
let mut f = File :: create ( path) . unwrap ( ) ;
357
384
write ! ( f, "{s}" ) . unwrap ( ) ;
358
385
}
@@ -389,16 +416,24 @@ mod tests {
389
416
async fn test_delete_local_file ( ) {
390
417
let tmp_dir = TempDir :: new ( ) . unwrap ( ) ;
391
418
392
- let file_name = "a.txt" ;
393
- let content = "Iceberg loves rust." ;
394
-
395
- let full_path = format ! ( "{}/{}" , tmp_dir. path( ) . to_str( ) . unwrap( ) , file_name) ;
396
- write_to_file ( content, & full_path) ;
419
+ let a_path = format ! ( "{}/{}" , tmp_dir. path( ) . to_str( ) . unwrap( ) , "a.txt" ) ;
420
+ let sub_dir_path = format ! ( "{}/sub" , tmp_dir. path( ) . to_str( ) . unwrap( ) ) ;
421
+ let b_path = format ! ( "{}/{}" , sub_dir_path, "b.txt" ) ;
422
+ let c_path = format ! ( "{}/{}" , sub_dir_path, "c.txt" ) ;
423
+ write_to_file ( "Iceberg loves rust." , & a_path) ;
424
+ write_to_file ( "Iceberg loves rust." , & b_path) ;
425
+ write_to_file ( "Iceberg loves rust." , & c_path) ;
397
426
398
427
let file_io = create_local_file_io ( ) ;
399
- assert ! ( file_io. is_exist( & full_path) . await . unwrap( ) ) ;
400
- file_io. delete ( & full_path) . await . unwrap ( ) ;
401
- assert ! ( !file_io. is_exist( & full_path) . await . unwrap( ) ) ;
428
+ assert ! ( file_io. is_exist( & a_path) . await . unwrap( ) ) ;
429
+
430
+ file_io. remove_all ( & sub_dir_path) . await . unwrap ( ) ;
431
+ assert ! ( !file_io. is_exist( & b_path) . await . unwrap( ) ) ;
432
+ assert ! ( !file_io. is_exist( & c_path) . await . unwrap( ) ) ;
433
+ assert ! ( file_io. is_exist( & a_path) . await . unwrap( ) ) ;
434
+
435
+ file_io. delete ( & a_path) . await . unwrap ( ) ;
436
+ assert ! ( !file_io. is_exist( & a_path) . await . unwrap( ) ) ;
402
437
}
403
438
404
439
#[ tokio:: test]
@@ -411,6 +446,7 @@ mod tests {
411
446
let file_io = create_local_file_io ( ) ;
412
447
assert ! ( !file_io. is_exist( & full_path) . await . unwrap( ) ) ;
413
448
assert ! ( file_io. delete( & full_path) . await . is_ok( ) ) ;
449
+ assert ! ( file_io. remove_all( & full_path) . await . is_ok( ) ) ;
414
450
}
415
451
416
452
#[ tokio:: test]
0 commit comments