]>
Commit | Line | Data |
---|---|---|
1e2d00b6 RBR |
1 | mod file_strategies; |
2 | ||
3 | use file_strategies::file::Strategy as FileStrategy; | |
4 | use std::path::PathBuf; | |
5 | ||
6 | pub struct FileHandler { | |
7 | pub strategies: Vec<Box<dyn FileHandlerStrategy>> | |
8 | } | |
9 | ||
10 | impl Default for FileHandler { | |
11 | fn default() -> FileHandler { | |
12 | FileHandler { | |
13 | strategies: vec![Box::new(FileStrategy{})] | |
14 | } | |
15 | } | |
16 | } | |
17 | ||
18 | impl FileHandler { | |
19 | pub fn identify(&self, path: &PathBuf) -> FileType { | |
20 | for strategy in self.strategies.iter() { | |
21 | if strategy.is(&path) { | |
22 | return strategy.identify(); | |
23 | } | |
24 | } | |
25 | FileType::Unknown | |
26 | } | |
27 | ||
28 | pub fn handle(&self, path: &PathBuf) { | |
29 | for strategy in self.strategies.iter() { | |
30 | if strategy.can_handle(path) { | |
31 | return strategy.handle(path); | |
32 | } | |
33 | } | |
34 | } | |
35 | } | |
36 | ||
37 | pub trait FileHandlerStrategy { | |
38 | fn is(&self, path: &PathBuf) -> bool; | |
39 | fn identify(&self) -> FileType; | |
40 | fn can_handle(&self, path: &PathBuf) -> bool; | |
41 | fn handle(&self, path: &PathBuf); | |
42 | } | |
43 | ||
44 | pub enum FileType { | |
45 | Gemini, | |
46 | File, | |
47 | Layout, | |
48 | Unknown, | |
49 | } | |
50 | ||
51 | pub struct File { | |
52 | pub path: PathBuf, | |
53 | pub file_type: FileType, | |
54 | } |