worthit/src/cli.rs
2025-08-10 20:19:49 +08:00

115 lines
3 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::{
model::Records,
utils::{add_handler, delete_handler, set_handler, show_handler},
};
#[derive(Parser, Debug)]
#[command(
version,
about = "記錄買過咩東西",
long_about = "大額商品總是希望可以耐用不易壞,記錄買過啲貴嘢睇下可以用幾耐",
disable_help_subcommand = true
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
Add {
#[arg(short = 'n', long, help = "佢個名")]
name: String,
#[arg(short = 'p', long, help = "幾钱")]
price: f64,
#[arg(long = "pd", help = "幾時買,例如2024-1-6")]
purchase_date: String,
},
Set {
#[arg(short = 'n', long = "current-name", help = "佢個名")]
name: Option<String>,
#[arg(long = "new-name", help = "起個新名")]
new_name: Option<String>,
#[arg(short = 'p', long, help = "幾钱")]
price: Option<f64>,
#[arg(long, help = "幾時買,例如2024-1-6")]
purchase_date: Option<String>,
#[arg(
short,
long,
help = "0: 用緊, 1: 食塵, 2: 壞咗, 3: 賣咗",
default_value = "0"
)]
status: Option<u32>,
#[arg(long, help = "整咗幾次")]
repair_count: Option<u32>,
#[arg(long, help = "維修費")]
repair_cost: Option<f64>,
#[arg(long, help = "轉手價")]
sold_price: Option<f64>,
#[arg(long, help = "幾時賣,例如2025-1-6")]
sold_date: Option<String>,
},
Show,
Delete {
#[arg(short, long, help = "要 delete 咩?(或者唔指定名稱開啓交互模式)")]
name: Option<String>,
},
}
pub trait CommandHandler {
fn execute(self, records: Records, record_path: String) -> Result<()>;
}
impl CommandHandler for Commands {
fn execute(self, records: Records, record_path: String) -> Result<()> {
match self {
Commands::Add {
name,
price,
purchase_date,
} => add_handler(name, price, purchase_date, records, record_path),
Commands::Set {
name,
new_name,
price,
purchase_date,
status,
repair_count,
repair_cost,
sold_price,
sold_date,
} => set_handler(
name,
new_name,
price,
purchase_date,
status,
repair_count,
repair_cost,
sold_price,
sold_date,
records,
record_path,
),
Commands::Show => {
show_handler(records);
Ok(())
}
Commands::Delete { name } => delete_handler(name, records, record_path),
}
}
}