From 576f88307789bb8bf2e7b26e4e605f1c19f18fe7 Mon Sep 17 00:00:00 2001 From: The Chef Date: Thu, 17 Apr 2025 01:34:15 +0000 Subject: [PATCH] Update src/main.rs --- src/main.rs | 127 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 49 deletions(-) diff --git a/src/main.rs b/src/main.rs index a113ebd..f4aca3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,6 @@ -use std::fmt; use json::JsonValue; use clap::Parser; use std::fs; -// #[macro_use] extern crate prettytable; -// use prettytable::{Table, Row, Cell}; use prettytable::Table; use prettytable::row; @@ -31,34 +28,6 @@ struct Args { } -// create a struct of a task -#[allow(dead_code)] -struct Task<'a> { - id: u64, - name: &'a str, - done: bool, - category: &'a str -} - -// implement print for Task -impl fmt::Display for Task<'_>{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if self.category == "" { - if self.done { - write!(f, "done | {} | {}", self.id, self.name) - } else { - write!(f, "not-done | {} | {}", self.id, self.name) - } - } else { - if self.done { - write!(f, "done | {} | {} | {}", self.id, self.name, self.category) - } else { - write!(f, "not-done | {} | {} | {}", self.id, self.name, self.category) - } - } - } -} - fn main() { // parse arguments let args = Args::parse(); @@ -69,9 +38,9 @@ fn main() { // main data file name let main_file: String = "data.json".to_owned(); - let current_string: String = fs::read_to_string(main_file) + let current_string: String = fs::read_to_string(&main_file) .expect("File could not be found."); - let current_data: &JsonValue = &json::parse(¤t_string).unwrap()["data"]; + let current_data: &mut JsonValue = &mut json::parse(¤t_string).unwrap()["data"]; // for list commands if args.command == "list" { @@ -181,39 +150,99 @@ fn main() { // for add command else if args.command == "add" { // todo add --category _category_ _task_ + // variable to hold category string + let mut category_: String = "default".to_owned(); if args.category != None { - let category_: String = args.category.to_owned().unwrap(); - let taskname: String = args.mainvalue; - println!("You are trying to add a task \"{}\" to category {}", taskname, category_); + // if category is provided, change default to provided category + category_ = args.category.to_owned().unwrap(); } // todo add _task_ - else { - let taskname: String = args.mainvalue; - println!("You are trying to add a (uncategorized) task! \"{}\"", taskname); + let mut next_task_id: u32 = 1; + for index_ in 0..current_data.len() { + let task_id_string: String = json::stringify(current_data[index_]["task_id"].clone()); + let current_task_id: u32 = task_id_string.parse().unwrap(); + if current_task_id >= next_task_id { + next_task_id += 1; + } + } + let taskname: String = args.mainvalue; + let new_data_string: String = format!("{{\"task_id\": {}, \"task_name\": \"{}\", \"category\": \"{}\", \"done\": {}}}", next_task_id, taskname, category_, false); + let new_data: JsonValue = json::parse(&new_data_string).unwrap(); + let _ = current_data.push(new_data); + let current_data_string: String = json::stringify(current_data.clone()); + let final_data_string: String = format!("{{\"data\": {} }}", current_data_string); + let _ = fs::write(main_file, final_data_string); + println!("You added the task!"); } - } // for did command else if args.command == "did" { // todo did _task_id_ - let task_id: i64= args.mainvalue.parse::().unwrap(); - println!("You are saying, you finished task with ID: {}", task_id); + let task_id: u32= args.mainvalue.parse::().unwrap(); + for index_ in 0..current_data.len() { + let current_task_id_string: String = json::stringify(current_data[index_]["task_id"].clone()); + let current_task_id: u32 = current_task_id_string.parse().unwrap(); + if current_task_id == task_id { + current_data[index_]["done"] = json::JsonValue::Boolean(true); + } + } + let current_data_string: String = json::stringify(current_data.clone()); + let final_data_string: String = format!("{{\"data\": {} }}", current_data_string); + let _ = fs::write(main_file, final_data_string); + println!("Marked task: {} as finished.", task_id); } else if args.command == "undid" { // todo undid _task_id_ - let task_id: i64= args.mainvalue.parse::().unwrap(); - println!("You are saying, you haven't actually finished with ID: {}", task_id); + let task_id: u32= args.mainvalue.parse::().unwrap(); + for index_ in 0..current_data.len() { + let current_task_id_string: String = json::stringify(current_data[index_]["task_id"].clone()); + let current_task_id: u32 = current_task_id_string.parse().unwrap(); + if current_task_id == task_id { + current_data[index_]["done"] = json::JsonValue::Boolean(false); + } + } + let current_data_string: String = json::stringify(current_data.clone()); + let final_data_string: String = format!("{{\"data\": {} }}", current_data_string); + let _ = fs::write(main_file, final_data_string); + println!("Marked task: {} as unfinished.", task_id); } else if args.command == "update" { // todo update --category _category_ _task_id_ + let mut new_category: String = "default".to_owned(); + let mut category_: String = "".to_owned(); + let task_id: u32= args.mainvalue.parse::().unwrap(); if args.category != None { - let task_id: i64= args.mainvalue.parse::().unwrap(); - println!("You are changing category of ID: {} to {}", task_id, args.category.to_owned().unwrap()); + new_category = args.category.to_owned().unwrap(); } // todo update _task_id_ - else { - let task_id: i64= args.mainvalue.parse::().unwrap(); - println!("You are removing category of task with ID: {}", task_id); + for index_ in 0..current_data.len() { + let current_task_id_string: String = json::stringify(current_data[index_]["task_id"].clone()); + let current_task_id: u32 = current_task_id_string.parse().unwrap(); + if current_task_id == task_id { + category_ = json::stringify(current_data[index_]["category"].clone()).replace("\"", ""); + current_data[index_]["category"] = json::JsonValue::String(new_category.clone()); + } + } + let current_data_string: String = json::stringify(current_data.clone()); + let final_data_string: String = format!("{{\"data\": {} }}", current_data_string); + let _ = fs::write(main_file, final_data_string); + println!("Updated the category of task: {} from {} to {}", task_id, category_, new_category); + } + else if args.command == "delete" { + // todo delete _task_id_ + let task_id: u32= args.mainvalue.parse::().unwrap(); + let mut remove_index_: usize = 9999; + for index_ in 0..current_data.len() { + let current_task_id_string: String = json::stringify(current_data[index_]["task_id"].clone()); + let current_task_id: u32 = current_task_id_string.parse().unwrap(); + if current_task_id == task_id { + remove_index_ = index_; + } } + let _ = current_data.array_remove(remove_index_); + let current_data_string: String = json::stringify(current_data.clone()); + let final_data_string: String = format!("{{\"data\": {} }}", current_data_string); + let _ = fs::write(main_file, final_data_string); + println!("You have deleted task: {}", task_id); } else { // When commands aren't from known list