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;
use std::path::{Path, PathBuf};
use std::collections::{HashMap};
use super::{Hook};
pub struct MemHook {
files: HashMap<PathBuf, String>,
}
impl MemHook {
pub fn new() -> Self {
Self { files: HashMap::new() }
}
pub fn add_file(mut self, name: &Path, data: String) -> io::Result<Self> {
match self.files.insert(name.to_path_buf(), data) {
Some(_) => Err(io::ErrorKind::AlreadyExists.into()),
None => Ok(self),
}
}
fn read_file(&self, path: &Path) -> Option<String> {
self.files.get(path).map(|data| data.clone())
}
}
impl Hook for MemHook {
fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
dir
.and_then(|dir| {
let path = dir.join(path);
self.read_file(&path)
.map(|data| (path, data))
})
.or_else(|| {
self.files.get(path)
.map(|data| (path.to_path_buf(), data.clone()))
})
.ok_or(io::Error::new(
io::ErrorKind::NotFound,
format!("path: {:?}, dir: {:?}", path, dir),
))
}
}