1.8 KiB · text History 2803012
use crate::data::{TraitDefaultOps, TraitKind};
use crate::PplError;
use clap::{Args, Subcommand};
use sea_orm::DatabaseConnection;

#[derive(Args, Debug)]
pub struct TraitArgs {
    #[command(subcommand)]
    pub action: Option<TraitAction>,
}

#[derive(Subcommand, Debug)]
pub enum TraitAction {
    /// List the traits ppl can have
    List,
    /// Create a new trait
    Add {
        name: String,
        #[arg(long)]
        symbol: Option<String>,
        /// One of: red, gold, amber, green, teal, cyan, blue, violet, pink, slate
        #[arg(long)]
        color: Option<String>,
        /// trait (default), date (shows in motd and the calendar) or contact
        #[arg(long, default_value = "trait")]
        kind: TraitKind,
    },
}

pub async fn do_traits(args: &TraitArgs, db: &DatabaseConnection) -> Result<(), PplError> {
    match &args.action {
        None | Some(TraitAction::List) => {
            for t in TraitDefaultOps::list(db).await? {
                let kind = match (t.is_date, t.is_contact) {
                    (true, _) => "date",
                    (_, true) => "contact",
                    _ => "trait",
                };
                println!(
                    "{} {:<16} {:<8} {}",
                    t.symbol,
                    t.key,
                    kind,
                    if t.enabled { "" } else { "(disabled)" }
                );
            }
        }
        Some(TraitAction::Add {
            name,
            symbol,
            color,
            kind,
        }) => {
            let key = TraitDefaultOps::add(
                db,
                name,
                symbol.as_deref().unwrap_or(""),
                color.as_deref().unwrap_or(""),
                *kind,
            )
            .await?;
            println!("added trait {}", key);
        }
    }
    Ok(())
}