1use crate::event::{meta::MetaEvent, notice::NoticeEvent, request::RequestEvent};
2
3#[cfg(feature = "selector")]
4use crate::selector::Selector;
5
6use async_trait::async_trait;
7use message::MessageEvent;
8use serde::Deserialize;
9use strum::{Display, EnumIs};
10use tokio::sync::broadcast;
11
12pub mod message;
13pub mod meta;
14pub mod notice;
15pub mod request;
16
17#[derive(Deserialize, Debug, Clone)]
18pub struct EventMessage {
19 pub time: i64,
20 pub self_id: i64,
21 #[serde(flatten)]
22 pub data: Box<MessageEvent>,
23}
24
25impl EventMessage {
26 #[cfg(feature = "selector")]
27 pub fn selector(&'_ self) -> Selector<'_, Self> {
28 Selector { data: Some(self) }
29 }
30}
31
32#[cfg(feature = "selector")]
33impl<'a> Selector<'a, EventMessage> {
34 pub fn message_event_selector(&self) -> Selector<'a, MessageEvent> {
35 Selector {
36 data: self.data.map(|d| &*d.data),
37 }
38 }
39
40 pub fn filter(&mut self, f: impl FnOnce(&'a EventMessage) -> bool) {
41 if let Some(data) = self.data
42 && !f(data)
43 {
44 self.data = None
45 }
46 }
47
48 pub fn and_filter(mut self, f: impl FnOnce(&'a EventMessage) -> bool) -> Self {
49 self.filter(f);
50 self
51 }
52
53 pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&'a EventMessage) -> bool) {
54 if let Some(data) = self.data
55 && !f(data).await
56 {
57 self.data = None
58 }
59 }
60
61 pub async fn and_filter_async(mut self, f: impl AsyncFnOnce(&'a EventMessage) -> bool) -> Self {
62 self.filter_async(f).await;
63 self
64 }
65
66 pub fn filter_time(&mut self, f: impl FnOnce(i64) -> bool) {
67 if let Some(data) = self.data
68 && !f(data.time)
69 {
70 self.data = None
71 }
72 }
73
74 pub fn and_filter_time(mut self, f: impl FnOnce(i64) -> bool) -> Self {
75 self.filter_time(f);
76 self
77 }
78
79 pub async fn filter_time_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
80 if let Some(data) = self.data
81 && !f(data.time).await
82 {
83 self.data = None
84 }
85 }
86
87 pub async fn and_filter_time_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
88 self.filter_time_async(f).await;
89 self
90 }
91
92 pub fn filter_self_id(&mut self, f: impl FnOnce(i64) -> bool) {
93 if let Some(data) = self.data
94 && !f(data.self_id)
95 {
96 self.data = None
97 }
98 }
99
100 pub fn and_filter_self_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
101 self.filter_self_id(f);
102 self
103 }
104
105 pub async fn filter_self_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
106 if let Some(data) = self.data
107 && !f(data.self_id).await
108 {
109 self.data = None
110 }
111 }
112
113 pub async fn and_filter_self_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
114 self.filter_self_id_async(f).await;
115 self
116 }
117
118 pub fn filter_message_event(&mut self, f: impl FnOnce(&MessageEvent) -> bool) {
119 if let Some(data) = self.data
120 && !f(&data.data)
121 {
122 self.data = None
123 }
124 }
125
126 pub fn and_filter_message_event(mut self, f: impl FnOnce(&MessageEvent) -> bool) -> Self {
127 self.filter_message_event(f);
128 self
129 }
130
131 pub async fn filter_message_event_async(&mut self, f: impl AsyncFnOnce(&MessageEvent) -> bool) {
132 if let Some(data) = self.data
133 && !f(&data.data).await
134 {
135 self.data = None
136 }
137 }
138
139 pub async fn and_filter_message_event_async(
140 mut self,
141 f: impl AsyncFnOnce(&MessageEvent) -> bool,
142 ) -> Self {
143 self.filter_message_event_async(f).await;
144 self
145 }
146}
147
148#[derive(Deserialize, Debug, Clone)]
149pub struct EventNotice {
150 pub time: i64,
151 pub self_id: i64,
152 #[serde(flatten)]
153 pub data: NoticeEvent,
154}
155
156impl EventNotice {
157 #[cfg(feature = "selector")]
158 pub fn selector(&'_ self) -> Selector<'_, Self> {
159 Selector { data: Some(self) }
160 }
161}
162
163#[cfg(feature = "selector")]
164impl<'a> Selector<'a, EventNotice> {
165 pub fn notice_event_selector(&self) -> Selector<'a, NoticeEvent> {
166 Selector {
167 data: self.data.map(|d| &d.data),
168 }
169 }
170
171 pub fn filter(&mut self, f: impl FnOnce(&'a EventNotice) -> bool) {
172 if let Some(data) = self.data
173 && !f(data)
174 {
175 self.data = None
176 }
177 }
178
179 pub fn and_filter(mut self, f: impl FnOnce(&'a EventNotice) -> bool) -> Self {
180 self.filter(f);
181 self
182 }
183
184 pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&'a EventNotice) -> bool) {
185 if let Some(data) = self.data
186 && !f(data).await
187 {
188 self.data = None
189 }
190 }
191
192 pub async fn and_filter_async(mut self, f: impl AsyncFnOnce(&'a EventNotice) -> bool) -> Self {
193 self.filter_async(f).await;
194 self
195 }
196
197 pub fn filter_time(&mut self, f: impl FnOnce(i64) -> bool) {
198 if let Some(data) = self.data
199 && !f(data.time)
200 {
201 self.data = None
202 }
203 }
204
205 pub fn and_filter_time(mut self, f: impl FnOnce(i64) -> bool) -> Self {
206 self.filter_time(f);
207 self
208 }
209
210 pub async fn filter_time_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
211 if let Some(data) = self.data
212 && !f(data.time).await
213 {
214 self.data = None
215 }
216 }
217
218 pub async fn and_filter_time_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
219 self.filter_time_async(f).await;
220 self
221 }
222
223 pub fn filter_self_id(&mut self, f: impl FnOnce(i64) -> bool) {
224 if let Some(data) = self.data
225 && !f(data.self_id)
226 {
227 self.data = None
228 }
229 }
230
231 pub fn and_filter_self_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
232 self.filter_self_id(f);
233 self
234 }
235
236 pub async fn filter_self_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
237 if let Some(data) = self.data
238 && !f(data.self_id).await
239 {
240 self.data = None
241 }
242 }
243
244 pub async fn and_filter_self_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
245 self.filter_self_id_async(f).await;
246 self
247 }
248
249 pub fn filter_notice_event(&mut self, f: impl FnOnce(&NoticeEvent) -> bool) {
250 if let Some(data) = self.data
251 && !f(&data.data)
252 {
253 self.data = None
254 }
255 }
256
257 pub fn and_filter_notice_event(mut self, f: impl FnOnce(&NoticeEvent) -> bool) -> Self {
258 self.filter_notice_event(f);
259 self
260 }
261
262 pub async fn filter_notice_event_async(&mut self, f: impl AsyncFnOnce(&NoticeEvent) -> bool) {
263 if let Some(data) = self.data
264 && !f(&data.data).await
265 {
266 self.data = None
267 }
268 }
269
270 pub async fn and_filter_notice_event_async(
271 mut self,
272 f: impl AsyncFnOnce(&NoticeEvent) -> bool,
273 ) -> Self {
274 self.filter_notice_event_async(f).await;
275 self
276 }
277}
278
279#[derive(Deserialize, Debug, Clone)]
280pub struct EventRequest {
281 pub time: i64,
282 pub self_id: i64,
283 #[serde(flatten)]
284 pub data: RequestEvent,
285}
286
287impl EventRequest {
288 #[cfg(feature = "selector")]
289 pub fn selector(&'_ self) -> Selector<'_, Self> {
290 Selector { data: Some(self) }
291 }
292}
293
294#[cfg(feature = "selector")]
295impl<'a> Selector<'a, EventRequest> {
296 pub fn request_event_selector(&self) -> Selector<'a, RequestEvent> {
297 Selector {
298 data: self.data.map(|d| &d.data),
299 }
300 }
301
302 pub fn filter(&mut self, f: impl FnOnce(&'a EventRequest) -> bool) {
303 if let Some(data) = self.data
304 && !f(data)
305 {
306 self.data = None
307 }
308 }
309
310 pub fn and_filter(mut self, f: impl FnOnce(&'a EventRequest) -> bool) -> Self {
311 self.filter(f);
312 self
313 }
314
315 pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&'a EventRequest) -> bool) {
316 if let Some(data) = self.data
317 && !f(data).await
318 {
319 self.data = None
320 }
321 }
322
323 pub async fn and_filter_async(mut self, f: impl AsyncFnOnce(&'a EventRequest) -> bool) -> Self {
324 self.filter_async(f).await;
325 self
326 }
327
328 pub fn filter_time(&mut self, f: impl FnOnce(i64) -> bool) {
329 if let Some(data) = self.data
330 && !f(data.time)
331 {
332 self.data = None
333 }
334 }
335
336 pub fn and_filter_time(mut self, f: impl FnOnce(i64) -> bool) -> Self {
337 self.filter_time(f);
338 self
339 }
340
341 pub async fn filter_time_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
342 if let Some(data) = self.data
343 && !f(data.time).await
344 {
345 self.data = None
346 }
347 }
348
349 pub async fn and_filter_time_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
350 self.filter_time_async(f).await;
351 self
352 }
353
354 pub fn filter_self_id(&mut self, f: impl FnOnce(i64) -> bool) {
355 if let Some(data) = self.data
356 && !f(data.self_id)
357 {
358 self.data = None
359 }
360 }
361
362 pub fn and_filter_self_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
363 self.filter_self_id(f);
364 self
365 }
366
367 pub async fn filter_self_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
368 if let Some(data) = self.data
369 && !f(data.self_id).await
370 {
371 self.data = None
372 }
373 }
374
375 pub async fn and_filter_self_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
376 self.filter_self_id_async(f).await;
377 self
378 }
379
380 pub fn filter_request_event(&mut self, f: impl FnOnce(&RequestEvent) -> bool) {
381 if let Some(data) = self.data
382 && !f(&data.data)
383 {
384 self.data = None
385 }
386 }
387
388 pub fn and_filter_request_event(mut self, f: impl FnOnce(&RequestEvent) -> bool) -> Self {
389 self.filter_request_event(f);
390 self
391 }
392
393 pub async fn filter_request_event_async(&mut self, f: impl AsyncFnOnce(&RequestEvent) -> bool) {
394 if let Some(data) = self.data
395 && !f(&data.data).await
396 {
397 self.data = None
398 }
399 }
400
401 pub async fn and_filter_request_event_async(
402 mut self,
403 f: impl AsyncFnOnce(&RequestEvent) -> bool,
404 ) -> Self {
405 self.filter_request_event_async(f).await;
406 self
407 }
408}
409
410#[derive(Deserialize, Debug, Clone)]
411pub struct EventMetaEvent {
412 pub time: i64,
413 pub self_id: i64,
414 #[serde(flatten)]
415 pub data: MetaEvent,
416}
417
418impl EventMetaEvent {
419 #[cfg(feature = "selector")]
420 pub fn selector(&'_ self) -> Selector<'_, Self> {
421 Selector { data: Some(self) }
422 }
423}
424
425#[cfg(feature = "selector")]
426impl<'a> Selector<'a, EventMetaEvent> {
427 pub fn meta_event_selector(&self) -> Selector<'a, MetaEvent> {
428 Selector {
429 data: self.data.map(|d| &d.data),
430 }
431 }
432
433 pub fn filter(&mut self, f: impl FnOnce(&'a EventMetaEvent) -> bool) {
434 if let Some(data) = self.data
435 && !f(data)
436 {
437 self.data = None
438 }
439 }
440
441 pub fn and_filter(mut self, f: impl FnOnce(&'a EventMetaEvent) -> bool) -> Self {
442 self.filter(f);
443 self
444 }
445
446 pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&'a EventMetaEvent) -> bool) {
447 if let Some(data) = self.data
448 && !f(data).await
449 {
450 self.data = None
451 }
452 }
453
454 pub async fn and_filter_async(mut self, f: impl AsyncFnOnce(&'a EventMetaEvent) -> bool) -> Self {
455 self.filter_async(f).await;
456 self
457 }
458
459 pub fn filter_time(&mut self, f: impl FnOnce(i64) -> bool) {
460 if let Some(data) = self.data
461 && !f(data.time)
462 {
463 self.data = None
464 }
465 }
466
467 pub fn and_filter_time(mut self, f: impl FnOnce(i64) -> bool) -> Self {
468 self.filter_time(f);
469 self
470 }
471
472 pub async fn filter_time_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
473 if let Some(data) = self.data
474 && !f(data.time).await
475 {
476 self.data = None
477 }
478 }
479
480 pub async fn and_filter_time_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
481 self.filter_time_async(f).await;
482 self
483 }
484
485 pub fn filter_self_id(&mut self, f: impl FnOnce(i64) -> bool) {
486 if let Some(data) = self.data
487 && !f(data.self_id)
488 {
489 self.data = None
490 }
491 }
492
493 pub fn and_filter_self_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
494 self.filter_self_id(f);
495 self
496 }
497
498 pub async fn filter_self_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
499 if let Some(data) = self.data
500 && !f(data.self_id).await
501 {
502 self.data = None
503 }
504 }
505
506 pub async fn and_filter_self_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
507 self.filter_self_id_async(f).await;
508 self
509 }
510
511 pub fn filter_meta_event(&mut self, f: impl FnOnce(&MetaEvent) -> bool) {
512 if let Some(data) = self.data
513 && !f(&data.data)
514 {
515 self.data = None
516 }
517 }
518
519 pub fn and_filter_meta_event(mut self, f: impl FnOnce(&MetaEvent) -> bool) -> Self {
520 self.filter_meta_event(f);
521 self
522 }
523
524 pub async fn filter_meta_event_async(&mut self, f: impl AsyncFnOnce(&MetaEvent) -> bool) {
525 if let Some(data) = self.data
526 && !f(&data.data).await
527 {
528 self.data = None
529 }
530 }
531
532 pub async fn and_filter_meta_event_async(
533 mut self,
534 f: impl AsyncFnOnce(&MetaEvent) -> bool,
535 ) -> Self {
536 self.filter_meta_event_async(f).await;
537 self
538 }
539}
540
541#[derive(Deserialize, Debug, Clone, Display, EnumIs)]
542#[serde(tag = "post_type")]
543pub enum Event {
544 #[serde(rename = "message")]
545 Message(EventMessage),
546
547 #[serde(rename = "notice")]
548 Notice(EventNotice),
549
550 #[serde(rename = "request")]
551 Request(EventRequest),
552
553 #[serde(rename = "meta_event")]
554 MetaEvent(EventMetaEvent),
555}
556
557impl Event {
558 #[cfg(feature = "selector")]
559 pub fn selector(&'_ self) -> Selector<'_, Event> {
560 Selector { data: Some(self) }
561 }
562
563 pub fn match_message(&self) -> Option<&EventMessage> {
564 if let Self::Message(data) = self {
565 Some(data)
566 } else {
567 None
568 }
569 }
570
571 pub fn on_message<T>(&self, handler: impl FnOnce(&EventMessage) -> T) -> Option<T> {
572 if let Self::Message(data) = self {
573 Some(handler(data))
574 } else {
575 None
576 }
577 }
578
579 pub async fn on_message_async<T>(
580 &self,
581 handler: impl AsyncFnOnce(&EventMessage) -> T,
582 ) -> Option<T> {
583 if let Self::Message(data) = self {
584 Some(handler(data).await)
585 } else {
586 None
587 }
588 }
589
590 pub fn match_notice(&self) -> Option<&EventNotice> {
591 if let Self::Notice(data) = self {
592 Some(data)
593 } else {
594 None
595 }
596 }
597
598 pub fn on_notice<T>(&self, handler: impl FnOnce(&EventNotice) -> T) -> Option<T> {
599 if let Self::Notice(data) = self {
600 Some(handler(data))
601 } else {
602 None
603 }
604 }
605
606 pub async fn on_notice_async<T>(
607 &self,
608 handler: impl AsyncFnOnce(&EventNotice) -> T,
609 ) -> Option<T> {
610 if let Self::Notice(data) = self {
611 Some(handler(data).await)
612 } else {
613 None
614 }
615 }
616
617 pub fn match_request(&self) -> Option<&EventRequest> {
618 if let Self::Request(data) = self {
619 Some(data)
620 } else {
621 None
622 }
623 }
624
625 pub fn on_request<T>(&self, handler: impl FnOnce(&EventRequest) -> T) -> Option<T> {
626 if let Self::Request(data) = self {
627 Some(handler(data))
628 } else {
629 None
630 }
631 }
632
633 pub async fn on_request_async<T>(
634 &self,
635 handler: impl AsyncFnOnce(&EventRequest) -> T,
636 ) -> Option<T> {
637 if let Self::Request(data) = self {
638 Some(handler(data).await)
639 } else {
640 None
641 }
642 }
643
644 pub fn match_meta_event(&self) -> Option<&EventMetaEvent> {
645 if let Self::MetaEvent(data) = self {
646 Some(data)
647 } else {
648 None
649 }
650 }
651
652 pub fn on_meta_event<T>(&self, handler: impl FnOnce(&EventMetaEvent) -> T) -> Option<T> {
653 if let Self::MetaEvent(data) = self {
654 Some(handler(data))
655 } else {
656 None
657 }
658 }
659
660 pub async fn on_meta_event_async<T>(
661 &self,
662 handler: impl AsyncFnOnce(&EventMetaEvent) -> T,
663 ) -> Option<T> {
664 if let Self::MetaEvent(data) = self {
665 Some(handler(data).await)
666 } else {
667 None
668 }
669 }
670}
671
672#[cfg(feature = "selector")]
673impl<'a> Selector<'a, Event> {
674 pub fn message(&self) -> Selector<'a, EventMessage> {
675 Selector {
676 data: self.data.and_then(|d| d.match_message()),
677 }
678 }
679
680 pub fn notice(&self) -> Selector<'a, EventNotice> {
681 Selector {
682 data: self.data.and_then(|d| d.match_notice()),
683 }
684 }
685
686 pub fn request(&self) -> Selector<'a, EventRequest> {
687 Selector {
688 data: self.data.and_then(|d| d.match_request()),
689 }
690 }
691
692 pub fn meta_event(&self) -> Selector<'a, EventMetaEvent> {
693 Selector {
694 data: self.data.and_then(|d| d.match_meta_event()),
695 }
696 }
697}
698
699pub trait EventTrait {}
700
701impl EventTrait for Event {}
702
703#[async_trait]
704pub trait EventReceiver<T: EventTrait> {
705 fn subscribe(&self) -> broadcast::Receiver<T>;
706}