]> git.r.bdr.sh - rbdr/page/blame - src/file_handler/mod.rs
Add basic strategies
[rbdr/page] / src / file_handler / mod.rs
CommitLineData
1e2d00b6
RBR
1mod file_strategies;
2
3use file_strategies::file::Strategy as FileStrategy;
102a4884
RBR
4use file_strategies::gemini::Strategy as GeminiStrategy;
5use file_strategies::layout::Strategy as LayoutStrategy;
6
1e2d00b6
RBR
7use std::path::PathBuf;
8
9pub struct FileHandler {
10 pub strategies: Vec<Box<dyn FileHandlerStrategy>>
11}
12
13impl Default for FileHandler {
14 fn default() -> FileHandler {
15 FileHandler {
102a4884
RBR
16 strategies: vec![
17 Box::new(GeminiStrategy{}),
18 Box::new(LayoutStrategy{}),
19 Box::new(FileStrategy{}),
20 ]
1e2d00b6
RBR
21 }
22 }
23}
24
25impl 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
44pub 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
51pub enum FileType {
52 Gemini,
53 File,
54 Layout,
55 Unknown,
56}
57
58pub struct File {
59 pub path: PathBuf,
60 pub file_type: FileType,
61}