From 12448c26c9603bd0590daf42f2b0a62ec5c64f19 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 27 Dec 2024 17:56:54 +0100 Subject: [PATCH] Reorganize command wrappers --- euphoxide-bot/src/command.rs | 5 +- euphoxide-bot/src/command/bang.rs | 2 + euphoxide-bot/src/command/basic.rs | 113 ++++++++++++++++++++++++++ euphoxide-bot/src/command/hidden.rs | 55 ------------- euphoxide-bot/src/command/prefixed.rs | 43 ---------- 5 files changed, 116 insertions(+), 102 deletions(-) create mode 100644 euphoxide-bot/src/command/basic.rs delete mode 100644 euphoxide-bot/src/command/hidden.rs delete mode 100644 euphoxide-bot/src/command/prefixed.rs diff --git a/euphoxide-bot/src/command.rs b/euphoxide-bot/src/command.rs index e4521a4..789c2d9 100644 --- a/euphoxide-bot/src/command.rs +++ b/euphoxide-bot/src/command.rs @@ -1,8 +1,7 @@ pub mod bang; +pub mod basic; #[cfg(feature = "clap")] pub mod clap; -mod hidden; -mod prefixed; use std::future::Future; @@ -15,8 +14,6 @@ use euphoxide::{ }, }; -pub use self::{hidden::*, prefixed::*}; - #[non_exhaustive] pub struct Context { pub conn: ClientConnHandle, diff --git a/euphoxide-bot/src/command/bang.rs b/euphoxide-bot/src/command/bang.rs index 99dc814..da2d213 100644 --- a/euphoxide-bot/src/command/bang.rs +++ b/euphoxide-bot/src/command/bang.rs @@ -1,3 +1,5 @@ +//! Euphoria-style `!foo` and `!foo @bar` command wrappers. + use async_trait::async_trait; use euphoxide::{api::Message, nick}; diff --git a/euphoxide-bot/src/command/basic.rs b/euphoxide-bot/src/command/basic.rs new file mode 100644 index 0000000..8e5b35c --- /dev/null +++ b/euphoxide-bot/src/command/basic.rs @@ -0,0 +1,113 @@ +//! Basic command wrappers. + +use async_trait::async_trait; +use euphoxide::api::Message; + +use super::{Command, Context, Info, Propagate}; + +/// Rewrite or hide command info. +pub struct Described { + pub inner: C, + pub trigger: Option>, + pub description: Option>, +} + +impl Described { + pub fn new(inner: C) -> Self { + Self { + inner, + trigger: None, + description: None, + } + } + + pub fn hidden(inner: C) -> Self { + Self::new(inner) + .with_trigger_hidden() + .with_description_hidden() + } + + pub fn with_trigger(mut self, trigger: impl ToString) -> Self { + self.trigger = Some(Some(trigger.to_string())); + self + } + + pub fn with_trigger_hidden(mut self) -> Self { + self.trigger = Some(None); + self + } + + pub fn with_description(mut self, description: impl ToString) -> Self { + self.description = Some(Some(description.to_string())); + self + } + + pub fn with_description_hidden(mut self) -> Self { + self.description = Some(None); + self + } +} + +#[async_trait] +impl Command for Described +where + B: Send, + C: Command + Send + Sync, +{ + fn info(&self, ctx: &Context) -> Info { + let info = self.inner.info(ctx); + Info { + trigger: self.trigger.clone().unwrap_or(info.trigger), + description: self.description.clone().unwrap_or(info.description), + } + } + + async fn execute( + &self, + arg: &str, + msg: &Message, + ctx: &Context, + bot: &mut B, + ) -> Result { + self.inner.execute(arg, msg, ctx, bot).await + } +} + +pub struct Prefixed { + prefix: String, + inner: C, +} + +impl Prefixed { + pub fn new(prefix: S, inner: C) -> Self { + Self { + prefix: prefix.to_string(), + inner, + } + } +} + +#[async_trait] +impl Command for Prefixed +where + B: Send, + C: Command + Send + Sync, +{ + fn info(&self, ctx: &Context) -> Info { + self.inner.info(ctx).with_prepended_trigger(&self.prefix) + } + + async fn execute( + &self, + arg: &str, + msg: &Message, + ctx: &Context, + bot: &mut B, + ) -> Result { + if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) { + self.inner.execute(rest, msg, ctx, bot).await + } else { + Ok(Propagate::Yes) + } + } +} diff --git a/euphoxide-bot/src/command/hidden.rs b/euphoxide-bot/src/command/hidden.rs deleted file mode 100644 index 4c6ee12..0000000 --- a/euphoxide-bot/src/command/hidden.rs +++ /dev/null @@ -1,55 +0,0 @@ -use async_trait::async_trait; -use euphoxide::api::Message; - -use super::{Command, Context, Info, Propagate}; - -pub struct Hidden { - pub inner: C, - pub allow_trigger: bool, - pub allow_description: bool, -} - -impl Hidden { - pub fn new(inner: C) -> Self { - Self { - inner, - allow_trigger: false, - allow_description: false, - } - } - - pub fn with_allow_trigger(mut self, allow: bool) -> Self { - self.allow_trigger = allow; - self - } - - pub fn with_allow_description(mut self, allow: bool) -> Self { - self.allow_description = allow; - self - } -} - -#[async_trait] -impl Command for Hidden -where - B: Send, - C: Command + Send + Sync, -{ - fn info(&self, ctx: &Context) -> Info { - let info = self.inner.info(ctx); - Info { - trigger: info.trigger.filter(|_| self.allow_trigger), - description: info.description.filter(|_| self.allow_description), - } - } - - async fn execute( - &self, - arg: &str, - msg: &Message, - ctx: &Context, - bot: &mut B, - ) -> Result { - self.inner.execute(arg, msg, ctx, bot).await - } -} diff --git a/euphoxide-bot/src/command/prefixed.rs b/euphoxide-bot/src/command/prefixed.rs deleted file mode 100644 index f989a26..0000000 --- a/euphoxide-bot/src/command/prefixed.rs +++ /dev/null @@ -1,43 +0,0 @@ -use async_trait::async_trait; -use euphoxide::api::Message; - -use super::{Command, Context, Info, Propagate}; - -pub struct Prefixed { - prefix: String, - inner: C, -} - -impl Prefixed { - pub fn new(prefix: S, inner: C) -> Self { - Self { - prefix: prefix.to_string(), - inner, - } - } -} - -#[async_trait] -impl Command for Prefixed -where - B: Send, - C: Command + Send + Sync, -{ - fn info(&self, ctx: &Context) -> Info { - self.inner.info(ctx).with_prepended_trigger(&self.prefix) - } - - async fn execute( - &self, - arg: &str, - msg: &Message, - ctx: &Context, - bot: &mut B, - ) -> Result { - if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) { - self.inner.execute(rest, msg, ctx, bot).await - } else { - Ok(Propagate::Yes) - } - } -}