md_todo/
lib.rs

1use anyhow::Result;
2use error::MDTodoError;
3use std::path::Path;
4use todo::Todo;
5
6pub mod error;
7mod md_reader;
8mod md_writer;
9pub mod todo;
10
11/// Gets a Vector of all todos that can be found in markdown-files in directorys
12/// beneath the given Path.
13pub fn get_todos_from_path(dir: &dyn AsRef<Path>) -> Result<Vec<Todo>> {
14    if !dir.as_ref().is_dir() {
15        return Err(anyhow::Error::new(MDTodoError::NotADir));
16    }
17
18    md_reader::get_todos_from_dir(dir.as_ref())
19}
20
21/// Toggles the done state of the todo in the physical file.
22///
23/// This is done via finding the line of the todo and replacing `[ ]` with `[x]` or the other way.
24/// Before the change is done, its checked if the file changed since the last load of todos.
25/// If yes an error is returned.
26pub fn toggle_todo(todo: &mut Todo) -> Result<()> {
27    md_writer::toggle_todo(todo)
28}
29
30#[cfg(test)]
31mod tests {
32
33    use super::*;
34
35    #[test]
36    #[should_panic]
37    fn test_invalid_path() {
38        get_todos_from_path(&"thisPath/ShouldNotExist32/2/013".to_string()).unwrap();
39    }
40}