Struct TodoFile

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn remove(&mut self, index: usize)

Removes a todo item from the todos vector.

Source

pub fn add(&mut self, todo: Todo)

Adds a todo item to the todos vector.

Source

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.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of todo items in the todos vector.

Source

pub fn is_empty(&self) -> bool

Returns whether the todos vector is empty or not.

Source

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:

  1. Not completed todo items sorted by creation date.
  2. Completed todo items sorted by completion date.
  3. Merged list of the two categories.

This behavior is inspired by the todo.txt cli application.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn list_projects(&self) -> Vec<String>

Lists all of the projects in the todo items in a sorted and deduplicated manner.

Source

pub fn list_contexts(&self) -> Vec<String>

Lists all of the contexts in the todo items in a sorted and deduplicated manner.

Source

pub fn list_tags(&self) -> Vec<String>

Lists all of the tags in the todo items in a sorted and deduplicated manner.

Source

pub fn list_hashtags(&self) -> Vec<String>

Experimental: Lists all of the hashtags in the todo items in a sorted and deduplicated manner.

Source

pub fn completed(&self) -> Vec<Todo>

Returns a vector of all the todo items that are completed.

Source

pub fn not_completed(&self) -> Vec<Todo>

Returns a vector of all the todo items that are not completed.

Source

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.

Source

pub fn due_on(&self, date: NaiveDate) -> Vec<Todo>

Returns a vector of all the todo items that are due tomorrow. Similar to the due_today method, but the date can be specified.

Source

pub fn as_json(&self) -> Value

Returns the file as a json of parsed todo items.

Source

pub fn from_json(path: &Path, json: Value) -> Self

Parses a json value and returns a TodoFile struct.

Trait Implementations§

Source§

impl Display for TodoFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Index<usize> for TodoFile

Source§

type Output = Todo

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for TodoFile

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IntoIterator for TodoFile

Source§

type Item = Todo

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<TodoFile as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.