Better error handling

This commit is contained in:
Jonas Zeunert
2024-04-19 21:28:25 +02:00
parent 9234a4e2d0
commit 18ff742f25

View File

@@ -42,17 +42,17 @@ fn render(input: &str) -> String {
skin.term_text(input).to_string() skin.term_text(input).to_string()
} }
async fn process_file(file: DirEntry, output_dir: PathBuf) { async fn process_file(file: DirEntry, output_dir: PathBuf) -> Result<(), std::io::Error> {
let markdown: &str = &std::fs::read_to_string(file.path()).unwrap(); let markdown: &str = &std::fs::read_to_string(file.path()).unwrap();
let html_stripped = &remove_html(markdown); let html_stripped = &remove_html(markdown);
let links_replaced = &replace_links(html_stripped); let links_replaced = &replace_links(html_stripped);
let rendered = render(links_replaced); let rendered = render(links_replaced);
write_file(output_dir, file, &rendered); write_file(output_dir, file, &rendered)
} }
fn write_file(output_dir: PathBuf, file: DirEntry, rendered: &str) { fn write_file(output_dir: PathBuf, file: DirEntry, rendered: &str) -> Result<(), std::io::Error> {
let basename = file let basename = file
.path() .path()
.parent() .parent()
@@ -65,9 +65,15 @@ fn write_file(output_dir: PathBuf, file: DirEntry, rendered: &str) {
.to_string(); .to_string();
let name = basename.replace("awesome", "").replace("-", ""); let name = basename.replace("awesome", "").replace("-", "");
let path = output_dir.clone().join(name); let mut path = output_dir.clone().join(name.clone());
fs::write(path, rendered).expect("Error writing file!"); let mut i = 2;
while path.exists() {
path.set_file_name(name.clone() + &i.to_string());
i = i + 1;
}
fs::write(path, rendered)
} }
#[tokio::main] #[tokio::main]