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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use errors::Result;
use linked_hash_map::LinkedHashMap;
use std::cell::RefCell;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use {RelativePath, RelativePathBuf};
pub trait Handle {
fn is_dir(&self, path: &RelativePath) -> bool;
fn is_file(&self, path: &RelativePath) -> bool;
fn create_dir_all(&self, path: &RelativePath) -> Result<()>;
fn create(&self, path: &RelativePath) -> Result<Box<io::Write>>;
}
pub trait Filesystem {
fn open_root(&self, root: Option<&Path>) -> Result<Box<Handle>>;
}
pub struct RealFilesystem {}
impl RealFilesystem {
pub fn new() -> RealFilesystem {
Self {}
}
}
impl Filesystem for RealFilesystem {
fn open_root(&self, root: Option<&Path>) -> Result<Box<Handle>> {
let root = root.ok_or_else(|| {
"Missing root directory, specify using `--out`, or `output` key in manifest"
})?
.to_owned();
return Ok(Box::new(RealHandle { root: root }));
struct RealHandle {
root: PathBuf,
}
impl Handle for RealHandle {
fn is_dir(&self, path: &RelativePath) -> bool {
path.to_path(&self.root).is_dir()
}
fn is_file(&self, path: &RelativePath) -> bool {
path.to_path(&self.root).is_file()
}
fn create_dir_all(&self, path: &RelativePath) -> Result<()> {
let path = path.to_path(&self.root);
Ok(fs::create_dir_all(&path)?)
}
fn create(&self, path: &RelativePath) -> Result<Box<io::Write>> {
let path = path.to_path(&self.root);
Ok(Box::new(fs::File::create(&path)?))
}
}
}
}
pub struct CapturingFilesystem {
files: Rc<RefCell<LinkedHashMap<RelativePathBuf, Vec<u8>>>>,
}
impl CapturingFilesystem {
pub fn new() -> CapturingFilesystem {
Self {
files: Rc::new(RefCell::new(LinkedHashMap::new())),
}
}
pub fn filesystem(&self) -> Box<Filesystem> {
Box::new(CapturingFilesystem {
files: self.files.clone(),
})
}
pub fn files(&self) -> &Rc<RefCell<LinkedHashMap<RelativePathBuf, Vec<u8>>>> {
&self.files
}
}
impl Filesystem for CapturingFilesystem {
fn open_root(&self, _root: Option<&Path>) -> Result<Box<Handle>> {
return Ok(Box::new(CapturingHandle {
files: self.files.clone(),
}));
}
}
struct CapturingHandle {
files: Rc<RefCell<LinkedHashMap<RelativePathBuf, Vec<u8>>>>,
}
impl Handle for CapturingHandle {
fn is_dir(&self, _path: &RelativePath) -> bool {
true
}
fn is_file(&self, path: &RelativePath) -> bool {
self.files.borrow().contains_key(path)
}
fn create_dir_all(&self, _path: &RelativePath) -> Result<()> {
Ok(())
}
fn create(&self, path: &RelativePath) -> Result<Box<io::Write>> {
Ok(Box::new(CapturingFileCreate {
files: self.files.clone(),
path: path.to_owned(),
buffer: Vec::new(),
}))
}
}
struct CapturingFileCreate {
files: Rc<RefCell<LinkedHashMap<RelativePathBuf, Vec<u8>>>>,
path: RelativePathBuf,
buffer: Vec<u8>,
}
impl io::Write for CapturingFileCreate {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buffer.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.buffer.flush()
}
}
impl Drop for CapturingFileCreate {
fn drop(&mut self) {
let mut files = self.files.borrow_mut();
files.insert(self.path.clone(), self.buffer.clone());
}
}