Skip to content

Commit 94cfe40

Browse files
committed
Move file writing into a separate file
1 parent 1c1a840 commit 94cfe40

File tree

2 files changed

+65
-61
lines changed

2 files changed

+65
-61
lines changed

src/main.rs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
use std::fs;
2-
use std::io::{self, Write};
3-
use std::path::PathBuf;
4-
5-
use colored::*;
6-
71
mod cmd_line;
82
mod module;
3+
mod write;
94

105
use 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-
}

src/write.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::fs;
2+
use std::io::{self, Write};
3+
use std::path::PathBuf;
4+
5+
use colored::*;
6+
7+
use crate::Options;
8+
use crate::module::Module;
9+
10+
impl Module {
11+
/// Write the generated module content to the path specified in `options` with the set file name.
12+
pub fn write_file(&self, options: &Options) {
13+
// Compose the full (but still relative) file path from the target directory and the file name
14+
let full_path_buf: PathBuf = [&options.target_dir, &self.file_name].iter().collect();
15+
let full_path = full_path_buf.as_path();
16+
17+
// If the target file already exists, just print out an error
18+
if full_path.exists() {
19+
// A prompt enabling the user to overwrite the existing file
20+
eprintln!(
21+
"{}",
22+
format!("W: File already exists: {}", full_path.display()).yellow()
23+
);
24+
eprint!(" Do you want to overwrite it? [y/N] ");
25+
// We must manually flush the buffer or else the printed string doesn't appear.
26+
// The buffer otherwise waits for a newline.
27+
io::stdout().flush().unwrap();
28+
29+
let mut answer = String::new();
30+
31+
io::stdin()
32+
.read_line(&mut answer)
33+
.expect("Failed to read the response");
34+
35+
match answer.trim().to_lowercase().as_str() {
36+
"y" | "yes" => {
37+
eprintln!(" → Rewriting the file.");
38+
}
39+
_ => {
40+
eprintln!(" → Preserving the existing file.");
41+
// Break from generating this particular module.
42+
// Other modules that might be in the queue will be generated on next iteration.
43+
return;
44+
}
45+
};
46+
}
47+
48+
// If the target file doesn't exist, try to write to it
49+
let result = fs::write(full_path, &self.compose_text(&options));
50+
match result {
51+
// If the write succeeds, print the include statement
52+
Ok(()) => {
53+
eprintln!("‣ File generated: {}", full_path.display());
54+
eprintln!(" {}", self.include_statement);
55+
}
56+
// If the write fails, print why it failed
57+
Err(e) => {
58+
eprintln!("{}", format!("E: Failed to write the file: {}", e).red());
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)