1
- use log:: { debug, info} ;
2
- use std:: fs:: metadata;
1
+ use log:: info;
3
2
use std:: {
4
3
fs:: { self , File , OpenOptions } ,
5
4
io:: { self , Write } ,
@@ -8,63 +7,43 @@ use std::{
8
7
9
8
/// Creates a file if it does not already exist.
10
9
/// If the file already exists, it doesn't modify its content.
11
- pub fn create_file ( path : & Path , content : & str ) -> io:: Result < ( ) > {
10
+ pub fn create_file ( path : & Path , content : Option < & str > ) -> io:: Result < ( ) > {
12
11
match OpenOptions :: new ( ) . write ( true ) . create_new ( true ) . open ( path) {
13
- Ok ( mut file) => {
14
- info ! ( "File '{}' created." , path. to_string_lossy( ) ) ;
15
- file. write_all ( content. as_bytes ( ) ) ?;
16
- Ok ( ( ) )
17
- }
12
+ Ok ( mut file) => content. map_or_else (
13
+ || {
14
+ info ! ( "Created an empty file: '{}'" , path. display( ) ) ;
15
+ Ok ( ( ) )
16
+ } ,
17
+ |s| {
18
+ file. write_all ( s. as_bytes ( ) )
19
+ . inspect ( |_| info ! ( "File '{}' created." , path. display( ) ) )
20
+ } ,
21
+ ) ,
18
22
Err ( e) if e. kind ( ) == io:: ErrorKind :: AlreadyExists => {
19
- info ! (
20
- "File '{}' already exists. Not altering it." ,
21
- path. to_string_lossy( )
22
- ) ;
23
- Ok ( ( ) )
24
- }
25
- Err ( e) => Err ( e) ,
26
- }
27
- }
28
- ///Create_file with no content...
29
- pub fn create_file_nn ( path : & Path ) -> io:: Result < ( ) > {
30
- // Verify if the file does not already exist.
31
- if metadata ( path) . is_ok ( ) {
32
- info ! (
33
- "File '{}' already exists. Not altering it." ,
34
- path. to_string_lossy( )
35
- ) ;
36
- return Ok ( ( ) ) ;
37
- }
38
-
39
- // Create the file.
40
- match std:: fs:: File :: create ( path) {
41
- Ok ( _) => {
42
- info ! ( "File '{}' created." , path. to_string_lossy( ) ) ;
23
+ info ! ( "File '{}' already exists. Not altering it." , path. display( ) ) ;
43
24
Ok ( ( ) )
44
25
}
45
26
Err ( e) => Err ( e) ,
46
27
}
47
28
}
48
29
49
- /// Creates a file given its path.
30
+ /// Creates a directory given its path.
50
31
pub fn create_dir ( path : & Path ) -> io:: Result < ( ) > {
51
32
if path. exists ( ) {
52
33
info ! (
53
34
"Directory '{}' already exists. Not altering it." ,
54
- path. to_string_lossy ( )
35
+ path. display ( )
55
36
) ;
56
37
return Ok ( ( ) ) ;
57
38
}
58
- fs:: create_dir ( path) ?;
59
- info ! ( "Created directory: '{}'" , path. to_string_lossy( ) ) ;
60
- Ok ( ( ) )
39
+ fs:: create_dir ( path) . inspect ( |_| info ! ( "Created directory: '{}'" , path. display( ) ) )
61
40
}
62
41
63
42
/// Opens an already existing file and overwrites all content with `content`.
64
43
pub fn overwrite_file ( path : & Path , content : & str ) -> std:: io:: Result < ( ) > {
65
44
let mut file = File :: create ( path) ?;
66
- debug ! ( "Overwrote file '{}'" , path. to_string_lossy( ) ) ;
67
45
file. write_all ( content. as_bytes ( ) )
46
+ . inspect ( |_| info ! ( "Overwrote file: '{}'" , path. display( ) ) )
68
47
}
69
48
70
49
#[ cfg( test) ]
@@ -107,18 +86,18 @@ mod tests {
107
86
// Test 1: Create a new file
108
87
let file_path = temp_dir. path ( ) . join ( "test1.txt" ) ;
109
88
let content = "Hello, World!" ;
110
- create_file ( & file_path, content) ?;
89
+ create_file ( & file_path, Some ( content) ) ?;
111
90
assert ! ( file_path. exists( ) ) ;
112
91
assert_eq ! ( fs:: read_to_string( & file_path) ?, content) ;
113
92
114
93
// Test 2: Attempt to create an existing file (should not modify)
115
94
let existing_content = fs:: read_to_string ( & file_path) ?;
116
- create_file ( & file_path, "New content" ) ?;
95
+ create_file ( & file_path, Some ( "New content" ) ) ?;
117
96
assert_eq ! ( fs:: read_to_string( & file_path) ?, existing_content) ;
118
97
119
98
// Test 3: Create file with empty content
120
99
let empty_file_path = temp_dir. path ( ) . join ( "empty.txt" ) ;
121
- create_file ( & empty_file_path, "" ) ?;
100
+ create_file ( & empty_file_path, Some ( "" ) ) ?;
122
101
assert ! ( empty_file_path. exists( ) ) ;
123
102
assert_eq ! ( fs:: read_to_string( & empty_file_path) ?, "" ) ;
124
103
0 commit comments