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
64
use super::Source;
use std::{
io,
path::{Path, PathBuf},
rc::Rc,
};
impl<S: Source> Source for Vec<S> {
fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
let mut res = None;
for source in self.iter() {
match source.read(path, dir) {
Ok(x) => {
res = Some(Ok(x));
break;
}
Err(e) => match e.kind() {
io::ErrorKind::NotFound => continue,
_ => {
res = Some(Err(e));
break;
}
},
}
}
match res {
Some(x) => x,
None => Err(io::Error::new(
io::ErrorKind::NotFound,
format!("path: {:?}, dir: {:?}", path, dir),
)),
}
}
}
#[macro_export]
macro_rules! source_vec {
($($x:expr),* $(,)?) => {
vec![ $(Box::new($x) as Box::<dyn $crate::Source>),* ]
};
}
impl<'a, S: Source> Source for &'a S {
fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
(*self).read(path, dir)
}
}
impl Source for Box<dyn Source> {
fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
self.as_ref().read(path, dir)
}
}
impl<S: Source> Source for Rc<S> {
fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
self.as_ref().read(path, dir)
}
}