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
161
162
163
164
165
166
167
168
169
170
171
172
use std::path::PathBuf;
use std::fmt::Debug;
use std::collections::HashMap;
use std::sync::Arc;
use failure::Fallible as Result;
use store::Entry;
use storeid::StoreId;
mod fs;
mod inmemory;
pub(crate) mod iter;
pub use self::fs::FSFileAbstraction;
pub use self::fs::FSFileAbstractionInstance;
pub use self::inmemory::InMemoryFileAbstraction;
pub use self::inmemory::InMemoryFileAbstractionInstance;
use self::iter::PathIterator;
pub trait FileAbstraction : Debug {
fn remove_file(&self, path: &PathBuf) -> Result<()>;
fn copy(&self, from: &PathBuf, to: &PathBuf) -> Result<()>;
fn rename(&self, from: &PathBuf, to: &PathBuf) -> Result<()>;
fn create_dir_all(&self, _: &PathBuf) -> Result<()>;
fn exists(&self, &PathBuf) -> Result<bool>;
fn is_file(&self, &PathBuf) -> Result<bool>;
fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance>;
fn drain(&self) -> Result<Drain>;
fn fill<'a>(&'a mut self, d: Drain) -> Result<()>;
fn pathes_recursively(&self, basepath: PathBuf, storepath: PathBuf, backend: Arc<FileAbstraction>) -> Result<PathIterator>;
}
pub trait FileAbstractionInstance : Debug {
fn get_file_content(&mut self, id: StoreId) -> Result<Option<Entry>>;
fn write_file_content(&mut self, buf: &Entry) -> Result<()>;
}
pub struct Drain(HashMap<PathBuf, Entry>);
impl Drain {
pub fn new(hm: HashMap<PathBuf, Entry>) -> Drain {
Drain(hm)
}
pub fn empty() -> Drain {
Drain::new(HashMap::new())
}
pub fn iter<'a>(&'a mut self) -> DrainIter<'a> {
DrainIter(self.0.drain())
}
}
pub struct DrainIter<'a>(::std::collections::hash_map::Drain<'a, PathBuf, Entry>);
impl<'a> Iterator for DrainIter<'a> {
type Item = (PathBuf, Entry);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use super::FileAbstractionInstance;
use super::inmemory::InMemoryFileAbstraction;
use super::inmemory::InMemoryFileAbstractionInstance;
use storeid::StoreId;
use store::Entry;
#[test]
fn lazy_file() {
let fs = InMemoryFileAbstraction::default();
let mut path = PathBuf::from("tests");
path.set_file_name("test1");
let mut lf = InMemoryFileAbstractionInstance::new(fs.backend().clone(), path.clone());
let loca = StoreId::new_baseless(path).unwrap();
let file = Entry::from_str(loca.clone(), &format!(r#"---
[imag]
version = "{}"
---
Hello World"#, env!("CARGO_PKG_VERSION"))).unwrap();
lf.write_file_content(&file).unwrap();
let bah = lf.get_file_content(loca).unwrap().unwrap();
assert_eq!(bah.get_content(), "Hello World");
}
#[test]
fn lazy_file_multiline() {
let fs = InMemoryFileAbstraction::default();
let mut path = PathBuf::from("tests");
path.set_file_name("test1");
let mut lf = InMemoryFileAbstractionInstance::new(fs.backend().clone(), path.clone());
let loca = StoreId::new_baseless(path).unwrap();
let file = Entry::from_str(loca.clone(), &format!(r#"---
[imag]
version = "{}"
---
Hello World
baz"#, env!("CARGO_PKG_VERSION"))).unwrap();
lf.write_file_content(&file).unwrap();
let bah = lf.get_file_content(loca).unwrap().unwrap();
assert_eq!(bah.get_content(), "Hello World\nbaz");
}
#[test]
fn lazy_file_multiline_trailing_newlines() {
let fs = InMemoryFileAbstraction::default();
let mut path = PathBuf::from("tests");
path.set_file_name("test1");
let mut lf = InMemoryFileAbstractionInstance::new(fs.backend().clone(), path.clone());
let loca = StoreId::new_baseless(path).unwrap();
let file = Entry::from_str(loca.clone(), &format!(r#"---
[imag]
version = "{}"
---
Hello World
baz
"#, env!("CARGO_PKG_VERSION"))).unwrap();
lf.write_file_content(&file).unwrap();
let bah = lf.get_file_content(loca).unwrap().unwrap();
assert_eq!(bah.get_content(), "Hello World\nbaz\n\n");
}
}