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
62
63
use std::{
io,
path::{Path, PathBuf},
};
use super::{Hook};
pub struct ListHook {
hooks: Vec<Box<dyn Hook>>,
}
impl ListHook {
pub fn new() -> Self {
Self { hooks: Vec::new() }
}
pub fn builder() -> ListHookBuilder {
ListHookBuilder { hook: Self::new() }
}
pub fn add_hook<T: 'static + Hook>(&mut self, hook: T) {
self.hooks.push(Box::new(hook));
}
}
pub struct ListHookBuilder {
hook: ListHook,
}
impl ListHookBuilder {
pub fn add_hook<T: 'static + Hook>(mut self, hook: T) -> Self {
self.hook.add_hook(hook);
self
}
pub fn build(self) -> ListHook {
self.hook
}
}
impl Hook for ListHook {
fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
let mut res = None;
for hook in self.hooks.iter() {
match hook.read(path, dir) {
Ok(x) => {
res = Some(Ok(x));
break;
},
Err(e) => match e.kind() {
io::ErrorKind::NotFound => continue,
_ => {
res = Some(Err(e));
break;
}
}
}
}
res.unwrap_or(Err(io::Error::new(
io::ErrorKind::NotFound,
format!("path: {:?}, dir: {:?}", path, dir),
)))
}
}