From 82fa15b3e3161f7fef5241ef343a864978f87a5c Mon Sep 17 00:00:00 2001 From: The Chef Date: Sat, 12 Apr 2025 02:29:19 +0000 Subject: [PATCH] Added main.rs file --- src/main.rs | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8f50109 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,134 @@ +use std::fmt; +use clap::Parser; + + +/// Simple todo program +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + // parse the main command + #[arg(index=1)] + command: String, + + // parse the main value + #[arg(index=2, default_value="")] + mainvalue: String, + + // parse the subcommand + #[arg(short, long, action)] + all: bool, + + #[arg(short, long, action)] + done: bool, + + #[arg(short, long, action)] + category: Option, +} + + +// 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(); + // 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_); + } + // todo list --all + else { + println!("You are trying to print all tasks!"); + } + } + 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_); + } + // todo list --done + else { + println!("You are trying to print completed tasks!"); + } + } + 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_); + } + // todo list + else { + println!("You are trying to list tasks"); + } + } + + } + // for add command + else if args.command == "add" { + // todo add --category _category_ _task_ + 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_); + } + // todo add _task_ + else { + let taskname: String = args.mainvalue; + println!("You are trying to add a (uncategorized) task! \"{}\"", taskname); + } + } + // 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); + } + 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); + } + else if args.command == "update" { + // todo update --category _category_ _task_id_ + 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()); + } + // todo update _task_id_ + else { + let task_id: i64= args.mainvalue.parse::().unwrap(); + println!("You are removing category of task with ID: {}", task_id); + } + } +}