diff options
Diffstat (limited to 'src/file_handler')
| -rw-r--r-- | src/file_handler/file_strategies/file.rs | 13 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/gemini.rs | 13 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/layout.rs | 14 | ||||
| -rw-r--r-- | src/file_handler/mod.rs | 37 |
4 files changed, 53 insertions, 24 deletions
diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index c9e8c96..b1de596 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -2,7 +2,7 @@ pub struct Strategy {} use std::path::PathBuf; -use crate::file_handler::{FileType, FileHandlerStrategy}; +use crate::file_handler::{File, FileType, FileHandlerStrategy}; impl FileHandlerStrategy for Strategy { fn is(&self, path: &PathBuf) -> bool { @@ -13,11 +13,14 @@ impl FileHandlerStrategy for Strategy { FileType::File } - fn can_handle(&self, path: &PathBuf) -> bool { - self.is(path) + fn can_handle(&self, file_type: &FileType) -> bool { + match file_type { + FileType::File => true, + _ => false, + } } - fn handle(&self, path: &PathBuf) { - println!("Should copy {}", path.display()) + fn handle(&self, file: &File) { + println!("Should copy {}", file.path.display()) } } diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index ffa2a9f..905e1a9 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -2,7 +2,7 @@ pub struct Strategy {} use std::path::PathBuf; -use crate::file_handler::{FileType, FileHandlerStrategy}; +use crate::file_handler::{File, FileType, FileHandlerStrategy}; impl FileHandlerStrategy for Strategy { fn is(&self, path: &PathBuf) -> bool { @@ -16,11 +16,14 @@ impl FileHandlerStrategy for Strategy { FileType::Gemini } - fn can_handle(&self, path: &PathBuf) -> bool { - self.is(path) + fn can_handle(&self, file_type: &FileType) -> bool { + match file_type { + FileType::Gemini => true, + _ => false, + } } - fn handle(&self, path: &PathBuf) { - println!("Should convert {}", path.display()) + fn handle(&self, file: &File) { + println!("Should parse and copy {}", file.path.display()) } } diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index cf1bb9f..bac1059 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -2,7 +2,7 @@ pub struct Strategy {} use std::path::PathBuf; -use crate::file_handler::{FileType, FileHandlerStrategy}; +use crate::file_handler::{File, FileType, FileHandlerStrategy}; impl FileHandlerStrategy for Strategy { fn is(&self, path: &PathBuf) -> bool { @@ -13,12 +13,12 @@ impl FileHandlerStrategy for Strategy { FileType::Layout } - fn can_handle(&self, path: &PathBuf) -> bool { - self.is(path) + fn can_handle(&self, file_type: &FileType) -> bool { + match file_type { + FileType::Layout => true, + _ => false, + } } - fn handle(&self, path: &PathBuf) { - println!("Should convert {}", path.display()) - } + fn handle(&self, _file: &File) {} } - diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index 44fec6d..53aaba9 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<Box<dyn FileHandlerStrategy>> + pub strategies: Vec<Box<dyn FileHandlerStrategy>>, + pub layout: Option<String> } 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,30 @@ impl FileHandler { FileType::Unknown } - pub fn handle(&self, path: &PathBuf) { + pub fn get_layout_or_panic(&mut self, files: &Vec<File>) { + 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; + }, + _ => {} + } + } + panic!("No layout found. Please ensure there's a _layout.html file at the root"); + } + + pub fn handle_all(&self, source: &PathBuf, destination: &PathBuf, files: &Vec<File>) { + for file in files { + self.handle(source, destination, file); + } + } + + pub fn handle(&self, source: &PathBuf, 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) { + return strategy.handle(file); } } } @@ -44,8 +67,8 @@ 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(&self, file: &File); } pub enum FileType { |