tulpje_framework/handler/
event_handler.rs

1use std::hash::{Hash, Hasher};
2use std::{future::Future, pin::Pin};
3
4use twilight_gateway::EventType;
5
6use super::super::context::EventContext;
7use crate::Error;
8
9pub(crate) type EventFunc<T> =
10    fn(EventContext<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
11
12#[derive(Clone)]
13pub struct EventHandler<T: Clone + Send + Sync> {
14    pub module: String,
15    pub uuid: String,
16    pub event: EventType,
17    pub func: EventFunc<T>,
18}
19
20impl<T: Clone + Send + Sync> EventHandler<T> {
21    pub async fn run(&self, ctx: EventContext<T>) -> Result<(), Error> {
22        // can add more handling/parsing/etc here in the future
23        (self.func)(ctx).await
24    }
25}
26
27impl<T: Clone + Send + Sync> Hash for EventHandler<T> {
28    fn hash<H: Hasher>(&self, state: &mut H) {
29        self.uuid.hash(state);
30        self.event.hash(state);
31    }
32}
33
34impl<T: Clone + Send + Sync> PartialEq for EventHandler<T> {
35    fn eq(&self, other: &Self) -> bool {
36        self.event == other.event && self.uuid == other.uuid
37    }
38}
39
40impl<T: Clone + Send + Sync> Eq for EventHandler<T> {}