]> git.r.bdr.sh - rbdr/page/blobdiff - src/file_handler/mod.rs
Add gemini parsing
[rbdr/page] / src / file_handler / mod.rs
index 8038b3c032189536191e7855864944a4f423cbe6..a106c27745013db17b9941e640afee7040d44b86 100644 (file)
@@ -1,16 +1,26 @@
 mod file_strategies;
 
 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;
 
 pub struct FileHandler {
-   pub strategies: Vec<Box<dyn FileHandlerStrategy>>
+   pub strategies: Vec<Box<dyn FileHandlerStrategy>>,
+   pub layout: Option<String>
 }
 
 impl Default for FileHandler {
     fn default() -> FileHandler {
         FileHandler {
-            strategies: vec![Box::new(FileStrategy{})]
+            strategies: vec![
+                Box::new(GeminiStrategy{}),
+                Box::new(LayoutStrategy{}),
+                Box::new(FileStrategy{}),
+            ],
+            layout: None
         }
     }
 }
@@ -25,10 +35,31 @@ 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) {
+                let layout = self.layout.as_ref().unwrap();
+                return strategy.handle(source, destination, file, layout);
             }
         }
     }
@@ -37,8 +68,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, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String);
 }
 
 pub enum FileType {