traq_ws_bot/bot/
handler.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use futures::Future;
5use paste::paste;
6
7use crate::events::{payload, Events};
8
9#[async_trait]
10pub trait Handler<T: Send + Sync + 'static>: Send + Sync + 'static {
11    async fn handle(&self, event: Events, resource: Arc<T>) -> ();
12}
13
14#[async_trait]
15impl<T: Send + Sync + 'static, Fut> Handler<T> for fn(Events) -> Fut
16where
17    Fut: Future<Output = ()> + Send + 'static,
18{
19    async fn handle(&self, event: Events, _: Arc<T>) {
20        (self)(event).await
21    }
22}
23#[async_trait]
24impl<T: Send + Sync + 'static, Fut> Handler<T> for fn(Events, Arc<T>) -> Fut
25where
26    Fut: Future<Output = ()> + Send + 'static,
27{
28    async fn handle(&self, event: Events, resource: Arc<T>) {
29        (self)(event, resource).await
30    }
31}
32
33#[async_trait]
34impl<T: Send + Sync + 'static, Fut> Handler<T> for fn(String) -> Fut
35where
36    Fut: Future<Output = ()> + Send + 'static,
37{
38    async fn handle(&self, event: Events, _: Arc<T>) {
39        if let Events::Error(payload) = event {
40            self(payload).await
41        }
42    }
43}
44#[async_trait]
45impl<T: Send + Sync + 'static, Fut> Handler<T> for fn(String, Arc<T>) -> Fut
46where
47    Fut: Future<Output = ()> + Send + 'static,
48{
49    async fn handle(&self, event: Events, resource: Arc<T>) {
50        if let Events::Error(payload) = event {
51            self(payload, resource).await
52        }
53    }
54}
55
56macro_rules! x_handler_structs {
57    ($($x:ident),*$(,)?) => {
58        $(
59            paste! {
60                #[async_trait]
61                impl<T: Send + Sync + 'static, Fut> Handler<T> for fn(payload::[<$x:camel>]) -> Fut
62                where
63                    Fut: Future<Output = ()> + Send + 'static,
64                {
65                    async fn handle(&self, event: Events, _: Arc<T>) {
66                        if let Events::[<$x:camel>](payload) = event {
67                            self(payload).await
68                        }
69                    }
70                }
71                #[async_trait]
72                impl<T: Send + Sync + 'static, Fut> Handler<T> for fn(payload::[<$x:camel>], Arc<T>) -> Fut
73                where
74                    Fut: Future<Output = ()> + Send + 'static,
75                {
76                    async fn handle(&self, event: Events, resource: Arc<T>) {
77                        if let Events::[<$x:camel>](payload) = event {
78                            self(payload, resource).await
79                        }
80                    }
81                }
82            }
83        )*
84    };
85}
86
87x_handler_structs!(
88    Ping,
89    Joined,
90    Left,
91    MessageCreated,
92    MessageUpdated,
93    MessageDeleted,
94    BotMessageStampsUpdated,
95    DirectMessageCreated,
96    DirectMessageUpdated,
97    DirectMessageDeleted,
98    ChannelCreated,
99    ChannelTopicChanged,
100    UserCreated,
101    StampCreated,
102    TagAdded,
103    TagRemoved,
104);