141 lines
3.3 KiB
Rust
141 lines
3.3 KiB
Rust
//! Basic command wrappers.
|
|
|
|
use std::future::Future;
|
|
|
|
use async_trait::async_trait;
|
|
use euphoxide::api::Message;
|
|
|
|
use super::{Command, Context, Info, Propagate};
|
|
|
|
/// Rewrite or hide command info.
|
|
pub struct WithInfo<C> {
|
|
pub inner: C,
|
|
pub trigger: Option<Option<String>>,
|
|
pub description: Option<Option<String>>,
|
|
}
|
|
|
|
impl<C> WithInfo<C> {
|
|
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<E, C> Command<E> for WithInfo<C>
|
|
where
|
|
C: Command<E> + Sync,
|
|
{
|
|
fn info(&self, ctx: &Context<E>) -> 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<E>) -> Result<Propagate, E> {
|
|
self.inner.execute(arg, msg, ctx).await
|
|
}
|
|
}
|
|
|
|
pub struct Prefixed<C> {
|
|
pub prefix: String,
|
|
pub inner: C,
|
|
}
|
|
|
|
impl<C> Prefixed<C> {
|
|
pub fn new<S: ToString>(prefix: S, inner: C) -> Self {
|
|
Self {
|
|
prefix: prefix.to_string(),
|
|
inner,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl<E, C> Command<E> for Prefixed<C>
|
|
where
|
|
C: Command<E> + Sync,
|
|
{
|
|
fn info(&self, ctx: &Context<E>) -> Info {
|
|
self.inner.info(ctx).with_prepended_trigger(&self.prefix)
|
|
}
|
|
|
|
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
|
|
if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) {
|
|
self.inner.execute(rest, msg, ctx).await
|
|
} else {
|
|
Ok(Propagate::Yes)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Black type magic, thanks a lot to https://github.com/kpreid and the
|
|
// async_fn_traits crate!
|
|
|
|
// TODO Simplify all this once AsyncFn becomes stable
|
|
|
|
pub trait HandlerFn<'a0, 'a1, 'a2, E>:
|
|
Fn(&'a0 str, &'a1 Message, &'a2 Context<E>) -> Self::Future
|
|
where
|
|
E: 'a2,
|
|
{
|
|
type Future: Future<Output = Result<Propagate, E>> + Send;
|
|
}
|
|
|
|
impl<'a0, 'a1, 'a2, E, F, Fut> HandlerFn<'a0, 'a1, 'a2, E> for F
|
|
where
|
|
E: 'a2,
|
|
F: Fn(&'a0 str, &'a1 Message, &'a2 Context<E>) -> Fut + ?Sized,
|
|
Fut: Future<Output = Result<Propagate, E>> + Send,
|
|
{
|
|
type Future = Fut;
|
|
}
|
|
|
|
pub struct FromHandler<F>(pub F);
|
|
|
|
impl<F> FromHandler<F> {
|
|
pub fn new(f: F) -> Self {
|
|
Self(f)
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl<E, F> Command<E> for FromHandler<F>
|
|
where
|
|
F: for<'a0, 'a1, 'a2> HandlerFn<'a0, 'a1, 'a2, E> + Sync,
|
|
{
|
|
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
|
|
(self.0)(arg, msg, ctx).await
|
|
}
|
|
}
|