1- use std:: fs;
2- use std:: io:: { self , Write } ;
3- use std:: path:: PathBuf ;
4-
5- use colored:: * ;
6-
71mod cmd_line;
82mod module;
3+ mod write;
94
105use module:: { Module , ModuleType } ;
116
@@ -52,7 +47,7 @@ fn main() {
5247
5348 // Write all non-populated modules to the disk
5449 for module in non_populated. iter ( ) {
55- write_module ( & module, & options) ;
50+ module. write_file ( & options) ;
5651 }
5752
5853 // Treat the populated assembly module as a special case:
@@ -73,7 +68,7 @@ fn main() {
7368 let populated =
7469 Module :: new ( ModuleType :: Assembly , title, & options) . includes ( include_statements) ;
7570
76- write_module ( & populated, & options) ;
71+ populated. write_file ( & options) ;
7772 }
7873}
7974
@@ -105,56 +100,3 @@ fn process_module_type(
105100
106101 modules_from_type
107102}
108-
109- /// Write the generated module content to the path specified in `options` with the set file name.
110- // fn write_module(file_name: &str, content: &str, options: &Options) {
111- fn write_module ( module : & Module , options : & Options ) {
112- // Compose the full (but still relative) file path from the target directory and the file name
113- let full_path_buf: PathBuf = [ & options. target_dir , & module. file_name ] . iter ( ) . collect ( ) ;
114- let full_path = full_path_buf. as_path ( ) ;
115-
116- // If the target file already exists, just print out an error
117- if full_path. exists ( ) {
118- // A prompt enabling the user to overwrite the existing file
119- eprintln ! (
120- "{}" ,
121- format!( "W: File already exists: {}" , full_path. display( ) ) . yellow( )
122- ) ;
123- eprint ! ( " Do you want to overwrite it? [y/N] " ) ;
124- // We must manually flush the buffer or else the printed string doesn't appear.
125- // The buffer otherwise waits for a newline.
126- io:: stdout ( ) . flush ( ) . unwrap ( ) ;
127-
128- let mut answer = String :: new ( ) ;
129-
130- io:: stdin ( )
131- . read_line ( & mut answer)
132- . expect ( "Failed to read the response" ) ;
133-
134- match answer. trim ( ) . to_lowercase ( ) . as_str ( ) {
135- "y" | "yes" => {
136- eprintln ! ( " → Rewriting the file." ) ;
137- }
138- _ => {
139- eprintln ! ( " → Preserving the existing file." ) ;
140- // Break from generating this particular module.
141- // Other modules that might be in the queue will be generated on next iteration.
142- return ;
143- }
144- } ;
145- }
146-
147- // If the target file doesn't exist, try to write to it
148- let result = fs:: write ( full_path, & module. compose_text ( & options) ) ;
149- match result {
150- // If the write succeeds, print the include statement
151- Ok ( ( ) ) => {
152- eprintln ! ( "‣ File generated: {}" , full_path. display( ) ) ;
153- eprintln ! ( " {}" , module. include_statement) ;
154- }
155- // If the write fails, print why it failed
156- Err ( e) => {
157- eprintln ! ( "{}" , format!( "E: Failed to write the file: {}" , e) . red( ) ) ;
158- }
159- }
160- }
0 commit comments