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
use json::parse;
use std::fs::File;
use std::io::Read;
use list::TodoList;
use todo::Todo;
use error::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tdo {
pub lists: Vec<TodoList>,
version: String,
}
impl Tdo {
pub fn new() -> Tdo {
Tdo {
lists: vec![TodoList::default()],
version: env!("CARGO_PKG_VERSION").to_string(),
}
}
pub fn load(path: &str) -> TdoResult<Tdo> {
match File::open(path) {
Ok(file) => {
match super::serde_json::from_reader(&file) {
Ok(tdo) => Ok(tdo),
Err(_) => update_json(path),
}
}
Err(_) => Err(ErrorKind::StorageError(storage_error::ErrorKind::FileNotFound).into()),
}
}
pub fn save(&self, path: &str) -> TdoResult<()> {
match File::create(path) {
Ok(mut f) => {
let _ = super::serde_json::to_writer_pretty(&mut f, self);
Ok(())
}
Err(_) => Err(ErrorKind::StorageError(storage_error::ErrorKind::SaveFailure).into()),
}
}
pub fn add_list(&mut self, list: TodoList) -> TdoResult<()> {
match self.get_list_index(&list.name) {
Ok(_) => Err(ErrorKind::TodoError(todo_error::ErrorKind::NameAlreadyExists).into()),
Err(_) => {
self.lists.push(list);
Ok(())
}
}
}
pub fn remove_list(&mut self, list_name: &str) -> TdoResult<()> {
if list_name == "default" {
Err(ErrorKind::TodoError(todo_error::ErrorKind::CanNotRemoveDefault).into())
} else {
match self.get_list_index(list_name) {
Ok(index) => {
self.lists.remove(index);
Ok(())
}
Err(_) => Err(ErrorKind::TodoError(todo_error::ErrorKind::NoSuchList).into()),
}
}
}
pub fn add_todo(&mut self, list_name: Option<&str>, todo: Todo) -> TdoResult<()> {
match self.get_list_index(&list_name.unwrap_or("default")) {
Ok(index) => {
self.lists[index].add(todo);
Ok(())
}
Err(x) => Err(x),
}
}
pub fn find_id(&self, id: u32) -> TdoResult<usize> {
for list in 0..self.lists.len() {
if self.lists[list].contains_id(id).is_ok() {
return Ok(list);
}
}
Err(ErrorKind::TodoError(todo_error::ErrorKind::NotInList).into())
}
pub fn done_id(&mut self, id: u32) -> TdoResult<()> {
let list = match self.find_id(id) {
Ok(list_id) => list_id,
Err(e) => return Err(e),
};
self.lists[list].done_id(id)
}
pub fn remove_id(&mut self, id: u32) -> TdoResult<()> {
let list = match self.find_id(id) {
Ok(list_id) => list_id,
Err(e) => return Err(e),
};
match self.lists[list].remove_id(id) {
Err(e) => Err(e),
_ => Ok(()),
}
}
pub fn clean_lists(&mut self) {
for list in 0..self.lists.len() {
self.lists[list].clean();
}
}
fn get_list_index(&self, name: &str) -> TdoResult<usize> {
match self.lists
.iter()
.position(|x| x.name.to_lowercase() == name.to_string().to_lowercase()) {
Some(index) => Ok(index),
None => Err(ErrorKind::TodoError(todo_error::ErrorKind::NoSuchList).into()),
}
}
pub fn get_highest_id(&self) -> u32 {
self.lists.iter().fold(0, |acc, &ref x| {
x.list.iter().fold(acc,
|inner_acc, &ref y| if inner_acc < y.id { y.id } else { inner_acc })
})
}
}
fn update_json(path: &str) -> TdoResult<Tdo> {
let mut file = File::open(path).unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();
let mut json = match parse(&data) {
Ok(content) => content,
Err(_) => return Err(ErrorKind::StorageError(storage_error::ErrorKind::FileCorrupted).into()),
};
let mut lists: Vec<TodoList> = vec![];
for outer in json.entries_mut() {
let mut list = TodoList::new(outer.0);
for inner in outer.1.entries_mut() {
let tdo_id = match inner.0.parse::<u32>() {
Ok(id) => id,
Err(_) => return Err(ErrorKind::StorageError(storage_error::ErrorKind::UnableToConvert).into()),
};
let done = match inner.1.pop().as_bool() {
Some(x) => x,
None => return Err(ErrorKind::StorageError(storage_error::ErrorKind::UnableToConvert).into()),
};
let tdo_name = match inner.1.pop().as_str() {
Some(x) => String::from(x),
None => return Err(ErrorKind::StorageError(storage_error::ErrorKind::UnableToConvert).into()),
};
let mut todo = Todo::new(tdo_id, &tdo_name);
if done {
todo.set_done();
}
list.add(todo);
}
lists.push(list);
}
let tdo = Tdo {
lists: lists,
version: env!("CARGO_PKG_VERSION").to_string(),
};
Ok(tdo)
}