2022-12-24 21:16:23 +11:00
|
|
|
use super::ListenerSession;
|
2022-12-24 13:43:28 +11:00
|
|
|
use crate::DResult;
|
|
|
|
use crate::db::DBPool;
|
|
|
|
use ansi_macro::ansi;
|
2022-12-24 21:16:23 +11:00
|
|
|
use phf::phf_map;
|
|
|
|
use async_trait::async_trait;
|
2022-12-24 13:43:28 +11:00
|
|
|
|
2022-12-24 21:16:23 +11:00
|
|
|
mod parsing;
|
|
|
|
mod ignore;
|
|
|
|
mod help;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct VerbContext<'l> {
|
|
|
|
session: &'l ListenerSession,
|
|
|
|
pool: &'l DBPool
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum CommandHandlingError {
|
|
|
|
UserError(String),
|
|
|
|
SystemError(Box<dyn std::error::Error + Send + Sync>)
|
|
|
|
}
|
|
|
|
use CommandHandlingError::*;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait UserVerb {
|
|
|
|
async fn handle(self: &Self, ctx: &VerbContext, verb: &str, remaining: &str) -> UResult<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type UserVerbRef = &'static (dyn UserVerb + Sync + Send);
|
|
|
|
pub type UResult<A> = Result<A, CommandHandlingError>;
|
|
|
|
|
|
|
|
impl From<Box<dyn std::error::Error + Send + Sync>> for CommandHandlingError {
|
|
|
|
fn from(input: Box<dyn std::error::Error + Send + Sync>) -> CommandHandlingError {
|
|
|
|
SystemError(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn user_error<A>(msg: String) -> UResult<A> {
|
|
|
|
Err(UserError(msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserVerbRegistry = phf::Map<&'static str, UserVerbRef>;
|
|
|
|
|
|
|
|
static ALWAYS_AVAILABLE_COMMANDS: UserVerbRegistry = phf_map! {
|
|
|
|
"" => ignore::VERB,
|
|
|
|
"help" => help::VERB
|
|
|
|
};
|
|
|
|
|
|
|
|
pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DResult<()> {
|
|
|
|
let (cmd, params) = parsing::parse_command_name(msg);
|
|
|
|
let handler_opt = ALWAYS_AVAILABLE_COMMANDS.get(cmd);
|
|
|
|
|
|
|
|
match handler_opt {
|
|
|
|
None => {
|
|
|
|
pool.queue_for_session(session,
|
|
|
|
ansi!(
|
|
|
|
"That's not a command I know. Try <bold>help<reset>\r\n"
|
|
|
|
)
|
|
|
|
).await?;
|
|
|
|
}
|
|
|
|
Some(handler) => {
|
|
|
|
match handler.handle(&VerbContext { session, pool }, cmd, params).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(UserError(err_msg)) => {
|
2022-12-24 21:38:14 +11:00
|
|
|
pool.queue_for_session(session, &(err_msg + "\r\n")).await?;
|
2022-12-24 21:16:23 +11:00
|
|
|
}
|
|
|
|
Err(SystemError(e)) => Err(e)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2022-12-24 13:43:28 +11:00
|
|
|
pool.queue_for_session(session,
|
|
|
|
&format!(ansi!(
|
|
|
|
"You hear an echo saying: <bggreen><red>{}<reset>\r\n"
|
|
|
|
), msg)).await?;
|
2022-12-24 21:16:23 +11:00
|
|
|
*/
|
2022-12-24 13:43:28 +11:00
|
|
|
Ok(())
|
|
|
|
}
|