use std::fmt::{Display, Formatter, Result};
use std::path::PathBuf;
use crate::md_writer;
#[derive(Debug, Default)]
pub struct Todo {
pub name: String,
pub filename: String,
pub line_no: usize,
pub done: bool,
pub filepath: PathBuf,
pub file_md5: Vec<u8>,
pub headings: Vec<String>,
}
impl Todo {
pub fn new(
name: &str,
filename: &str,
line_no: usize,
done: bool,
filepath: PathBuf,
headings: Vec<String>,
file_md5: Vec<u8>,
) -> Todo {
Todo {
name: name.to_string(),
filename: filename.to_string(),
line_no,
done,
filepath,
headings,
file_md5,
}
}
pub fn toggle(&mut self) {
md_writer::toggle_todo(self).unwrap();
}
}
impl Display for Todo {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let mut todo_state = "[ ]";
if self.done {
todo_state = "[x]"
}
write!(f, "{} {}", todo_state, self.name)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn get_simple_todo() -> Todo {
Todo::new(
"A todo",
"SomeFile.md",
33,
false,
PathBuf::default(),
vec!["# One".to_string(), "# Two".to_string()],
vec![1, 2, 3],
)
}
#[test]
fn test_creation_with_new() {
let todo = get_simple_todo();
assert_eq!(todo.name, "A todo");
assert_eq!(todo.filename, "SomeFile.md");
assert_eq!(todo.line_no, 33);
assert!(!todo.done);
assert_eq!(todo.filepath, PathBuf::default());
assert_eq!(todo.headings[1], "# Two");
assert_eq!(todo.file_md5[2], 3);
}
#[test]
fn test_display() {
let mut todo = get_simple_todo();
assert_eq!(format!("{}", todo), "[ ] A todo");
todo.done = true;
assert_eq!(format!("{}", todo), "[x] A todo");
}
}