X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/ad021007efdaced54b6351b4220d77fa8188309e..8766e4412b95cfa0288683748cc20aba81a64d08:/src/file_handler/mod.rs diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index cefe6a2..e8a446b 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -4,31 +4,31 @@ use file_strategies::file::Strategy as FileStrategy; use file_strategies::gemini::Strategy as GeminiStrategy; use file_strategies::layout::Strategy as LayoutStrategy; -use std::path::PathBuf; use std::fs::read_to_string; +use std::path::{Path, PathBuf}; pub struct FileHandler { - pub strategies: Vec>, - pub layout: Option + pub strategies: Vec>, + pub layout: Option, } impl Default for FileHandler { fn default() -> FileHandler { FileHandler { strategies: vec![ - Box::new(GeminiStrategy{}), - Box::new(LayoutStrategy{}), - Box::new(FileStrategy{}), + Box::new(GeminiStrategy {}), + Box::new(LayoutStrategy {}), + Box::new(FileStrategy {}), ], - layout: None + layout: None, } } } impl FileHandler { - pub fn identify(&self, path: &PathBuf) -> FileType { - for strategy in self.strategies.iter() { - if strategy.is(&path) { + pub fn identify(&self, path: &Path) -> FileType { + for strategy in &self.strategies { + if strategy.is(path) { return strategy.identify(); } } @@ -37,44 +37,55 @@ impl FileHandler { pub fn get_layout_or_panic(&mut self, files: &[File]) -> Result<(), &str> { for file in files { - match file.file_type { - FileType::Layout => { - let layout_text = read_to_string(&file.path).unwrap(); - self.layout = Some(layout_text); - return Ok(()); - }, - _ => {} + if file.file_type == FileType::Layout { + let layout_text = read_to_string(&file.path).unwrap(); + self.layout = Some(layout_text); + return Ok(()); } } Err("No layout found. Please ensure there's a _layout.html file at the root") } - pub fn handle_all(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, files: &[File]) { - files.iter().for_each(|file| { + pub fn handle_all( + &self, + source: &Path, + html_destination: &Path, + gemini_destination: &Path, + files: &[File], + ) { + for file in files { self.handle(source, html_destination, gemini_destination, file); - }); + } } - pub fn handle(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, file: &File) { - if let Some(strategy) = self.strategies + pub fn handle( + &self, + source: &Path, + html_destination: &Path, + gemini_destination: &Path, + file: &File, + ) { + if let Some(strategy) = self + .strategies .iter() - .find(|s| s.can_handle(&file.file_type)) + .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); - return; + 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); } } } -pub trait FileHandlerStrategy { - fn is(&self, path: &PathBuf) -> bool; +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: &PathBuf, destination: &PathBuf, file: &File, layout: &str); - fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File); + fn handle_html(&self, source: &Path, destination: &Path, file: &File, layout: &str); + fn handle_gemini(&self, source: &Path, destination: &Path, file: &File); } #[derive(Debug, Clone, PartialEq)] @@ -91,9 +102,9 @@ pub struct File { pub file_type: FileType, } - #[cfg(test)] mod tests { + use std::fs::create_dir_all; use std::path::PathBuf; use super::*; @@ -144,7 +155,10 @@ mod tests { let mut handler = FileHandler::default(); let files = vec![ create_test_internal_file("test.gmi", FileType::Gemini), - create_test_internal_file(&layout_path.to_str().expect("Could not encode layout"), FileType::Layout), + create_test_internal_file( + layout_path.to_str().expect("Could not encode layout"), + FileType::Layout, + ), create_test_internal_file("regular.html", FileType::File), ]; @@ -168,8 +182,8 @@ mod tests { file_type: FileType, } - impl FileHandlerStrategy for MockStrategy { - fn is(&self, _path: &PathBuf) -> bool { + impl Strategy for MockStrategy { + fn is(&self, _path: &Path) -> bool { self.is_match } @@ -181,8 +195,8 @@ mod tests { &self.file_type == file_type } - fn handle_html(&self, _source: &PathBuf, _destination: &PathBuf, _file: &File, _layout: &str) {} - fn handle_gemini(&self, _source: &PathBuf, _destination: &PathBuf, _file: &File) {} + fn handle_html(&self, _source: &Path, _destination: &Path, _file: &File, _layout: &str) {} + fn handle_gemini(&self, _source: &Path, _destination: &Path, _file: &File) {} } #[test] @@ -209,25 +223,39 @@ mod tests { // Should not panic with empty vector handler.handle_all( &PathBuf::from("source"), - &PathBuf::from("tests/fixtures/output"), - &PathBuf::from("tests/fixtures/output"), - &files + &PathBuf::from("output_html"), + &PathBuf::from("output_gemini"), + &files, ); } #[test] fn test_handle_with_layout() { - let mut handler = FileHandler::default(); - handler.layout = Some("test layout".to_string()); + let handler = FileHandler { + layout: Some("test layout".to_string()), + ..Default::default() + }; - let file = create_test_internal_file("tests/fixtures/test1.gmi", FileType::Gemini); + let test_dir = setup_test_dir(); + create_dir_all(test_dir.join("output_html")) + .expect("Could not create output html test directory"); + create_dir_all(test_dir.join("output_gemini")) + .expect("Could not create output gemini test directory"); + let test_path = test_dir.join("test.gmi"); + create_test_file(&test_path, ""); + let file = create_test_internal_file( + test_path + .to_str() + .expect("Could not encode gemini test file"), + FileType::Gemini, + ); // Should not panic with valid layout handler.handle( - &PathBuf::from(""), - &PathBuf::from("tests/fixtures/output"), - &PathBuf::from("tests/fixtures/output"), - &file + &test_dir, + &test_dir.join("output_html"), + &test_dir.join("output_gemini"), + &file, ); } @@ -239,9 +267,9 @@ mod tests { handler.handle( &PathBuf::from("source"), - &PathBuf::from("tests/fixtures/output"), - &PathBuf::from("tests/fixtures/output"), - &file + &PathBuf::from("output_html"), + &PathBuf::from("output_gemini"), + &file, ); } @@ -250,23 +278,51 @@ mod tests { let test_dir = setup_test_dir(); let layout_path = test_dir.join("_layout.html"); create_test_file(&layout_path, ""); + create_test_file(&test_dir.join("test1.gmi"), ""); + create_test_file(&test_dir.join("test2.gmi"), ""); + create_test_file(&test_dir.join("test3.gmi"), ""); + create_dir_all(test_dir.join("output_html")) + .expect("Could not create output html test directory"); + create_dir_all(test_dir.join("output_gemini")) + .expect("Could not create output gemini test directory"); let mut handler = FileHandler::default(); - let files = vec![ - create_test_internal_file("tests/fixtures/test1.gmi", FileType::Gemini), - create_test_internal_file(&layout_path.to_str().expect("Could not encode layout"), FileType::Layout), - create_test_internal_file("tests/fixtures/test2.gmi", FileType::Gemini), - create_test_internal_file("tests/fixtures/test3.gmi", FileType::Gemini), + let files = [ + create_test_internal_file( + test_dir + .join("test1.gmi") + .to_str() + .expect("Could not encode test1"), + FileType::Gemini, + ), + create_test_internal_file( + layout_path.to_str().expect("Could not encode layout"), + FileType::Layout, + ), + create_test_internal_file( + test_dir + .join("test2.gmi") + .to_str() + .expect("Could not encode test2"), + FileType::Gemini, + ), + create_test_internal_file( + test_dir + .join("test3.gmi") + .to_str() + .expect("Could not encode test3"), + FileType::Gemini, + ), ]; let _ = handler.get_layout_or_panic(&files[1..]); // Test with slice handler.handle_all( - &PathBuf::from(""), - &PathBuf::from("tests/fixtures/output"), - &PathBuf::from("tests/fixtures/output"), - &files[1..] // Test with slice of last three elements + &test_dir, + &test_dir.join("output_html"), + &test_dir.join("output_gemini"), + &files[1..], // Test with slice of last three elements ); } }