Added main.rs file
This commit is contained in:
134
src/main.rs
Normal file
134
src/main.rs
Normal file
@@ -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<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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::<i64>().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::<i64>().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::<i64>().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::<i64>().unwrap();
|
||||||
|
println!("You are removing category of task with ID: {}", task_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user