Update src/main.rs
This commit is contained in:
125
src/main.rs
125
src/main.rs
@@ -1,9 +1,6 @@
|
|||||||
use std::fmt;
|
|
||||||
use json::JsonValue;
|
use json::JsonValue;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
// #[macro_use] extern crate prettytable;
|
|
||||||
// use prettytable::{Table, Row, Cell};
|
|
||||||
use prettytable::Table;
|
use prettytable::Table;
|
||||||
use prettytable::row;
|
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() {
|
fn main() {
|
||||||
// parse arguments
|
// parse arguments
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
@@ -69,9 +38,9 @@ fn main() {
|
|||||||
|
|
||||||
// main data file name
|
// main data file name
|
||||||
let main_file: String = "data.json".to_owned();
|
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.");
|
.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
|
// for list commands
|
||||||
if args.command == "list" {
|
if args.command == "list" {
|
||||||
@@ -181,40 +150,100 @@ fn main() {
|
|||||||
// for add command
|
// for add command
|
||||||
else if args.command == "add" {
|
else if args.command == "add" {
|
||||||
// todo add --category _category_ _task_
|
// todo add --category _category_ _task_
|
||||||
|
// variable to hold category string
|
||||||
|
let mut category_: String = "default".to_owned();
|
||||||
if args.category != None {
|
if args.category != None {
|
||||||
let category_: String = args.category.to_owned().unwrap();
|
// if category is provided, change default to provided category
|
||||||
let taskname: String = args.mainvalue;
|
category_ = args.category.to_owned().unwrap();
|
||||||
println!("You are trying to add a task \"{}\" to category {}", taskname, category_);
|
|
||||||
}
|
}
|
||||||
// todo add _task_
|
// todo add _task_
|
||||||
else {
|
let mut next_task_id: u32 = 1;
|
||||||
let taskname: String = args.mainvalue;
|
for index_ in 0..current_data.len() {
|
||||||
println!("You are trying to add a (uncategorized) task! \"{}\"", taskname);
|
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
|
// for did command
|
||||||
else if args.command == "did" {
|
else if args.command == "did" {
|
||||||
// todo did _task_id_
|
// todo did _task_id_
|
||||||
let task_id: i64= args.mainvalue.parse::<i64>().unwrap();
|
let task_id: u32= args.mainvalue.parse::<u32>().unwrap();
|
||||||
println!("You are saying, you finished 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 {
|
||||||
|
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" {
|
else if args.command == "undid" {
|
||||||
// todo undid _task_id_
|
// todo undid _task_id_
|
||||||
let task_id: i64= args.mainvalue.parse::<i64>().unwrap();
|
let task_id: u32= args.mainvalue.parse::<u32>().unwrap();
|
||||||
println!("You are saying, you haven't actually finished 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 {
|
||||||
|
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" {
|
else if args.command == "update" {
|
||||||
// todo update --category _category_ _task_id_
|
// 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::<u32>().unwrap();
|
||||||
if args.category != None {
|
if args.category != None {
|
||||||
let task_id: i64= args.mainvalue.parse::<i64>().unwrap();
|
new_category = args.category.to_owned().unwrap();
|
||||||
println!("You are changing category of ID: {} to {}", task_id, args.category.to_owned().unwrap());
|
|
||||||
}
|
}
|
||||||
// todo update _task_id_
|
// todo update _task_id_
|
||||||
else {
|
for index_ in 0..current_data.len() {
|
||||||
let task_id: i64= args.mainvalue.parse::<i64>().unwrap();
|
let current_task_id_string: String = json::stringify(current_data[index_]["task_id"].clone());
|
||||||
println!("You are removing category of task with ID: {}", task_id);
|
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::<u32>().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 {
|
else {
|
||||||
// When commands aren't from known list
|
// When commands aren't from known list
|
||||||
println!("Unknown command!\nUse --help for more information!");
|
println!("Unknown command!\nUse --help for more information!");
|
||||||
|
|||||||
Reference in New Issue
Block a user