diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-04-05 14:43:19 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-04-05 14:43:19 +0200 |
| commit | 93c0e8ecee430883c62120f9f0b6999acfac4435 (patch) | |
| tree | 785b9275d9dd525889bfb8cb687a41260ae39b16 /src/file_handler/file_strategies | |
| parent | bb3ec6b31f3c709fbf9f2a12c1708d63a61892ff (diff) | |
Update, panic less and propagate errors
Diffstat (limited to 'src/file_handler/file_strategies')
| -rw-r--r-- | src/file_handler/file_strategies/file.rs | 32 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/gemini.rs | 117 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/layout.rs | 29 |
3 files changed, 131 insertions, 47 deletions
diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index 363f712..c31eae4 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -1,17 +1,31 @@ pub struct Strategy {} use std::fs::{copy, create_dir_all}; +use std::io::{Error, ErrorKind, Result}; use std::path::Path; use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy}; impl Strategy { - fn handle(source: &Path, destination: &Path, file: &File) { - let relative_path = file.path.strip_prefix(source).unwrap(); + fn handle(source: &Path, destination: &Path, file: &File) -> Result<()> { + let relative_path = file.path.strip_prefix(source).map_err(|_| { + Error::new( + ErrorKind::InvalidData, + "Path was not part of source directory.", + ) + })?; let complete_destination = destination.join(relative_path); - let destination_parent = complete_destination.parent().unwrap(); - create_dir_all(destination_parent).unwrap(); - copy(&file.path, &complete_destination).unwrap(); + match complete_destination.parent() { + Some(destination_parent) => { + create_dir_all(destination_parent)?; + copy(&file.path, &complete_destination)?; + Ok(()) + } + None => Err(Error::new( + ErrorKind::InvalidData, + "Destination parent was not readable.", + )), + } } } @@ -28,12 +42,12 @@ impl FileHandlerStrategy for Strategy { matches!(file_type, FileType::File) } - fn handle_html(&self, source: &Path, destination: &Path, file: &File, _l: &str) { - Strategy::handle(source, destination, file); + fn handle_html(&self, source: &Path, destination: &Path, file: &File, _l: &str) -> Result<()> { + Strategy::handle(source, destination, file) } - fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) { - Strategy::handle(source, destination, file); + fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) -> Result<()> { + Strategy::handle(source, destination, file) } } diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index 5388769..0c7efbb 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -1,7 +1,7 @@ pub struct Strategy {} use std::fs::{File as IOFile, create_dir_all, read_to_string}; -use std::io::Write; +use std::io::{Error, ErrorKind, Result, Write}; use std::path::Path; use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy}; @@ -17,11 +17,17 @@ impl Strategy { } fn get_title(line: &str) -> &str { - line.split_once("--- title:").unwrap().1 + match line.split_once("--- title:") { + Some(title_line) => title_line.1, + None => "", + } } fn get_description(line: &str) -> &str { - line.split_once("--- description:").unwrap().1 + match line.split_once("--- description:") { + Some(description_line) => description_line.1, + None => "", + } } } @@ -41,8 +47,14 @@ impl FileHandlerStrategy for Strategy { matches!(file_type, FileType::Gemini) } - fn handle_html(&self, source: &Path, destination: &Path, file: &File, layout: &str) { - let gemini_contents = read_to_string(&file.path).unwrap(); + fn handle_html( + &self, + source: &Path, + destination: &Path, + file: &File, + layout: &str, + ) -> Result<()> { + let gemini_contents = read_to_string(&file.path)?; // Front matter extraction let lines: Vec<&str> = gemini_contents.split('\n').collect(); @@ -59,12 +71,12 @@ impl FileHandlerStrategy for Strategy { if Strategy::is_description(line) { description = Strategy::get_description(line).trim(); lines_found += 1; - continue; } } } - let gemini_source = lines[lines_found..].join("\n"); + let gemini_lines = lines.get(lines_found..).unwrap_or(&[]); + let gemini_source = gemini_lines.join("\n"); let content_html = render_html(&parse(&gemini_source[..])); let generated_html = layout @@ -72,20 +84,30 @@ impl FileHandlerStrategy for Strategy { .replace("{{ description }}", description) .replace("{{ content }}", &content_html[..]); - let relative_path = file.path.strip_prefix(source).unwrap(); + let relative_path = file.path.strip_prefix(source).map_err(|_| { + Error::new( + ErrorKind::InvalidData, + "Path was not part of source directory.", + ) + })?; let mut complete_destination = destination.join(relative_path); complete_destination.set_extension("html"); - let destination_parent = complete_destination.parent().unwrap(); - create_dir_all(destination_parent).unwrap(); - - let mut destination_file = IOFile::create(&complete_destination).unwrap(); - destination_file - .write_all(generated_html.as_bytes()) - .unwrap(); + match complete_destination.parent() { + Some(destination_parent) => { + create_dir_all(destination_parent)?; + let mut destination_file = IOFile::create(&complete_destination)?; + destination_file.write_all(generated_html.as_bytes())?; + Ok(()) + } + None => Err(Error::new( + ErrorKind::InvalidData, + "Destination parent was not readable.", + )), + } } - fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) { - let gemini_contents = read_to_string(&file.path).unwrap(); + fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) -> Result<()> { + let gemini_contents = read_to_string(&file.path)?; // Front matter extraction let lines: Vec<&str> = gemini_contents.split('\n').collect(); @@ -98,22 +120,32 @@ impl FileHandlerStrategy for Strategy { } if Strategy::is_description(line) { lines_found += 1; - continue; } } } - let gemini_source = lines[lines_found..].join("\n"); + let gemini_lines = lines.get(lines_found..).unwrap_or(&[]); + let gemini_source = gemini_lines.join("\n"); - let relative_path = file.path.strip_prefix(source).unwrap(); + let relative_path = file.path.strip_prefix(source).map_err(|_| { + Error::new( + ErrorKind::InvalidData, + "Path was not part of source directory.", + ) + })?; let complete_destination = destination.join(relative_path); - let destination_parent = complete_destination.parent().unwrap(); - create_dir_all(destination_parent).unwrap(); - - let mut destination_file = IOFile::create(&complete_destination).unwrap(); - destination_file - .write_all(gemini_source.as_bytes()) - .unwrap(); + match complete_destination.parent() { + Some(destination_parent) => { + create_dir_all(destination_parent)?; + let mut destination_file = IOFile::create(&complete_destination)?; + destination_file.write_all(gemini_source.as_bytes())?; + Ok(()) + } + None => Err(Error::new( + ErrorKind::InvalidData, + "Destination parent was not readable.", + )), + } } } @@ -151,6 +183,11 @@ mod tests { } #[test] + fn returns_empty_string_on_bad_title_extraction() { + assert_eq!(Strategy::get_title("--- mitle: Hello!").trim(), ""); + } + + #[test] fn extracts_description() { assert_eq!( Strategy::get_description("--- description: What is this?").trim(), @@ -159,6 +196,14 @@ mod tests { } #[test] + fn returns_empty_string_on_bad_description_extraction() { + assert_eq!( + Strategy::get_description("--- mescription: NOOO!").trim(), + "" + ); + } + + #[test] fn identifies_gemini_file() { let test_dir = setup_test_dir(); create_test_file(&test_dir.join("test.gmi"), ""); @@ -229,7 +274,11 @@ Hello world file_type: FileType::Gemini, }; - strategy.handle_html(&source_dir, &output_dir, &file, layout); + assert!( + strategy + .handle_html(&source_dir, &output_dir, &file, layout) + .is_ok() + ); let html_output = output_dir.join("test.html"); assert!(html_output.exists()); @@ -274,7 +323,11 @@ Hello world file_type: FileType::Gemini, }; - strategy.handle_gemini(&source_dir, &output_dir, &file); + assert!( + strategy + .handle_gemini(&source_dir, &output_dir, &file) + .is_ok() + ); let gemini_output = output_dir.join("test.gmi"); assert!(gemini_output.exists()); @@ -319,7 +372,11 @@ Hello world file_type: FileType::Gemini, }; - strategy.handle_html(&source_dir, &output_dir, &file, layout); + assert!( + strategy + .handle_html(&source_dir, &output_dir, &file, layout) + .is_ok() + ); let html_output = output_dir.join("nested/test.html"); assert!(html_output.exists()); diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index 9a60f12..45ef665 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -1,5 +1,6 @@ pub struct Strategy {} +use std::io::Result; use std::path::Path; use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy}; @@ -19,8 +20,12 @@ impl FileHandlerStrategy for Strategy { // We don't implement handling for layout, as we assume there's only one // and it got handled before. - fn handle_html(&self, _s: &Path, _d: &Path, _f: &File, _l: &str) {} - fn handle_gemini(&self, _s: &Path, _d: &Path, _f: &File) {} + fn handle_html(&self, _s: &Path, _d: &Path, _f: &File, _l: &str) -> Result<()> { + Ok(()) + } + fn handle_gemini(&self, _s: &Path, _d: &Path, _f: &File) -> Result<()> { + Ok(()) + } } #[cfg(test)] @@ -101,11 +106,15 @@ mod tests { file_type: FileType::Layout, }; - strategy.handle_html( - &PathBuf::from("source"), - &PathBuf::from("dest"), - &file, - "layout content", + assert!( + strategy + .handle_html( + &PathBuf::from("source"), + &PathBuf::from("dest"), + &file, + "layout content", + ) + .is_ok() ); } @@ -118,6 +127,10 @@ mod tests { }; // Should not panic - strategy.handle_gemini(&PathBuf::from("source"), &PathBuf::from("dest"), &file); + assert!( + strategy + .handle_gemini(&PathBuf::from("source"), &PathBuf::from("dest"), &file) + .is_ok() + ); } } |