-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent_file_list.rs
256 lines (208 loc) · 7.12 KB
/
persistent_file_list.rs
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
255
256
extern crate serde_json;
use file_list::{FileList, FileLocation, FileListSource, FileListList};
use file_database;
use std::path::{PathBuf, Path};
use std::fs;
use std::io::{Write, Read};
use error::Result;
#[derive(Serialize, Deserialize)]
pub enum SaveableFileLocation {
Unsaved(PathBuf),
Database(i32),
}
#[derive(Serialize, Deserialize)]
pub struct SaveableFileList {
pub source: FileListSource,
pub files: Vec<SaveableFileLocation>,
}
/**
Converts a `FileList` to a json string
*/
fn saveable_file_list(list: &FileList) -> SaveableFileList {
let files = list.get_files()
.iter()
.map(|location| match *location {
FileLocation::Unsaved(ref path) => SaveableFileLocation::Unsaved(path.clone()),
FileLocation::Database(ref entry) => SaveableFileLocation::Database(entry.id),
})
.collect();
SaveableFileList {
source: list.get_source().clone(),
files,
}
}
/**
Converts a jsonified file list into a file list.
If the file entry associated
with a saved id has disappeared since the json was generated, it will be ignored
and removed from the new list.
*/
fn list_from_saveable(
saveable_list: SaveableFileList,
db: &file_database::FileDatabase,
) -> FileList {
let files = saveable_list
.files
.into_iter()
.filter_map(|location| match location {
SaveableFileLocation::Unsaved(path) => Some(FileLocation::Unsaved(path)),
SaveableFileLocation::Database(id) => {
match db.get_file_with_id(id) {
Some(file) => Some(FileLocation::Database(file)),
None => None,
}
}
})
.collect();
FileList::from_locations(files, saveable_list.source)
}
/**
Generates a vector of `SaveableFileList`s from a `FileListList`. Only file lists
originating from a directory will be saved
*/
pub fn saveable_file_list_list(list: &FileListList) -> Vec<SaveableFileList> {
list.get_lists()
.iter()
.filter(|file_list| match *file_list.get_source() {
FileListSource::Search => false,
_ => true,
})
.map(saveable_file_list)
.collect()
}
/**
Converts a vector of `SaveableFileList` to a `FileListList`
*/
fn file_list_list_from_saveable(
saveable: Vec<SaveableFileList>,
db: &file_database::FileDatabase,
) -> FileListList {
let file_lists = saveable
.into_iter()
.map(|saveable| list_from_saveable(saveable, db))
.collect();
FileListList::from_lists(file_lists)
}
/**
Saves a `FileListList` to the specified file
*/
pub fn save_file_list_list(list: &[SaveableFileList], destination: &Path) -> Result<()> {
let mut file = fs::File::create(destination)?;
let as_json = serde_json::to_string(&list)?;
file.write_all(&as_json.into_bytes())?;
Ok(())
}
/**
Reads a `FileListList` from the specified file
*/
pub fn read_file_list_list(file: &Path, db: &file_database::FileDatabase) -> Result<FileListList> {
// Ensure that the file exists
if file.exists() {
let mut file = fs::File::open(file)?;
let mut json_string = String::new();
file.read_to_string(&mut json_string)?;
let saveable = serde_json::from_str(&json_string)?;
Ok(file_list_list_from_saveable(saveable, db))
}
else {
Ok(FileListList::new())
}
}
#[cfg(test)]
mod file_list_persistence_tests {
use super::*;
use changelog::ChangeCreationPolicy;
// Helpers
fn assert_lists_are_equal(list1: &FileList, list2: &FileList) {
for (original, read) in list1.get_files().iter().zip(list2.get_files().iter()) {
assert_eq!(list1.get_source(), list2.get_source());
assert_eq!(original, read);
}
}
fn dummy_database_list(db: &file_database::FileDatabase) -> FileList {
FileList::from_locations(
vec![
FileLocation::Database(db.add_new_file(
1,
"filename",
Some("thumbname"),
&vec![],
0,
&ChangeCreationPolicy::No
)),
FileLocation::Database(db.add_new_file(
2,
"filename",
Some("thumbname"),
&vec![],
0,
&ChangeCreationPolicy::No
)),
FileLocation::Unsaved(PathBuf::from("path")),
],
FileListSource::Folder(PathBuf::from("test/media")),
)
}
// Tests
#[test]
fn path_only_jsonification_test() {
let file_list = FileList::from_directory(PathBuf::from("test/media"), &PathBuf::from(""));
file_database::db_test_helpers::run_test(|db| {
let saveable = saveable_file_list(&file_list);
let decoded = list_from_saveable(saveable, db);
assert_eq!(file_list.get_source(), decoded.get_source());
for (original, read) in file_list.get_files().iter().zip(decoded.get_files().iter()) {
assert_eq!(original, read);
}
});
}
#[test]
fn tests_with_db() {
file_database::db_test_helpers::run_test(|db| {
let file_list = dummy_database_list(db);
let saveable = saveable_file_list(&file_list);
let decoded = list_from_saveable(saveable, db);
assert_lists_are_equal(&file_list, &decoded);
})
}
#[test]
fn file_list_list_test() {
file_database::db_test_helpers::run_test(|db| {
let file_lists = vec![
dummy_database_list(db),
FileList::from_directory(PathBuf::from("test/media"), &PathBuf::from("")),
];
let file_list_list = FileListList::from_lists(file_lists);
let saveable = saveable_file_list_list(&file_list_list);
let decoded = file_list_list_from_saveable(saveable, db);
for (original, decoded) in file_list_list
.get_lists()
.iter()
.zip(decoded.get_lists().iter())
{
assert_lists_are_equal(&original, &decoded)
}
})
}
#[test]
fn file_list_save_test() {
file_database::db_test_helpers::run_test(|db| {
let file_lists = vec![
dummy_database_list(db),
FileList::from_directory(PathBuf::from("test/media"), &PathBuf::from("")),
];
let file_list_list = FileListList::from_lists(file_lists);
let save_path = db.get_file_save_path()
.join(&PathBuf::from("persistent_file_list.json"));
save_file_list_list(&saveable_file_list_list(&file_list_list), &save_path).unwrap();
let decoded = read_file_list_list(&save_path, db).unwrap();
for (original, decoded) in file_list_list
.get_lists()
.iter()
.zip(decoded.get_lists().iter())
{
assert_lists_are_equal(&original, &decoded)
}
})
}
}