From 3d8b733b10d50add7fc6f17d0a6ea8e4f4a0b642 Mon Sep 17 00:00:00 2001 From: Madeleine Date: Sat, 11 Mar 2023 22:47:12 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Code=20improvements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli/tables.rs | 5 +++- src/tasks.rs | 74 ++++++++++++++++++----------------------------- 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/src/cli/tables.rs b/src/cli/tables.rs index 6b6ad17..4e7d9a1 100644 --- a/src/cli/tables.rs +++ b/src/cli/tables.rs @@ -9,6 +9,7 @@ pub fn calc_row(task: &Task, id: usize) -> Row { Row::from([ id.to_string().bright_black().italic(), task.status_string().bright_black().italic(), + task.tags_string().bright_black().italic(), task.title_string().bright_black().italic(), task.when_string().bright_black().italic(), task.deadline_string().bright_black().italic(), @@ -18,6 +19,7 @@ pub fn calc_row(task: &Task, id: usize) -> Row { Row::from([ id.to_string().cyan(), task.status_string(), + task.tags_string(), task.title_string(), task.when_string(), task.deadline_string(), @@ -31,6 +33,7 @@ pub fn tasks_table(tasks: &Tasks) -> Table { table.set_titles(row![ "ID".magenta().bold(), "Status".magenta().bold(), + "Tags".magenta().bold(), "Title".magenta().bold(), "When".magenta().bold(), "Deadline".magenta().bold(), @@ -38,7 +41,7 @@ pub fn tasks_table(tasks: &Tasks) -> Table { table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); // Iterate through each task - for (id, task) in tasks.tasks.as_ref().unwrap().iter().enumerate() { + for (id, task) in tasks.tasks.iter().enumerate() { table.add_row(calc_row(task, id)); } diff --git a/src/tasks.rs b/src/tasks.rs index 9d7e609..24d67e6 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -6,12 +6,12 @@ use serde::{Deserialize, Serialize}; pub struct TasksError(String); impl TasksError { - pub fn no_task(id: usize) -> TasksError { - TasksError(format!("couldn't find task with id {}", id)) + pub fn no_task(id: usize) -> Self { + Self(format!("couldn't find task with id {}", id)) } - pub fn no_tasks() -> TasksError { - TasksError(String::from("no tasks available")) + pub fn no_tasks() -> Self { + Self(String::from("no tasks available")) } } @@ -24,7 +24,7 @@ pub enum Status { } impl Status { - pub fn as_string(&self) -> ColoredString { + pub fn as_colored_string(&self) -> ColoredString { match self { Status::Inbox => "📮 Inbox".blue(), Status::Pending => "📅 Pending".yellow(), @@ -80,32 +80,18 @@ impl Task { deadline: Option, reminder: Option, ) { - if let Some(title) = title { - self.title = title; - }; + self.title = title.unwrap_or_else(|| self.title.clone()); + self.notes = notes.or_else(|| self.notes.take()); + self.tags = tags.or_else(|| self.tags.take()); + self.when = when.or_else(|| self.when.take()); + self.deadline = deadline.or_else(|| self.deadline.take()); + self.reminder = reminder.or_else(|| self.reminder.take()); - if let Some(notes) = notes { - self.notes = Some(notes); - }; - - if let Some(tags) = tags { - self.tags = Some(tags); - }; - - if let Some(when) = when { - self.when = Some(when); + if let Some(_when) = self.when { if self.is_inbox() { - self.pend() - }; - }; - - if let Some(deadline) = deadline { - self.deadline = Some(deadline); - }; - - if let Some(reminder) = reminder { - self.reminder = Some(reminder); - }; + self.status = Status::Pending; + } + } } } @@ -155,7 +141,7 @@ impl Task { } impl Task { - fn date_string(&self, date: &Option) -> ColoredString { + fn date_colored_string(&self, date: &Option) -> ColoredString { if let Some(date) = date { let date = date.date(); let date_string = format!("{}", date.format("%Y-%m-%d")); @@ -178,15 +164,15 @@ impl Task { } pub fn when_string(&self) -> ColoredString { - self.date_string(&self.when) + self.date_colored_string(&self.when) } pub fn deadline_string(&self) -> ColoredString { - self.date_string(&self.deadline) + self.date_colored_string(&self.deadline) } pub fn reminder_string(&self) -> ColoredString { - self.date_string(&self.reminder) + self.date_colored_string(&self.reminder) } pub fn title_string(&self) -> ColoredString { @@ -194,7 +180,7 @@ impl Task { } pub fn status_string(&self) -> ColoredString { - self.status.as_string() + self.status.as_colored_string() } pub fn tags_string(&self) -> ColoredString { @@ -218,7 +204,7 @@ impl Task { pub struct Tasks { pub path: String, // Path to the tasks repository pub file: String, // Path to the tasks file in the repository - pub tasks: Option>, // All the tasks in one vector + pub tasks: Vec, // All the tasks in one vector } impl Tasks { @@ -226,7 +212,7 @@ impl Tasks { Self { path: String::from(repo_path), file: String::from(tasks_file), - tasks: None, + tasks: Vec::new(), } } } @@ -247,7 +233,7 @@ impl Tasks { if self.is_empty() { Err(TasksError::no_tasks()) } else if self.exists(id) { - Ok(&mut self.tasks.as_mut().unwrap()[id]) + Ok(&mut self.tasks[id]) } else { Err(TasksError::no_task(id)) } @@ -257,15 +243,15 @@ impl Tasks { impl Tasks { pub fn push(&mut self, task: Task) { if self.is_empty() { - self.tasks = Some(vec![task]); + self.tasks = vec![task]; } else { - self.tasks.as_mut().unwrap().push(task); + self.tasks.push(task); }; } pub fn remove(&mut self, id: usize) -> Result<(), TasksError> { if self.exists(id) { - self.tasks.as_mut().unwrap().remove(id); + self.tasks.remove(id); Ok(()) } else { Err(TasksError::no_task(id)) @@ -273,18 +259,14 @@ impl Tasks { } pub fn len(&self) -> usize { - if self.tasks.is_none() { - 0 - } else { - self.tasks.as_ref().unwrap().len() - } + self.tasks.len() } pub fn clear(&mut self) -> Result<(), TasksError> { if self.is_empty() { Err(TasksError::no_tasks()) } else { - self.tasks = None; + self.tasks = Vec::new(); Ok(()) } }