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
|
//! Data structures, read and write logic for the todo list
use mingling::{
macros::{r_println, renderer},
Groupped,
};
use serde::{Deserialize, Serialize};
use std::{env::current_dir, path::PathBuf};
use crate::ResProgramFlags;
#[derive(Debug, Serialize, Deserialize, Clone, Default, Groupped)]
pub struct ResTodoList {
pub items: Vec<Todo>,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Todo {
pub item: String,
pub completed: bool,
}
fn get_todo_list_path() -> PathBuf {
current_dir().unwrap().join(".todo.json")
}
pub fn read_todo_list() -> ResTodoList {
let path = get_todo_list_path();
if !path.exists() {
return ResTodoList::default();
}
let file = match std::fs::File::open(&path) {
Ok(f) => f,
Err(_) => return ResTodoList::default(),
};
let reader = std::io::BufReader::new(file);
serde_json::from_reader(reader).unwrap_or_default()
}
#[renderer]
pub fn render_res_todo_list(todo_list: ResTodoList, program_flags: &ResProgramFlags) {
let mut idx = 0;
r_println!("TODO: ");
for item in &todo_list.items {
if item.completed && !program_flags.all {
idx += 1;
continue;
}
r_println!(
" {idx}. [{}] - \"{}\"",
if item.completed { "x" } else { " " },
item.item
);
idx += 1;
}
}
pub fn write_todo_list(todo_list: ResTodoList) {
let path = get_todo_list_path();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).unwrap();
}
let file = std::fs::File::create(path).unwrap();
let writer = std::io::BufWriter::new(file);
serde_json::to_writer(writer, &todo_list).unwrap();
}
|