use crate::error::Result;
use crate::response::ResponseUnit;
use crate::tokenizer::Tokenizer;
use crate::Context;
pub trait Command {
fn help(&self, _response: &mut ResponseUnit) {}
fn meta(&self) -> CommandTypeMeta {
CommandTypeMeta::Unknown
}
fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()>;
fn query(
&self,
context: &mut Context,
args: &mut Tokenizer,
response: &mut ResponseUnit,
) -> Result<()>;
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum CommandTypeMeta {
Unknown,
NoQuery,
QueryOnly,
None,
}
#[macro_export]
macro_rules! qonly {
() => {
fn meta(&self) -> CommandTypeMeta {
CommandTypeMeta::QueryOnly
}
fn event(&self, _context: &mut Context, _args: &mut Tokenizer) -> Result<()> {
Err(ErrorCode::UndefinedHeader.into())
}
};
}
#[macro_export]
macro_rules! nquery {
() => {
fn meta(&self) -> CommandTypeMeta {
CommandTypeMeta::NoQuery
}
fn query(
&self,
_context: &mut Context,
_args: &mut Tokenizer,
_response: &mut ResponseUnit,
) -> Result<()> {
Err(ErrorCode::UndefinedHeader.into())
}
};
}
#[cfg(test)]
mod test_command {
use crate::error::Result;
use crate::prelude::*;
struct Query {}
impl Command for Query {
qonly!();
fn query(
&self,
_context: &mut Context,
_args: &mut Tokenizer,
_response: &mut ResponseUnit,
) -> Result<()> {
Ok(())
}
}
#[test]
fn test_query() {
assert_eq!(Query {}.meta(), CommandTypeMeta::QueryOnly);
}
struct Event {}
impl Command for Event {
nquery!();
fn event(&self, _context: &mut Context, _args: &mut Tokenizer) -> Result<()> {
Ok(())
}
}
#[test]
fn test_event() {
assert_eq!(Event {}.meta(), CommandTypeMeta::NoQuery);
}
struct Default {}
impl Command for Default {
fn event(&self, _context: &mut Context, _args: &mut Tokenizer) -> Result<()> {
Ok(())
}
fn query(
&self,
_context: &mut Context,
_args: &mut Tokenizer,
_response: &mut ResponseUnit,
) -> Result<()> {
Ok(())
}
}
#[test]
fn test_default() {
assert_eq!(Default {}.meta(), CommandTypeMeta::Unknown);
}
}