Add error type parameter to commands
This commit is contained in:
parent
4dcc021f73
commit
ec9c4c9dd4
3 changed files with 41 additions and 33 deletions
|
|
@ -76,10 +76,10 @@ impl Context {
|
|||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Command<B> {
|
||||
pub trait Command<B, E> {
|
||||
fn description(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context, bot: &mut B);
|
||||
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context, bot: &mut B) -> Result<(), E>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,14 +2,21 @@ use async_trait::async_trait;
|
|||
use clap::{CommandFactory, Parser};
|
||||
|
||||
use crate::api::Message;
|
||||
use crate::conn;
|
||||
|
||||
use super::{Command, Context};
|
||||
|
||||
#[async_trait]
|
||||
pub trait ClapCommand<B> {
|
||||
pub trait ClapCommand<B, E> {
|
||||
type Args;
|
||||
|
||||
async fn execute(&self, args: Self::Args, msg: &Message, ctx: &Context, bot: &mut B);
|
||||
async fn execute(
|
||||
&self,
|
||||
args: Self::Args,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<(), E>;
|
||||
}
|
||||
|
||||
/// Parse bash-like quoted arguments separated by whitespace.
|
||||
|
|
@ -92,22 +99,23 @@ fn parse_quoted_args(text: &str) -> Result<Vec<String>, &'static str> {
|
|||
pub struct Clap<C>(pub C);
|
||||
|
||||
#[async_trait]
|
||||
impl<B, C> Command<B> for Clap<C>
|
||||
impl<B, E, C> Command<B, E> for Clap<C>
|
||||
where
|
||||
B: Send,
|
||||
C: ClapCommand<B> + Send + Sync,
|
||||
E: From<conn::Error>,
|
||||
C: ClapCommand<B, E> + Send + Sync,
|
||||
C::Args: Parser + Send,
|
||||
{
|
||||
fn description(&self) -> Option<String> {
|
||||
C::Args::command().get_about().map(|s| format!("{s}"))
|
||||
}
|
||||
|
||||
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context, bot: &mut B) {
|
||||
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context, bot: &mut B) -> Result<(), E> {
|
||||
let mut args = match parse_quoted_args(arg) {
|
||||
Ok(args) => args,
|
||||
Err(err) => {
|
||||
let _ = ctx.reply(msg.id, err);
|
||||
return;
|
||||
ctx.reply(msg.id, err).await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -116,8 +124,8 @@ where
|
|||
let args = match C::Args::try_parse_from(args) {
|
||||
Ok(args) => args,
|
||||
Err(err) => {
|
||||
let _ = ctx.reply(msg.id, format!("{}", err.render()));
|
||||
return;
|
||||
ctx.reply(msg.id, format!("{}", err.render())).await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -46,18 +46,18 @@ pub struct CommandInfo {
|
|||
pub visible: bool,
|
||||
}
|
||||
|
||||
struct CommandWrapper<B> {
|
||||
command: Box<dyn Command<B>>,
|
||||
struct CommandWrapper<B, E> {
|
||||
command: Box<dyn Command<B, E>>,
|
||||
visible: bool,
|
||||
}
|
||||
|
||||
pub struct Commands<B> {
|
||||
global: HashMap<String, CommandWrapper<B>>,
|
||||
general: HashMap<String, CommandWrapper<B>>,
|
||||
specific: HashMap<String, CommandWrapper<B>>,
|
||||
pub struct Commands<B, E> {
|
||||
global: HashMap<String, CommandWrapper<B, E>>,
|
||||
general: HashMap<String, CommandWrapper<B, E>>,
|
||||
specific: HashMap<String, CommandWrapper<B, E>>,
|
||||
}
|
||||
|
||||
impl<B> Commands<B> {
|
||||
impl<B, E> Commands<B, E> {
|
||||
/// Global commands always respond. They override any specific or general
|
||||
/// commands of the same name.
|
||||
///
|
||||
|
|
@ -65,7 +65,7 @@ impl<B> Commands<B> {
|
|||
pub fn global<S, C>(mut self, name: S, command: C, visible: bool) -> Self
|
||||
where
|
||||
S: ToString,
|
||||
C: Command<B> + 'static,
|
||||
C: Command<B, E> + 'static,
|
||||
{
|
||||
let command = Box::new(command);
|
||||
let info = CommandWrapper { command, visible };
|
||||
|
|
@ -80,7 +80,7 @@ impl<B> Commands<B> {
|
|||
pub fn general<S, C>(mut self, name: S, command: C, visible: bool) -> Self
|
||||
where
|
||||
S: ToString,
|
||||
C: Command<B> + 'static,
|
||||
C: Command<B, E> + 'static,
|
||||
{
|
||||
let command = Box::new(command);
|
||||
let info = CommandWrapper { command, visible };
|
||||
|
|
@ -92,7 +92,7 @@ impl<B> Commands<B> {
|
|||
pub fn specific<S, C>(mut self, name: S, command: C, visible: bool) -> Self
|
||||
where
|
||||
S: ToString,
|
||||
C: Command<B> + 'static,
|
||||
C: Command<B, E> + 'static,
|
||||
{
|
||||
let command = Box::new(command);
|
||||
let info = CommandWrapper { command, visible };
|
||||
|
|
@ -149,20 +149,20 @@ impl<B> Commands<B> {
|
|||
packet: &ParsedPacket,
|
||||
snapshot: &Snapshot,
|
||||
bot: &mut B,
|
||||
) -> bool {
|
||||
) -> Result<bool, E> {
|
||||
let msg = match &packet.content {
|
||||
Ok(Data::SendEvent(SendEvent(msg))) => msg,
|
||||
_ => return false,
|
||||
_ => return Ok(false),
|
||||
};
|
||||
|
||||
let joined = match &snapshot.state {
|
||||
conn::State::Joining(_) => return false,
|
||||
conn::State::Joining(_) => return Ok(false),
|
||||
conn::State::Joined(joined) => joined.clone(),
|
||||
};
|
||||
|
||||
let (cmd_name, rest) = match parse_command(&msg.content) {
|
||||
Some(parsed) => parsed,
|
||||
None => return false,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
let mut ctx = Context {
|
||||
|
|
@ -175,8 +175,8 @@ impl<B> Commands<B> {
|
|||
|
||||
if let Some(wrapper) = self.global.get(cmd_name) {
|
||||
ctx.kind = Kind::Global;
|
||||
wrapper.command.execute(rest, msg, &ctx, bot).await;
|
||||
return true;
|
||||
wrapper.command.execute(rest, msg, &ctx, bot).await?;
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if let Some((cmd_nick, rest)) = parse_specific(rest) {
|
||||
|
|
@ -185,8 +185,8 @@ impl<B> Commands<B> {
|
|||
let cmd_nick_norm = normalize_specific_nick(cmd_nick);
|
||||
if nick_norm == cmd_nick_norm {
|
||||
ctx.kind = Kind::Specific;
|
||||
wrapper.command.execute(rest, msg, &ctx, bot).await;
|
||||
return true;
|
||||
wrapper.command.execute(rest, msg, &ctx, bot).await?;
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -196,16 +196,16 @@ impl<B> Commands<B> {
|
|||
//
|
||||
// To call a specific command with a mention as its first positional
|
||||
// argument, -- can be used.
|
||||
return false;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
if let Some(wrapper) = self.general.get(cmd_name) {
|
||||
ctx.kind = Kind::General;
|
||||
wrapper.command.execute(rest, msg, &ctx, bot).await;
|
||||
return true;
|
||||
wrapper.command.execute(rest, msg, &ctx, bot).await?;
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
false
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue