X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/102a4884c3d7d26817fefb38c675be07047f5ee2..8a12b5cd2b2fe5beb2cb72ecda8b51a707ea80f8:/src/file_handler/mod.rs?ds=sidebyside diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index 44fec6d..9a0133a 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -5,9 +5,11 @@ use file_strategies::gemini::Strategy as GeminiStrategy; use file_strategies::layout::Strategy as LayoutStrategy; use std::path::PathBuf; +use std::fs::read_to_string; pub struct FileHandler { - pub strategies: Vec> + pub strategies: Vec>, + pub layout: Option } impl Default for FileHandler { @@ -17,7 +19,8 @@ impl Default for FileHandler { Box::new(GeminiStrategy{}), Box::new(LayoutStrategy{}), Box::new(FileStrategy{}), - ] + ], + layout: None } } } @@ -32,10 +35,33 @@ impl FileHandler { FileType::Unknown } - pub fn handle(&self, path: &PathBuf) { + pub fn get_layout_or_panic(&mut self, files: &Vec) -> 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(()); + }, + _ => {} + } + } + 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: &Vec) { + 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) { for strategy in self.strategies.iter() { - if strategy.can_handle(path) { - return strategy.handle(path); + if strategy.can_handle(&file.file_type) { + let layout = self.layout.as_ref().unwrap(); + strategy.handle_html(source, html_destination, file, layout); + strategy.handle_gemini(source, gemini_destination, file); + return; } } } @@ -44,8 +70,9 @@ impl FileHandler { pub trait FileHandlerStrategy { fn is(&self, path: &PathBuf) -> bool; fn identify(&self) -> FileType; - fn can_handle(&self, path: &PathBuf) -> bool; - fn handle(&self, path: &PathBuf); + fn can_handle(&self, file_type: &FileType) -> bool; + fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String); + fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File); } pub enum FileType {