🔧 Code improvements

This commit is contained in:
Maddie H 2023-03-11 22:47:12 +00:00
parent 81d6fd91e5
commit 3d8b733b10
No known key found for this signature in database
GPG Key ID: 0C7CB348221D0AEF
2 changed files with 32 additions and 47 deletions

View File

@ -9,6 +9,7 @@ pub fn calc_row(task: &Task, id: usize) -> Row {
Row::from([ Row::from([
id.to_string().bright_black().italic(), id.to_string().bright_black().italic(),
task.status_string().bright_black().italic(), task.status_string().bright_black().italic(),
task.tags_string().bright_black().italic(),
task.title_string().bright_black().italic(), task.title_string().bright_black().italic(),
task.when_string().bright_black().italic(), task.when_string().bright_black().italic(),
task.deadline_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([ Row::from([
id.to_string().cyan(), id.to_string().cyan(),
task.status_string(), task.status_string(),
task.tags_string(),
task.title_string(), task.title_string(),
task.when_string(), task.when_string(),
task.deadline_string(), task.deadline_string(),
@ -31,6 +33,7 @@ pub fn tasks_table(tasks: &Tasks) -> Table {
table.set_titles(row![ table.set_titles(row![
"ID".magenta().bold(), "ID".magenta().bold(),
"Status".magenta().bold(), "Status".magenta().bold(),
"Tags".magenta().bold(),
"Title".magenta().bold(), "Title".magenta().bold(),
"When".magenta().bold(), "When".magenta().bold(),
"Deadline".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); table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
// Iterate through each task // 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)); table.add_row(calc_row(task, id));
} }

View File

@ -6,12 +6,12 @@ use serde::{Deserialize, Serialize};
pub struct TasksError(String); pub struct TasksError(String);
impl TasksError { impl TasksError {
pub fn no_task(id: usize) -> TasksError { pub fn no_task(id: usize) -> Self {
TasksError(format!("couldn't find task with id {}", id)) Self(format!("couldn't find task with id {}", id))
} }
pub fn no_tasks() -> TasksError { pub fn no_tasks() -> Self {
TasksError(String::from("no tasks available")) Self(String::from("no tasks available"))
} }
} }
@ -24,7 +24,7 @@ pub enum Status {
} }
impl Status { impl Status {
pub fn as_string(&self) -> ColoredString { pub fn as_colored_string(&self) -> ColoredString {
match self { match self {
Status::Inbox => "📮 Inbox".blue(), Status::Inbox => "📮 Inbox".blue(),
Status::Pending => "📅 Pending".yellow(), Status::Pending => "📅 Pending".yellow(),
@ -80,32 +80,18 @@ impl Task {
deadline: Option<NaiveDateTime>, deadline: Option<NaiveDateTime>,
reminder: Option<NaiveDateTime>, reminder: Option<NaiveDateTime>,
) { ) {
if let Some(title) = title { self.title = title.unwrap_or_else(|| self.title.clone());
self.title = title; 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 { if let Some(_when) = self.when {
self.notes = Some(notes);
};
if let Some(tags) = tags {
self.tags = Some(tags);
};
if let Some(when) = when {
self.when = Some(when);
if self.is_inbox() { if self.is_inbox() {
self.pend() self.status = Status::Pending;
}; }
}; }
if let Some(deadline) = deadline {
self.deadline = Some(deadline);
};
if let Some(reminder) = reminder {
self.reminder = Some(reminder);
};
} }
} }
@ -155,7 +141,7 @@ impl Task {
} }
impl Task { impl Task {
fn date_string(&self, date: &Option<NaiveDateTime>) -> ColoredString { fn date_colored_string(&self, date: &Option<NaiveDateTime>) -> ColoredString {
if let Some(date) = date { if let Some(date) = date {
let date = date.date(); let date = date.date();
let date_string = format!("{}", date.format("%Y-%m-%d")); let date_string = format!("{}", date.format("%Y-%m-%d"));
@ -178,15 +164,15 @@ impl Task {
} }
pub fn when_string(&self) -> ColoredString { pub fn when_string(&self) -> ColoredString {
self.date_string(&self.when) self.date_colored_string(&self.when)
} }
pub fn deadline_string(&self) -> ColoredString { pub fn deadline_string(&self) -> ColoredString {
self.date_string(&self.deadline) self.date_colored_string(&self.deadline)
} }
pub fn reminder_string(&self) -> ColoredString { pub fn reminder_string(&self) -> ColoredString {
self.date_string(&self.reminder) self.date_colored_string(&self.reminder)
} }
pub fn title_string(&self) -> ColoredString { pub fn title_string(&self) -> ColoredString {
@ -194,7 +180,7 @@ impl Task {
} }
pub fn status_string(&self) -> ColoredString { pub fn status_string(&self) -> ColoredString {
self.status.as_string() self.status.as_colored_string()
} }
pub fn tags_string(&self) -> ColoredString { pub fn tags_string(&self) -> ColoredString {
@ -218,7 +204,7 @@ impl Task {
pub struct Tasks { pub struct Tasks {
pub path: String, // Path to the tasks repository pub path: String, // Path to the tasks repository
pub file: String, // Path to the tasks file in the repository pub file: String, // Path to the tasks file in the repository
pub tasks: Option<Vec<Task>>, // All the tasks in one vector pub tasks: Vec<Task>, // All the tasks in one vector
} }
impl Tasks { impl Tasks {
@ -226,7 +212,7 @@ impl Tasks {
Self { Self {
path: String::from(repo_path), path: String::from(repo_path),
file: String::from(tasks_file), file: String::from(tasks_file),
tasks: None, tasks: Vec::new(),
} }
} }
} }
@ -247,7 +233,7 @@ impl Tasks {
if self.is_empty() { if self.is_empty() {
Err(TasksError::no_tasks()) Err(TasksError::no_tasks())
} else if self.exists(id) { } else if self.exists(id) {
Ok(&mut self.tasks.as_mut().unwrap()[id]) Ok(&mut self.tasks[id])
} else { } else {
Err(TasksError::no_task(id)) Err(TasksError::no_task(id))
} }
@ -257,15 +243,15 @@ impl Tasks {
impl Tasks { impl Tasks {
pub fn push(&mut self, task: Task) { pub fn push(&mut self, task: Task) {
if self.is_empty() { if self.is_empty() {
self.tasks = Some(vec![task]); self.tasks = vec![task];
} else { } else {
self.tasks.as_mut().unwrap().push(task); self.tasks.push(task);
}; };
} }
pub fn remove(&mut self, id: usize) -> Result<(), TasksError> { pub fn remove(&mut self, id: usize) -> Result<(), TasksError> {
if self.exists(id) { if self.exists(id) {
self.tasks.as_mut().unwrap().remove(id); self.tasks.remove(id);
Ok(()) Ok(())
} else { } else {
Err(TasksError::no_task(id)) Err(TasksError::no_task(id))
@ -273,18 +259,14 @@ impl Tasks {
} }
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
if self.tasks.is_none() { self.tasks.len()
0
} else {
self.tasks.as_ref().unwrap().len()
}
} }
pub fn clear(&mut self) -> Result<(), TasksError> { pub fn clear(&mut self) -> Result<(), TasksError> {
if self.is_empty() { if self.is_empty() {
Err(TasksError::no_tasks()) Err(TasksError::no_tasks())
} else { } else {
self.tasks = None; self.tasks = Vec::new();
Ok(()) Ok(())
} }
} }