From 25af0af6f118676e313ce539a9a968325db2c1a3 Mon Sep 17 00:00:00 2001 From: Pierre de Lacroix Date: Sat, 27 Jun 2026 18:38:44 +0200 Subject: [PATCH] improve bot command interface --- src/command.rs | 64 ++++++++++++++++++++++++++++++-------------------- src/grist.rs | 25 ++++++++++++++++---- src/main.rs | 18 ++++---------- 3 files changed, 64 insertions(+), 43 deletions(-) diff --git a/src/command.rs b/src/command.rs index b62ad39..14b3289 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,4 +1,4 @@ -use crate::grist::Shift; +use crate::grist::{Priority, Shift}; use clap::{CommandFactory, Parser, Subcommand}; use matrix_sdk::{Client, Room}; @@ -14,16 +14,24 @@ struct Clap { #[derive(Debug, Subcommand)] enum Action { /// Lister les shifts - Lister, + #[command(name = "lister")] + List, + /// Ajouter un shift - Ajouter { + #[command(name = "ajouter")] + Add { /// Le nom du shift - nom: String, - /// Plus d'informations - infos: String, + #[arg(id = "NOM")] + name: String, + + /// Date de début, sous ce format : J#-HH-MM (ex: J1-14-30) + #[arg(id = "DATE", short = 'd', long = "date")] + starting_time: String, + + /// Priorité + #[arg(id = "PRIORITÉ", short = 'p', long = "priorité", value_enum)] + priority: Priority, }, - /// Obtenir de l'aide - Aide, } pub async fn send_room_msg(_client: Client, room: Room, content: String) -> Res { @@ -62,26 +70,32 @@ pub async fn handle(command: impl AsRef, room: Room, client: Client) -> Res let args = shlex::split(command.as_ref()).ok_or_else(lazyhow!("error: Invalid quoting"))?; - let clap = Clap::try_parse_from(args)?; + let mut command = Clap::command(); + match command.try_get_matches_from_mut(&args) { + Ok(_) => { + let clap = Clap::try_parse_from(args)?; - match clap.action { - Action::Lister => { - debug!("user asked for list"); - let shifts = crate::grist::list_shifts().await?; - debug!("{:?}", shifts); - send_room_msg_html(client, room, format_shifts(shifts)).await?; + match clap.action { + Action::List => { + debug!("user asked for list"); + let shifts = crate::grist::list_shifts().await?; + debug!("{:?}", shifts); + send_room_msg_html(client, room, format_shifts(shifts)).await?; + } + Action::Add { + name, + starting_time, + priority, + } => { + debug!("user asked for add"); + } + } } - Action::Ajouter { nom, infos } => { - debug!("user asked for add"); + Err(err) => { + debug!("{:?}", err); + send_room_msg(client.clone(), room.clone(), format!("{}", err)).await?; } - Action::Aide => { - debug!("user asked for help"); - let mut command = Clap::command(); - let help = command.render_long_help(); - debug!("{}", help); - send_room_msg(client, room, help.to_string()).await?; - } - } + }; Ok(()) } diff --git a/src/grist.rs b/src/grist.rs index ed1663f..7ba81bb 100644 --- a/src/grist.rs +++ b/src/grist.rs @@ -1,6 +1,8 @@ prelude! {} use chrono::DateTime; +use clap::ValueEnum; +use clap::builder::PossibleValue; use grist_client::apis::configuration::Configuration; use grist_client::apis::records_api::list_records; use grist_client::models::{RecordsList, RecordsListRecordsInner}; @@ -22,14 +24,29 @@ pub enum Priority { impl Display for Priority { fn fmt(&self, f: &mut Fmt<'_>) -> FmtRes { match self { - Priority::Prioritaire => write!(f, "Prioritaire"), - Priority::Secondaire => write!(f, "Secondaire"), - Priority::Bonus => write!(f, "Bonus"), - Priority::Urgent => write!(f, "Prioritaire"), + Self::Prioritaire => write!(f, "Prioritaire"), + Self::Secondaire => write!(f, "Secondaire"), + Self::Bonus => write!(f, "Bonus"), + Self::Urgent => write!(f, "Prioritaire"), } } } +impl ValueEnum for Priority { + fn value_variants<'a>() -> &'a [Self] { + &[Self::Prioritaire, Self::Secondaire, Self::Bonus, Self::Urgent] + } + + fn to_possible_value(&self) -> Option { + Some(match self { + Self::Prioritaire => PossibleValue::new("1"), + Self::Secondaire => PossibleValue::new("2"), + Self::Bonus => PossibleValue::new("3"), + Self::Urgent => PossibleValue::new("0"), + }) + } +} + #[derive(Clone, Debug)] pub struct ShiftType { pub name: String, diff --git a/src/main.rs b/src/main.rs index 37c6ed8..27f4bb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ prelude! {} #[command(version, about)] struct Clap { /// Verbosity level. - #[arg(short, long, value_name = "NATURAL", default_value_t = 2)] + #[arg(short, long, default_value_t = 2)] verb: usize, /// Wipe the static data directory before doing anything. @@ -32,25 +32,15 @@ struct Clap { wipe: bool, /// Identifier of the element bot account to run. - #[arg( - short, - long, - value_name = "MATRIX_USERNAME", - default_value = "shift_bot_dev" - )] + #[arg(short, long, default_value = "shift_bot_dev")] username: String, /// Homeserver. - #[arg( - short = 's', - long, - value_name = "MATRIX_HOMESERVER", - default_value = "matrix.org" - )] + #[arg(short = 's', long, default_value = "matrix.org")] homeserver: String, /// Password of the element bot account. - #[arg(short, long, value_name = "PASSWORD")] + #[arg(short, long)] password: String, /// Grist API KEY