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
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 add_hook<T: 'static + Hook>(mut self, hook: T) -> Self {
        self.hooks.push(Box::new(hook));
        self
    }
}

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