aboutsummaryrefslogtreecommitdiff
path: root/src/file_handler/mod.rs
blob: 44fec6db5589df43d12f6767f5eee687215b4b18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;

pub struct FileHandler {
   pub strategies: Vec<Box<dyn FileHandlerStrategy>>
}

impl Default for FileHandler {
    fn default() -> FileHandler {
        FileHandler {
            strategies: vec![
                Box::new(GeminiStrategy{}),
                Box::new(LayoutStrategy{}),
                Box::new(FileStrategy{}),
            ]
        }
    }
}

impl FileHandler {
    pub fn identify(&self, path: &PathBuf) -> FileType {
        for strategy in self.strategies.iter() {
            if strategy.is(&path) {
                return strategy.identify();
            }
        }
        FileType::Unknown
    }

    pub fn handle(&self, path: &PathBuf) {
        for strategy in self.strategies.iter() {
            if strategy.can_handle(path) {
                return strategy.handle(path);
            }
        }
    }
}

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);
}

pub enum FileType {
    Gemini,
    File,
    Layout,
    Unknown,
}

pub struct File {
    pub path: PathBuf,
    pub file_type: FileType,
}