/
lib.rs
376 lines (322 loc) · 11.5 KB
/
lib.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use console::style;
use rusqlite::{Connection, Result};
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
#[derive(Debug)]
pub struct Todo {
pub id: i32,
pub name: String,
pub date_added: String, // Using f64 for timestamp
pub is_done: i32,
}
impl Todo {
// Constructor for a new Todo instance
pub fn new(id: i32, name: String, date_added: String, is_done: i32) -> Self {
Todo {
id,
name,
date_added,
is_done,
}
}
// Add a new todo to the database
pub fn add(conn: &Connection, name: &str) -> Result<()> {
conn.execute("INSERT INTO todo (name) VALUES (?)", &[name])?;
Ok(())
}
// List all todo entries in the database
pub fn list(conn: &Connection, sort_by_status: bool) -> Result<Vec<Todo>> {
let sql = if sort_by_status {
"SELECT * FROM todo ORDER BY is_done, id"
} else {
"SELECT * FROM todo ORDER BY id"
};
let mut stmt = conn.prepare(sql)?;
let todo_iter = stmt.query_map((), |row| {
Ok(Todo::new(
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
))
})?;
let mut todos = Vec::new();
for todo in todo_iter {
todos.push(todo?);
}
Ok(todos)
}
// Toggle the 'is_done' property of a Todo
pub fn toggle(conn: &Connection, id: i32) -> Result<()> {
conn.execute("UPDATE todo SET is_done = 1 - is_done WHERE id = ?", &[&id])?;
Ok(())
}
// Reset the database, clearing all entries
pub fn reset(conn: &Connection) -> Result<()> {
conn.execute("DELETE FROM todo", ())?;
Ok(())
}
// Removes a task
pub fn rm(conn: &Connection, id: i32) -> Result<()> {
conn.execute("DELETE FROM todo WHERE id = ?", &[&id])?;
Ok(())
}
// Prints a list of todos objects
pub fn print_list(todos: Vec<Todo>) -> Result<()> {
for todo in todos {
// Styles the string representing the status
let status = if todo.is_done == 1 {
style("Done").green()
} else {
style("Pending").red()
};
println!(
"{:>4} | {:<44} {:<8} {}",
style(todo.id).cyan().bright(),
style(truncate_at(&todo.name, 44)).bright(),
status,
style(todo.date_added).dim(),
);
}
Ok(())
}
}
// Truncates an str and adds ellipsis if needed
pub fn truncate_at(input: &str, max: i32) -> String {
let max_len: usize = max as usize;
if input.len() > max_len {
let truncated = &input[..(max_len - 3)];
return format!("{}...", truncated);
};
input.to_string()
}
// Returns a connection, creating the database if needed
pub fn get_connection() -> Result<Connection> {
let db_folder = get_home() + "/" + "todo_db/";
let db_file_path = db_folder.clone() + "todo.sqlite";
verify_db_path(&db_folder)?;
let conn = Connection::open(db_file_path)?;
verify_db(&conn)?;
Ok(conn)
}
// Aux function that creates tables if they don't exist
pub fn verify_db(conn: &Connection) -> Result<()> {
conn.execute(
"CREATE TABLE IF NOT EXISTS todo (
id INTEGER NOT NULL,
name TEXT NOT NULL,
date_added REAL NOT NULL DEFAULT current_timestamp,
is_done NUMERIC NOT NULL DEFAULT 0,
PRIMARY KEY(id AUTOINCREMENT)
)",
[], // no params for this query
)?;
Ok(())
}
// Aux function that creates the folder where the DB should be stored
// if it doesn't exist
pub fn verify_db_path(db_folder: &str) -> Result<()> {
if !Path::new(db_folder).exists() {
// Check if the folder doesn't exist
match fs::create_dir(db_folder) {
Ok(_) => println!("Folder '{}' created.", db_folder),
Err(e) => eprintln!("Error creating folder: {}", e),
}
}
Ok(())
}
// Prints help with a list of commands and parameters
pub fn help() -> Result<()> {
let help_title = "\nAvailable commands:";
let help_text = r#"
- add [TASK]
Ads new task/s
Example: todo add "Build a tree"
- list
Lists all tasks
Example: todo list
- toggle [ID]
Toggles the status of a task (Done/Pending)
Example: todo toggle 2
- rm [ID]
Removes a task
Example: todo rm 4
- sort
Sorts completed and uncompleted tasks
- reset
Deletes all tasks
"#;
println!("{}", style(help_title).cyan().bright());
println!("{}", style(help_text).green());
Ok(())
}
// Get the user's home directory for each platform
fn get_home() -> String {
let home_dir = match env::var("HOME") {
Ok(path) => PathBuf::from(path),
Err(_) => {
// Fallback for Windows and macOS
if cfg!(target_os = "windows") {
if let Some(userprofile) = env::var("USERPROFILE").ok() {
PathBuf::from(userprofile)
} else if let Some(homedrive) = env::var("HOMEDRIVE").ok() {
let homepath = env::var("HOMEPATH").unwrap_or("".to_string());
PathBuf::from(format!("{}{}", homedrive, homepath))
} else {
panic!("Could not determine the user's home directory.");
}
} else if cfg!(target_os = "macos") {
let home = env::var("HOME").unwrap_or("".to_string());
PathBuf::from(home)
} else {
panic!("Could not determine the user's home directory.");
}
}
};
// Convert the PathBuf to a &str
match home_dir.to_str() {
Some(home_str) => home_str.to_string(),
None => panic!("Failed to convert home directory to a string."),
}
}
/* -----------------------------------------------------------
* Tests
* ---------------------------------------------------------*/
#[cfg(test)]
mod tests {
use super::*;
use lazy_static::lazy_static;
use std::sync::Mutex;
// Creates a persistant in memory db connection
// creates tables if necessary
lazy_static! {
static ref DATABASE_CONNECTION: Mutex<Connection> = {
let conn = Connection::open_in_memory().expect("Failed to create in-memory database");
verify_db(&conn).expect("Cannot create tables");
Mutex::new(conn)
};
}
fn reset_db(conn: &Connection) -> Result<()> {
conn.execute("DELETE FROM todo", ())?;
Ok(())
}
fn contains_task(todos: &Vec<Todo>, target_name: &str) -> bool {
for todo in todos {
if todo.name == target_name {
return true;
}
}
false
}
#[test]
fn test_add_todo() {
let conn = DATABASE_CONNECTION.lock().expect("Mutex lock failed");
reset_db(&conn).expect("Messed up resetting the db");
// Call the add function to add a todo
let name = "Test Todo";
Todo::add(&conn, name).expect("Failed to add todo");
// Query the database to check if the todo was added
let mut stmt = conn
.prepare("SELECT COUNT(*) FROM todo WHERE name = ?")
.expect("Failed to prepare statement");
let count: i32 = stmt
.query_row(&[name], |row| row.get(0))
.expect("Failed to query database");
assert_eq!(count, 1, "Todo was not added to the database");
}
#[test]
fn test_list_todo() {
let conn = DATABASE_CONNECTION.lock().expect("Mutex lock failed");
reset_db(&conn).expect("Messed up resetting the db");
Todo::add(&conn, "Task 1").expect("Could not add todo");
Todo::add(&conn, "Task 2").expect("Could not add todo");
Todo::add(&conn, "Task 3").expect("Could not add todo");
let todos = Todo::list(&conn, false).expect("Failed to list todo");
assert_eq!(
todos.len(),
3,
"Wrong number of todo items returned by list()"
);
}
#[test]
fn test_sort_todo() {
let conn = DATABASE_CONNECTION.lock().expect("Mutex lock failed");
reset_db(&conn).expect("Messed up resetting the db");
Todo::add(&conn, "Task 1").expect("Could not add todo");
Todo::add(&conn, "Task 2").expect("Could not add todo");
Todo::add(&conn, "Task 3").expect("Could not add todo");
let todos = Todo::list(&conn, false).expect("Failed to list todo");
// toggles the first entry
Todo::toggle(&conn, todos[0].id).expect("Could not toggle first todo");
// true means sorted by status
let todos = Todo::list(&conn, true).expect("Failed to sort todos");
assert_eq!(
todos[2].name, "Task 1",
"The todo marked as done was not the LAST one returned"
);
assert_eq!(
todos.len(),
3,
"Wrong number of todo items returned by sort()"
);
}
#[test]
fn test_rm_todo() {
let conn = DATABASE_CONNECTION.lock().expect("Mutex lock failed");
reset_db(&conn).expect("Messed up resetting the db");
Todo::add(&conn, "Task 1").expect("Could not add todo");
Todo::add(&conn, "Task 2").expect("Could not add todo");
Todo::add(&conn, "Task 3").expect("Could not add todo");
let todos = Todo::list(&conn, false).expect("Failed to list todo");
// toggles the first entry
Todo::rm(&conn, todos[0].id).expect("Could not remove first todo");
// true means sorted by status
let todos = Todo::list(&conn, false).expect("Failed to sort todos");
dbg!(&todos);
assert_eq!(
todos.len(),
2,
"Wrong number of todo items returned by sort()"
);
assert_eq!(
contains_task(&todos, "Task 1"),
false,
"Task 1 was not deleted!"
);
}
#[test]
fn test_toggle_todo() {
let conn = DATABASE_CONNECTION.lock().expect("Mutex lock failed");
reset_db(&conn).expect("Messed up resetting the db");
Todo::add(&conn, "Task 1").expect("Could not add todo");
Todo::add(&conn, "Task 2").expect("Could not add todo");
let todos = Todo::list(&conn, false).expect("Failed to list todo");
// toggles the first entry
Todo::toggle(&conn, todos[0].id).expect("Could not toggle first todo");
// true means sorted by status
let todos = Todo::list(&conn, false).expect("Failed to sort todos");
dbg!(&todos);
assert_eq!(
todos.len(),
2,
"Wrong number of todo items returned by toggle()"
);
// True and False are stored as 0 or 1 in the db
assert_eq!(todos[0].is_done, 1, "Task 1 was not toggled!");
}
#[test]
fn test_reset_todo() {
let conn = DATABASE_CONNECTION.lock().expect("Mutex lock failed");
Todo::add(&conn, "Some task").expect("Could not add todo");
Todo::reset(&conn).expect("Messed up resetting the db");
let todos = Todo::list(&conn, false).expect("Failed to list todo");
assert_eq!(todos.len(), 0, "There are still todos left after reset()");
assert_eq!(
contains_task(&todos, "Task 1"),
false,
"Task 1 was not deleted!"
);
}
}