3 use file_strategies::file::Strategy as FileStrategy;
4 use file_strategies::gemini::Strategy as GeminiStrategy;
5 use file_strategies::layout::Strategy as LayoutStrategy;
7 use std::path::PathBuf;
8 use std::fs::read_to_string;
10 pub struct FileHandler {
11 pub strategies: Vec<Box<dyn FileHandlerStrategy>>,
12 pub layout: Option<String>
15 impl Default for FileHandler {
16 fn default() -> FileHandler {
19 Box::new(GeminiStrategy{}),
20 Box::new(LayoutStrategy{}),
21 Box::new(FileStrategy{}),
29 pub fn identify(&self, path: &PathBuf) -> FileType {
30 for strategy in self.strategies.iter() {
31 if strategy.is(&path) {
32 return strategy.identify();
38 pub fn get_layout_or_panic(&mut self, files: &Vec<File>) {
40 match file.file_type {
42 let layout_text = read_to_string(&file.path).unwrap();
43 self.layout = Some(layout_text);
49 panic!("No layout found. Please ensure there's a _layout.html file at the root");
52 pub fn handle_all(&self, source: &PathBuf, destination: &PathBuf, files: &Vec<File>) {
54 self.handle(source, destination, file);
58 pub fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
59 for strategy in self.strategies.iter() {
60 if strategy.can_handle(&file.file_type) {
61 let layout = self.layout.as_ref().unwrap();
62 return strategy.handle(source, destination, file, layout);
68 pub trait FileHandlerStrategy {
69 fn is(&self, path: &PathBuf) -> bool;
70 fn identify(&self) -> FileType;
71 fn can_handle(&self, file_type: &FileType) -> bool;
72 fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String);
84 pub file_type: FileType,