tulpje_framework/handler/
event_handler.rs1use 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 #[tracing::instrument(name="event-handler", skip_all, fields(
22 module=self.module,
23 event=?self.event
24 ))]
25 pub async fn run(&self, ctx: EventContext<T>) -> Result<(), Error> {
26 (self.func)(ctx).await
28 }
29}
30
31impl<T: Clone + Send + Sync> Hash for EventHandler<T> {
32 fn hash<H: Hasher>(&self, state: &mut H) {
33 self.uuid.hash(state);
34 self.event.hash(state);
35 }
36}
37
38impl<T: Clone + Send + Sync> PartialEq for EventHandler<T> {
39 fn eq(&self, other: &Self) -> bool {
40 self.event == other.event && self.uuid == other.uuid
41 }
42}
43
44impl<T: Clone + Send + Sync> Eq for EventHandler<T> {}