Update src/main.rs

This commit is contained in:
2025-04-15 18:19:31 +00:00
parent 82fa15b3e3
commit becfdbdb15

View File

@@ -1,6 +1,11 @@
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;
/// Simple todo program
#[derive(Parser, Debug)]
@@ -57,39 +62,118 @@ impl fmt::Display for Task<'_>{
fn main() {
// parse arguments
let args = Args::parse();
// results table
let mut result_table = Table::new();
result_table.add_row(row!["DONE", "TASK ID", "TASK NAME", "CATEGORY"]);
// main data file name
let main_file: String = "data.json".to_owned();
let current_string: String = fs::read_to_string(main_file)
.expect("File could not be found.");
let current_data: &JsonValue = &json::parse(&current_string).unwrap()["data"];
// for list commands
if args.command == "list" {
if args.all {
// todo list --all --category _category_
if args.category != None {
let category_: String = args.category.to_owned().unwrap();
println!("You are trying to list all tasks in category: {}", category_);
let category__: String = args.category.to_owned().unwrap();
println!("You are trying to list all tasks in category: {}", category__);
for index_ in 0..current_data.len() {
let curr: JsonValue = current_data[index_].clone();
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
let task_id_: String = json::stringify(curr["task_id"].clone()).replace("\"","");
let task_name_: String = json::stringify(curr["task_name"].clone()).replace("\"", "");
let category_: String = json::stringify(curr["category"].clone()).replace("\"", "");
if category_ != category__ { continue; } else {
result_table.add_row(row![done_, task_id_, task_name_, category_]);
}
}
result_table.printstd();
}
// todo list --all
else {
println!("You are trying to print all tasks!");
for index_ in 0..current_data.len() {
let curr: JsonValue = current_data[index_].clone();
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
let task_id_: String = json::stringify(curr["task_id"].clone());
let task_name_: String = json::stringify(curr["task_name"].clone());
let category_: String = json::stringify(curr["category"].clone());
result_table.add_row(row![done_, task_id_, task_name_, category_]);
}
result_table.printstd();
}
}
else if args.done {
// todo list --done --category _category_
if args.category != None {
let category_: String = args.category.to_owned().unwrap();
println!("You have a category {}", category_);
let category__: String = args.category.to_owned().unwrap();
// println!("You have a category {}", category__);
for index_ in 0..current_data.len() {
let curr: JsonValue = current_data[index_].clone();
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
let task_id_: String = json::stringify(curr["task_id"].clone()).replace("\"","");
let task_name_: String = json::stringify(curr["task_name"].clone()).replace("\"", "");
let category_: String = json::stringify(curr["category"].clone()).replace("\"", "");
if !done_ { continue; } else {
if category_ != category__ { continue; } else {
result_table.add_row(row![done_, task_id_, task_name_, category_]);
}
}
}
result_table.printstd();
}
// todo list --done
else {
println!("You are trying to print completed tasks!");
// println!("You are trying to print completed tasks!");
for index_ in 0..current_data.len() {
let curr: JsonValue = current_data[index_].clone();
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
let task_id_: String = json::stringify(curr["task_id"].clone()).replace("\"","");
let task_name_: String = json::stringify(curr["task_name"].clone()).replace("\"", "");
let category_: String = json::stringify(curr["category"].clone()).replace("\"", "");
if !done_ { continue; } else {
result_table.add_row(row![done_, task_id_, task_name_, category_]);
}
}
result_table.printstd();
}
}
else {
// todo list --category _category_
if args.category != None {
let category_: String = args.category.to_owned().unwrap();
println!("You a printing default taks with category {}", category_);
let category__: String = args.category.to_owned().unwrap();
// println!("You a printing default taks with category {}", category__);
for index_ in 0..current_data.len() {
let curr: JsonValue = current_data[index_].clone();
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
let task_id_: String = json::stringify(curr["task_id"].clone()).replace("\"","");
let task_name_: String = json::stringify(curr["task_name"].clone()).replace("\"", "");
let category_: String = json::stringify(curr["category"].clone()).replace("\"", "");
if done_ { continue; } else {
if category_ != category__ { continue; } else {
result_table.add_row(row![done_, task_id_, task_name_, category_]);
}
}
}
result_table.printstd();
}
// todo list
else {
println!("You are trying to list tasks");
// printing with prettytable
for index_ in 0..current_data.len() {
let curr: JsonValue = current_data[index_].clone();
let done_: bool = json::stringify(curr["done"].clone()).parse().expect("Parsing a true-false value");
let task_id_: String = json::stringify(curr["task_id"].clone());
let task_name_: String = json::stringify(curr["task_name"].clone());
let category_: String = json::stringify(curr["category"].clone());
if done_ { continue; } else {
result_table.add_row(row![done_, task_id_, task_name_, category_]);
}
}
result_table.printstd();
}
}
@@ -131,4 +215,8 @@ fn main() {
println!("You are removing category of task with ID: {}", task_id);
}
}
else {
// When commands aren't from known list
println!("Unknown command!\nUse --help for more information!");
}
}