botx_api_framework/handlers/
command.rs

1use std::{any::TypeId, ops::Deref, marker::PhantomData};
2
3use regex::Regex;
4
5use crate::{results::CommandResult, contexts::RequestContext};
6
7pub struct DefaultCommandHandlerId(pub TypeId);
8
9impl Deref for DefaultCommandHandlerId {
10    type Target = TypeId;
11
12    fn deref(&self) -> &Self::Target {
13        &self.0
14    }
15}
16
17#[derive(Clone)]
18pub struct CommandHandlerId(pub TypeId);
19
20impl Deref for CommandHandlerId {
21    type Target = TypeId;
22
23    fn deref(&self) -> &Self::Target {
24        &self.0
25    }
26}
27
28#[derive(Clone)]
29pub struct CommandDetectionRegex(pub Regex);
30
31impl Into<CommandDetectionRegex> for Regex {
32    fn into(self) -> CommandDetectionRegex {
33        CommandDetectionRegex(self)
34    }
35}
36
37pub trait ICommandHandlerMatchingRule: Sync + Send {
38    fn get_command_regex(&self) -> &CommandDetectionRegex;
39    fn get_command_handler_id(&self) -> CommandHandlerId;
40}
41
42#[derive(Clone)]
43pub struct CommandHandlerMatchingRule<TCommandHandler: ICommandHandler + 'static> {
44    pub command_regex: CommandDetectionRegex,
45    pub pd: PhantomData<TCommandHandler>,
46}
47
48impl<TCommandHandler: ICommandHandler + 'static> ICommandHandlerMatchingRule for CommandHandlerMatchingRule<TCommandHandler> {
49    fn get_command_regex(&self) -> &CommandDetectionRegex {
50        &self.command_regex
51    }
52
53    fn get_command_handler_id(&self) -> CommandHandlerId {
54        CommandHandlerId(TypeId::of::<TCommandHandler>())
55    }
56}
57
58impl Deref for CommandDetectionRegex {
59    type Target = Regex;
60
61    fn deref(&self) -> &Self::Target {
62        &self.0
63    }
64}
65
66#[async_trait_with_sync::async_trait]
67pub trait ICommandHandler: Send + Sync {
68    async fn handle(&mut self, command: String, request_context: RequestContext) -> CommandResult;
69}