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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use failure::Fail;
use slugify::slugify;
use crate::dto::Task;
use crate::dto::Watchlist;
use crate::storage::storage::PhabStorage;
use crate::types::ResultDynError;
type Table = HashMap<String, Watchlist>;
type FileDB = HashMap<String, Table>;
pub struct PhabStorageFilesystem {
pub filepath: PathBuf,
db_content: FileDB,
}
impl PhabStorageFilesystem {
pub fn new(filepath: impl AsRef<Path>) -> ResultDynError<PhabStorageFilesystem> {
let mut storage = PhabStorageFilesystem {
db_content: HashMap::new(),
filepath: PathBuf::from(filepath.as_ref()),
};
storage.reload()?;
return Ok(storage);
}
}
impl PhabStorageFilesystem {
fn watchlist_table(&mut self) -> &mut Table {
return self
.db_content
.entry("watchlists".to_owned())
.or_insert(HashMap::new());
}
fn reload(&mut self) -> ResultDynError<()> {
if !self.filepath.exists() {
let _ = self.watchlist_table();
self.persist()?;
}
let str = fs::read_to_string(&self.filepath)?;
self.db_content = serde_json::from_str(&str)?;
return Ok(());
}
fn persist(&self) -> ResultDynError<()> {
if !self.filepath.exists() {
let mut cloned_filepath = self.filepath.clone();
cloned_filepath.pop();
fs::create_dir_all(&cloned_filepath)?;
}
let content = serde_json::to_string(&self.db_content)?;
fs::write(&self.filepath, content)?;
return Ok(());
}
}
#[derive(Debug, Fail)]
enum PhabStorageFilesystemError {
#[fail(display = "PhabStorageFilesystemError err: {}", message)]
QueryError { message: String },
}
impl PhabStorageFilesystemError {
fn query_error(message: &str) -> failure::Error {
return PhabStorageFilesystemError::QueryError {
message: message.to_owned(),
}
.into();
}
}
impl PhabStorage for PhabStorageFilesystem {
fn add_to_watchlist(&mut self, watchlist_id: &str, task: &Task) -> ResultDynError<()> {
self
.watchlist_table()
.get_mut(watchlist_id)
.unwrap()
.tasks
.push(task.clone());
self.persist()?;
return Ok(());
}
fn create_watchlist(&mut self, watchlist: &Watchlist) -> ResultDynError<Watchlist> {
let watchlists = self.db_content.get_mut("watchlists").unwrap();
let watchlist_id = slugify!(&watchlist.name);
let mut watchlist = watchlist.clone();
watchlist.id = Some(watchlist_id.clone());
watchlists.insert(watchlist_id, watchlist.to_owned());
self.persist()?;
return Ok(watchlist);
}
fn get_watchlists(&mut self) -> ResultDynError<Vec<Watchlist>> {
let watchlists: Vec<Watchlist> = self.watchlist_table().values().cloned().collect();
return Ok(watchlists);
}
fn get_watchlist_by_id(&mut self, watchlist_id: &str) -> ResultDynError<Option<Watchlist>> {
let watchlist = self.watchlist_table().get(watchlist_id).map(Clone::clone);
return Ok(watchlist);
}
}
#[cfg(test)]
mod test {
use super::*;
use std::fs;
struct DirCleaner {
dir: PathBuf,
}
impl Drop for DirCleaner {
fn drop(&mut self) {
if self.dir.exists() {
fs::remove_dir_all(&self.dir).unwrap();
}
}
}
mod reload {
use super::*;
use fake::Fake;
use fake::Faker;
fn create_new(db_dir_path: PathBuf) -> ResultDynError<PhabStorageFilesystem> {
let mut storage = PhabStorageFilesystem {
db_content: HashMap::new(),
filepath: PathBuf::from(format!(
"{}/{}",
db_dir_path.into_os_string().into_string().unwrap(),
"yo.json"
)),
};
storage.reload()?;
return Ok(storage);
}
fn test_db_dir(fn_name: &str) -> PathBuf {
return PathBuf::from(format!("/tmp/__phab_for_testing/db_{}", fn_name));
}
#[test]
fn it_should_create_dir_and_load_data_for_first_time() -> ResultDynError<()> {
let db_dir_path = test_db_dir(function_name!());
let _dir_cleaner = DirCleaner {
dir: db_dir_path.clone(),
};
let mut storage = test::reload::create_new(db_dir_path)?;
let watchlists = storage.get_watchlists()?;
assert_eq!(watchlists.len(), 0);
return Ok(());
}
#[test]
fn it_should_insert_data() -> ResultDynError<()> {
let db_dir_path = test_db_dir(function_name!());
let _dir_cleaner = DirCleaner {
dir: db_dir_path.clone(),
};
let mut storage = test::reload::create_new(db_dir_path)?;
let watchlist = Watchlist {
id: None,
name: String::from("hey ho test watchlist"),
tasks: vec![],
};
storage.create_watchlist(&watchlist)?;
let watchlists = storage.get_watchlists()?;
assert_eq!(watchlists.len(), 1);
assert_eq!(
watchlists.get(0).unwrap().id.as_ref().unwrap(),
"hey-ho-test-watchlist"
);
storage.reload()?;
assert_eq!(watchlists.len(), 1);
assert_eq!(
watchlists.get(0).unwrap().id.as_ref().unwrap(),
"hey-ho-test-watchlist"
);
return Ok(());
}
#[test]
fn it_should_add_to_watchlist() -> ResultDynError<()> {
let db_dir_path = test_db_dir(function_name!());
let _dir_cleaner = DirCleaner {
dir: db_dir_path.clone(),
};
let mut storage = test::reload::create_new(db_dir_path)?;
let watchlist = Watchlist {
id: None,
name: String::from("hey ho test watchlist"),
tasks: vec![],
};
let mut task_1: Task = Faker.fake();
task_1.id = "foo".to_owned();
let mut task_2: Task = Faker.fake();
task_2.id = "Bar".to_owned();
let watchlist = storage.create_watchlist(&watchlist)?;
let watchlist_id = watchlist.id.unwrap();
storage.add_to_watchlist(&watchlist_id, &task_1)?;
storage.add_to_watchlist(&watchlist_id, &task_2)?;
let watchlist = storage.get_watchlist_by_id(&watchlist_id)?;
assert!(watchlist.is_some());
let tasks: Vec<Task> = watchlist.unwrap().tasks;
assert_eq!(tasks.len(), 2);
assert_eq!(tasks.get(0).unwrap().id, "foo");
assert_eq!(tasks.get(1).unwrap().id, "Bar");
return Ok(());
}
}
}