]>
Commit | Line | Data |
---|---|---|
1e2d00b6 RBR |
1 | mod file_strategies; |
2 | ||
3 | use file_strategies::file::Strategy as FileStrategy; | |
102a4884 RBR |
4 | use file_strategies::gemini::Strategy as GeminiStrategy; |
5 | use file_strategies::layout::Strategy as LayoutStrategy; | |
6 | ||
1e2d00b6 | 7 | use std::path::PathBuf; |
4fd89b80 | 8 | use std::fs::read_to_string; |
1e2d00b6 RBR |
9 | |
10 | pub struct FileHandler { | |
4fd89b80 RBR |
11 | pub strategies: Vec<Box<dyn FileHandlerStrategy>>, |
12 | pub layout: Option<String> | |
1e2d00b6 RBR |
13 | } |
14 | ||
15 | impl Default for FileHandler { | |
16 | fn default() -> FileHandler { | |
17 | FileHandler { | |
102a4884 RBR |
18 | strategies: vec![ |
19 | Box::new(GeminiStrategy{}), | |
20 | Box::new(LayoutStrategy{}), | |
21 | Box::new(FileStrategy{}), | |
4fd89b80 RBR |
22 | ], |
23 | layout: None | |
1e2d00b6 RBR |
24 | } |
25 | } | |
26 | } | |
27 | ||
28 | impl FileHandler { | |
29 | pub fn identify(&self, path: &PathBuf) -> FileType { | |
30 | for strategy in self.strategies.iter() { | |
31 | if strategy.is(&path) { | |
32 | return strategy.identify(); | |
33 | } | |
34 | } | |
35 | FileType::Unknown | |
36 | } | |
37 | ||
48ea9080 | 38 | pub fn get_layout_or_panic(&mut self, files: &Vec<File>) -> Result<(), &str> { |
4fd89b80 RBR |
39 | for file in files { |
40 | match file.file_type { | |
41 | FileType::Layout => { | |
42 | let layout_text = read_to_string(&file.path).unwrap(); | |
43 | self.layout = Some(layout_text); | |
48ea9080 | 44 | return Ok(()); |
4fd89b80 RBR |
45 | }, |
46 | _ => {} | |
47 | } | |
48 | } | |
48ea9080 | 49 | Err("No layout found. Please ensure there's a _layout.html file at the root") |
4fd89b80 RBR |
50 | } |
51 | ||
dd0a540c | 52 | pub fn handle_all(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, files: &Vec<File>) { |
4fd89b80 | 53 | for file in files { |
dd0a540c | 54 | self.handle(source, html_destination, gemini_destination, file); |
4fd89b80 RBR |
55 | } |
56 | } | |
57 | ||
dd0a540c | 58 | pub fn handle(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, file: &File) { |
1e2d00b6 | 59 | for strategy in self.strategies.iter() { |
4fd89b80 | 60 | if strategy.can_handle(&file.file_type) { |
68fa37d6 | 61 | let layout = self.layout.as_ref().unwrap(); |
dd0a540c RBR |
62 | strategy.handle_html(source, html_destination, file, layout); |
63 | strategy.handle_gemini(source, gemini_destination, file); | |
64 | return; | |
1e2d00b6 RBR |
65 | } |
66 | } | |
67 | } | |
68 | } | |
69 | ||
70 | pub trait FileHandlerStrategy { | |
71 | fn is(&self, path: &PathBuf) -> bool; | |
72 | fn identify(&self) -> FileType; | |
4fd89b80 | 73 | fn can_handle(&self, file_type: &FileType) -> bool; |
dd0a540c RBR |
74 | fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String); |
75 | fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File); | |
1e2d00b6 RBR |
76 | } |
77 | ||
78 | pub enum FileType { | |
79 | Gemini, | |
80 | File, | |
81 | Layout, | |
82 | Unknown, | |
83 | } | |
84 | ||
85 | pub struct File { | |
86 | pub path: PathBuf, | |
87 | pub file_type: FileType, | |
88 | } |