pub struct TodoFile {
pub path: PathBuf,
pub todos: Vec<Todo>,
pub content: String,
}
Expand description
A struct that represents a todo.txt file.
This struct doesn’t actually represent a file on disk, but rather a collection of todos.
Any actions on the struct do not affect the file on disk, you have to call the save
method to
save the changes.
Fields§
§path: PathBuf
The path to the file on disk.
todos: Vec<Todo>
A vector of todo items.
content: String
The content of the file as a string.
Implementations§
Source§impl TodoFile
impl TodoFile
Sourcepub fn new(path: &str) -> Self
pub fn new(path: &str) -> Self
Reads off a path to a file and returns a TodoFile
struct.
If the file doesn’t exist, an empty TodoFile
struct is returned implicitly.
For more granular control flow of the file, you can use the from_path method.
Sourcepub fn from_path(path: &Path) -> Result<Self, Error>
pub fn from_path(path: &Path) -> Result<Self, Error>
Reads off a path to a file and returns a TodoFile
struct.
If the file doesn’t exist, it returns an IO error.
This method is useful when you want to handle the error explicitly.
Otherwise, you can use the new
method.
Sourcepub fn from_string(content: &str) -> Self
pub fn from_string(content: &str) -> Self
Reads off a string and returns a TodoFile
struct.
This method skips the file reading and directly parses the string into a TodoFile
struct.
Might be useful if you store the files in a database or some other storage.
Sourcepub fn load(&mut self)
pub fn load(&mut self)
The main function that parses each line of the file and stores it in the todos
vector.
The line is determined by the Line
struct as defined by rust’s standard library.
Each line is passed into the parse method of the Todo
struct, which returns a Result
.
Any errors are ignored and the loop continues.
Sourcepub fn save(&self)
pub fn save(&self)
Saves the TodoFile
struct to the file on disk.
The path has to be set before calling this method.
Alternatively, you can use the save_as
method to save the file to a different path.
The todo’s are formatted using the Display
trait and written to the file.
Sourcepub fn save_as(&self, path: &str)
pub fn save_as(&self, path: &str)
Saves the TodoFile
struct to a different file on disk.
Works the same as the save
method, but you can specify a different path.
Sourcepub fn change_status(&mut self, index: usize)
pub fn change_status(&mut self, index: usize)
Changes the status of a todo item.
The index is the index of the todo item in the todos
vector.
The status is toggled between completed and not completed.
Sourcepub fn update(&mut self, index: usize, todo: Todo)
pub fn update(&mut self, index: usize, todo: Todo)
Updates a todo item in the todos
vector.
Doesn’t do anything if the index is out of bounds.
Sourcepub fn get(&self, index: usize) -> Option<Todo>
pub fn get(&self, index: usize) -> Option<Todo>
Gets a todo item from the todos
vector.
Returns None if the index is out of bounds.
This can be seen as a safe way to access the todos
vector.
However, the [] operator is also implemented for the TodoFile
struct.
Sourcepub fn rearrange(&mut self) -> Vec<Todo>
pub fn rearrange(&mut self) -> Vec<Todo>
Rearranges the todo items into a new vector. The rearrangement is done by sorting the todo items sequentially in the following order:
- Not completed todo items sorted by creation date.
- Completed todo items sorted by completion date.
- Merged list of the two categories.
This behavior is inspired by the todo.txt cli application.
Sourcepub fn search(&self, query: &str) -> Vec<Todo>
pub fn search(&self, query: &str) -> Vec<Todo>
Searches for a query in the todo items.
The query is a string that is searched in the content
field of the todo items.
This search happens in a case-sensitive manner and takes O(n) time in the worst case.
Sourcepub fn regex(&self, query: &str) -> Vec<Todo>
pub fn regex(&self, query: &str) -> Vec<Todo>
Searches for a query in the todo items using a regex.
Regex can be seen as significantly faster than the search
method.
Alternatively, helper methods like get_project
and get_context
can be used to
simplify the search.
Sourcepub fn get_project(&self, project: &str) -> Vec<Todo>
pub fn get_project(&self, project: &str) -> Vec<Todo>
Gets all the todo items that have a specific project.
Uses a combination of regex and the parser::Todo
struct to get the project.
Sourcepub fn get_context(&self, context: &str) -> Vec<Todo>
pub fn get_context(&self, context: &str) -> Vec<Todo>
Gets all the todo items that have a specific context.
Works similarly to the get_project
method.
Sourcepub fn list_projects(&self) -> Vec<String>
pub fn list_projects(&self) -> Vec<String>
Lists all of the projects in the todo items in a sorted and deduplicated manner.
Sourcepub fn list_contexts(&self) -> Vec<String>
pub fn list_contexts(&self) -> Vec<String>
Lists all of the contexts in the todo items in a sorted and deduplicated manner.
Lists all of the tags in the todo items in a sorted and deduplicated manner.
Experimental: Lists all of the hashtags in the todo items in a sorted and deduplicated manner.
Sourcepub fn completed(&self) -> Vec<Todo>
pub fn completed(&self) -> Vec<Todo>
Returns a vector of all the todo items that are completed.
Sourcepub fn not_completed(&self) -> Vec<Todo>
pub fn not_completed(&self) -> Vec<Todo>
Returns a vector of all the todo items that are not completed.
Sourcepub fn due_today(&self) -> Vec<Todo>
pub fn due_today(&self) -> Vec<Todo>
Returns a vector of all the todo items that are due today. Uses the chrono library to get the current date and compares it with the due date of the todo item.