]> git.r.bdr.sh - rbdr/page/blob - src/file_handler/mod.rs
Add basic strategies
[rbdr/page] / src / file_handler / mod.rs
1 mod file_strategies;
2
3 use file_strategies::file::Strategy as FileStrategy;
4 use file_strategies::gemini::Strategy as GeminiStrategy;
5 use file_strategies::layout::Strategy as LayoutStrategy;
6
7 use std::path::PathBuf;
8
9 pub struct FileHandler {
10 pub strategies: Vec<Box<dyn FileHandlerStrategy>>
11 }
12
13 impl Default for FileHandler {
14 fn default() -> FileHandler {
15 FileHandler {
16 strategies: vec![
17 Box::new(GeminiStrategy{}),
18 Box::new(LayoutStrategy{}),
19 Box::new(FileStrategy{}),
20 ]
21 }
22 }
23 }
24
25 impl FileHandler {
26 pub fn identify(&self, path: &PathBuf) -> FileType {
27 for strategy in self.strategies.iter() {
28 if strategy.is(&path) {
29 return strategy.identify();
30 }
31 }
32 FileType::Unknown
33 }
34
35 pub fn handle(&self, path: &PathBuf) {
36 for strategy in self.strategies.iter() {
37 if strategy.can_handle(path) {
38 return strategy.handle(path);
39 }
40 }
41 }
42 }
43
44 pub trait FileHandlerStrategy {
45 fn is(&self, path: &PathBuf) -> bool;
46 fn identify(&self) -> FileType;
47 fn can_handle(&self, path: &PathBuf) -> bool;
48 fn handle(&self, path: &PathBuf);
49 }
50
51 pub enum FileType {
52 Gemini,
53 File,
54 Layout,
55 Unknown,
56 }
57
58 pub struct File {
59 pub path: PathBuf,
60 pub file_type: FileType,
61 }