tulpje_framework/handler/
event_handler.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::hash::{Hash, Hasher};
use std::{future::Future, pin::Pin};

use twilight_gateway::EventType;

use super::super::context::EventContext;
use crate::Error;

pub(crate) type EventFunc<T> =
    fn(EventContext<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;

#[derive(Clone)]
pub struct EventHandler<T: Clone + Send + Sync> {
    pub module: String,
    pub uuid: String,
    pub event: EventType,
    pub func: EventFunc<T>,
}

impl<T: Clone + Send + Sync> EventHandler<T> {
    pub async fn run(&self, ctx: EventContext<T>) -> Result<(), Error> {
        // can add more handling/parsing/etc here in the future
        (self.func)(ctx).await
    }
}

impl<T: Clone + Send + Sync> Hash for EventHandler<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.uuid.hash(state);
        self.event.hash(state);
    }
}

impl<T: Clone + Send + Sync> PartialEq for EventHandler<T> {
    fn eq(&self, other: &Self) -> bool {
        self.event == other.event && self.uuid == other.uuid
    }
}

impl<T: Clone + Send + Sync> Eq for EventHandler<T> {}