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/mod.rs | |
| parent | bb3ec6b31f3c709fbf9f2a12c1708d63a61892ff (diff) | |
Update, panic less and propagate errors
Diffstat (limited to 'src/file_handler/mod.rs')
| -rw-r--r-- | src/file_handler/mod.rs | 208 |
1 files changed, 166 insertions, 42 deletions
diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index 2bdbb74..f84d0d9 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -5,6 +5,7 @@ use file_strategies::gemini::Strategy as GeminiStrategy; use file_strategies::layout::Strategy as LayoutStrategy; use std::fs::read_to_string; +use std::io::{Error, ErrorKind, Result}; use std::path::{Path, PathBuf}; pub struct FileHandler { @@ -35,15 +36,18 @@ impl FileHandler { FileType::Unknown } - pub fn get_layout_or_panic(&mut self, files: &[File]) -> Result<(), &str> { + pub fn get_layout(&mut self, files: &[File]) -> Result<()> { for file in files { if file.file_type == FileType::Layout { - let layout_text = read_to_string(&file.path).unwrap(); + let layout_text = read_to_string(&file.path)?; self.layout = Some(layout_text); return Ok(()); } } - Err("No layout found. Please ensure there's a _layout.html file at the root") + Err(Error::new( + ErrorKind::NotFound, + "No layout found. Please ensure there's a _layout.html file at the root", + )) } pub fn handle_all( @@ -52,10 +56,11 @@ impl FileHandler { html_destination: &Path, gemini_destination: &Path, files: &[File], - ) { + ) -> Result<()> { for file in files { - self.handle(source, html_destination, gemini_destination, file); + self.handle(source, html_destination, gemini_destination, file)?; } + Ok(()) } pub fn handle( @@ -64,18 +69,26 @@ impl FileHandler { html_destination: &Path, gemini_destination: &Path, file: &File, - ) { - if let Some(strategy) = self + ) -> Result<()> { + match self .strategies .iter() .find(|s| s.can_handle(&file.file_type)) { - let layout = self - .layout - .as_ref() - .expect("Layout should be initialized before handling files"); - strategy.handle_html(source, html_destination, file, layout); - strategy.handle_gemini(source, gemini_destination, file); + Some(strategy) => { + let layout = self.layout.as_ref().ok_or_else(|| { + Error::new( + ErrorKind::NotFound, + "Layout should be initialized before handling files", + ) + })?; + strategy.handle_html(source, html_destination, file, layout)?; + strategy.handle_gemini(source, gemini_destination, file)?; + Ok(()) + } + + // We silently ignore files we can't process. + None => Ok(()), } } } @@ -84,8 +97,14 @@ pub trait Strategy { fn is(&self, path: &Path) -> bool; fn identify(&self) -> FileType; fn can_handle(&self, file_type: &FileType) -> bool; - fn handle_html(&self, source: &Path, destination: &Path, file: &File, layout: &str); - fn handle_gemini(&self, source: &Path, destination: &Path, file: &File); + fn handle_html( + &self, + source: &Path, + destination: &Path, + file: &File, + layout: &str, + ) -> Result<()>; + fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) -> Result<()>; } #[derive(Debug, Clone, PartialEq)] @@ -162,7 +181,7 @@ mod tests { create_test_internal_file("regular.html", FileType::File), ]; - assert!(handler.get_layout_or_panic(&files).is_ok()); + assert!(handler.get_layout(&files).is_ok()); } #[test] @@ -173,7 +192,7 @@ mod tests { create_test_internal_file("regular.html", FileType::File), ]; - assert!(handler.get_layout_or_panic(&files).is_err()); + assert!(handler.get_layout(&files).is_err()); } // Mock strategy for testing @@ -195,8 +214,57 @@ mod tests { &self.file_type == file_type } - fn handle_html(&self, _source: &Path, _destination: &Path, _file: &File, _layout: &str) {} - fn handle_gemini(&self, _source: &Path, _destination: &Path, _file: &File) {} + fn handle_html( + &self, + _source: &Path, + _destination: &Path, + _file: &File, + _layout: &str, + ) -> Result<()> { + Ok(()) + } + fn handle_gemini(&self, _source: &Path, _destination: &Path, _file: &File) -> Result<()> { + Ok(()) + } + } + + // Mock strategy for testing + struct ErroringMockStrategy { + is_match: bool, + file_type: FileType, + } + + impl Strategy for ErroringMockStrategy { + fn is(&self, _path: &Path) -> bool { + self.is_match + } + + fn identify(&self) -> FileType { + self.file_type.clone() + } + + fn can_handle(&self, file_type: &FileType) -> bool { + &self.file_type == file_type + } + + fn handle_html( + &self, + _source: &Path, + _destination: &Path, + _file: &File, + _layout: &str, + ) -> Result<()> { + Err(Error::new( + ErrorKind::Other, + "Forced to fail by my cruel author.", + )) + } + fn handle_gemini(&self, _source: &Path, _destination: &Path, _file: &File) -> Result<()> { + Err(Error::new( + ErrorKind::Other, + "Forced to fail by my benevolent author.", + )) + } } #[test] @@ -217,7 +285,48 @@ mod tests { file_type: FileType::Gemini, }; assert!(matches!(handler.identify(&path), FileType::Gemini)); - handler.handle(&path, &path, &path, &file); + assert!(handler.handle(&path, &path, &path, &file).is_ok()); + } + + #[test] + fn test_failure_propagates_in_handler() { + let mock_strategy = ErroringMockStrategy { + is_match: true, + file_type: FileType::Gemini, + }; + + let handler = FileHandler { + strategies: vec![Box::new(mock_strategy)], + layout: Some("None".to_string()), + }; + + let path = PathBuf::from("test.whatever"); + let file = File { + path: path.clone(), + file_type: FileType::Gemini, + }; + assert!(matches!(handler.identify(&path), FileType::Gemini)); + assert!(handler.handle(&path, &path, &path, &file).is_err()); + } + + #[test] + fn silently_ignore_unhandleable_files() { + let mock_strategy = MockStrategy { + is_match: false, + file_type: FileType::Gemini, + }; + + let handler = FileHandler { + strategies: vec![Box::new(mock_strategy)], + layout: Some("None".to_string()), + }; + + let path = PathBuf::from("test.whatever"); + let file = File { + path: path.clone(), + file_type: FileType::Layout, + }; + assert!(handler.handle(&path, &path, &path, &file).is_ok()); } #[test] @@ -226,11 +335,15 @@ mod tests { let files: Vec<File> = vec![]; // Should not panic with empty vector - handler.handle_all( - &PathBuf::from("source"), - &PathBuf::from("output_html"), - &PathBuf::from("output_gemini"), - &files, + assert!( + handler + .handle_all( + &PathBuf::from("source"), + &PathBuf::from("output_html"), + &PathBuf::from("output_gemini"), + &files, + ) + .is_ok() ); } @@ -256,25 +369,32 @@ mod tests { ); // Should not panic with valid layout - handler.handle( - &test_dir, - &test_dir.join("output_html"), - &test_dir.join("output_gemini"), - &file, + assert!( + handler + .handle( + &test_dir, + &test_dir.join("output_html"), + &test_dir.join("output_gemini"), + &file, + ) + .is_ok() ); } #[test] - #[should_panic(expected = "Layout should be initialized before handling files")] fn test_handle_without_layout() { let handler = FileHandler::default(); let file = create_test_internal_file("test.gmi", FileType::Gemini); - handler.handle( - &PathBuf::from("source"), - &PathBuf::from("output_html"), - &PathBuf::from("output_gemini"), - &file, + assert!( + handler + .handle( + &PathBuf::from("source"), + &PathBuf::from("output_html"), + &PathBuf::from("output_gemini"), + &file, + ) + .is_err() ); } @@ -320,14 +440,18 @@ mod tests { ), ]; - let _ = handler.get_layout_or_panic(&files[1..]); + let _ = handler.get_layout(&files[1..]); // Test with slice - handler.handle_all( - &test_dir, - &test_dir.join("output_html"), - &test_dir.join("output_gemini"), - &files[1..], // Test with slice of last three elements + assert!( + handler + .handle_all( + &test_dir, + &test_dir.join("output_html"), + &test_dir.join("output_gemini"), + &files[1..], // Test with slice of last three elements + ) + .is_ok() ); } } |