]> git.r.bdr.sh - rbdr/page/blame - src/file_handler/mod.rs
Read the files
[rbdr/page] / src / file_handler / mod.rs
CommitLineData
1e2d00b6
RBR
1mod file_strategies;
2
3use file_strategies::file::Strategy as FileStrategy;
4use std::path::PathBuf;
5
6pub struct FileHandler {
7 pub strategies: Vec<Box<dyn FileHandlerStrategy>>
8}
9
10impl Default for FileHandler {
11 fn default() -> FileHandler {
12 FileHandler {
13 strategies: vec![Box::new(FileStrategy{})]
14 }
15 }
16}
17
18impl 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
37pub 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
44pub enum FileType {
45 Gemini,
46 File,
47 Layout,
48 Unknown,
49}
50
51pub struct File {
52 pub path: PathBuf,
53 pub file_type: FileType,
54}