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
use std::path::Path;
use super::errors::FinderError;
#[derive(Clone, Debug, Default)]
pub struct Options {
pub paths: Vec<String>,
}
impl Options {
pub fn new() -> Options {
Options {
..Default::default()
}
}
}
#[derive(Clone, Debug, Default)]
pub struct Finder {
pub opts: Options,
}
impl Finder {
pub fn new() -> Finder {
Finder {
opts: Options::new(),
}
}
pub fn add_path<'a>(&'a mut self, path: &str) -> &'a mut Finder {
self.opts.paths.push(path.to_string());
self
}
pub fn add_paths(&mut self, paths: Vec<String>) -> &mut Finder {
for path in paths {
self.opts.paths.push(path);
}
self
}
pub fn find(&self, filename: &str) -> Result<String, FinderError> {
find_file(filename, &self.opts)
}
}
pub fn find_file(filename: &str, opts: &Options) -> Result<String, FinderError> {
let path = Path::new(filename);
if path.exists() {
return Ok(path.to_str().unwrap().to_string());
};
for path in opts.paths.iter() {
let file = Path::new(path).join(filename);
if file.exists() {
return Ok(file.to_str().unwrap().to_string());
};
}
Err(FinderError::NotFound(filename.to_string()))
}