Skip to main content

aionbot_core/router/
matcher.rs

1use crate::event::Event;
2
3use super::Router;
4
5pub struct ExactMatchRouter<T>
6where
7    T: Send + Sync + PartialEq + 'static,
8{
9    pub pattern: T,
10}
11
12impl<T> Router for ExactMatchRouter<T>
13where
14    T: Send + Sync + PartialEq + 'static,
15{
16    fn matches(&self, event: &dyn Event) -> bool {
17        if let Ok(val) = event.content().downcast::<T>() {
18            *val == self.pattern
19        } else {
20            false
21        }
22    }
23}
24
25impl<T> ExactMatchRouter<T>
26where
27    T: Send + Sync + PartialEq + 'static,
28{
29    pub fn new(pattern: T) -> Self {
30        Self { pattern }
31    }
32}
33
34pub struct StartsWithRouter<T>
35where
36    T: Send + Sync + AsRef<str> + 'static,
37{
38    pub pattern: T,
39}
40
41impl Router for StartsWithRouter<&str> {
42    fn matches(&self, event: &dyn Event) -> bool {
43        if let Ok(val) = event.content().downcast::<&str>() {
44            val.starts_with(self.pattern)
45        } else {
46            false
47        }
48    }
49}
50
51impl<T> StartsWithRouter<T>
52where
53    T: Send + Sync + AsRef<str> + 'static,
54{
55    pub fn new(pattern: T) -> Self {
56        Self { pattern }
57    }
58}
59
60pub struct ContainsRouter<T>
61where
62    T: Send + Sync + AsRef<str> + 'static,
63{
64    pub pattern: T,
65}
66
67impl Router for ContainsRouter<&str> {
68    fn matches(&self, event: &dyn Event) -> bool {
69        if let Ok(val) = event.content().downcast::<&str>() {
70            val.contains(self.pattern)
71        } else {
72            false
73        }
74    }
75}
76
77impl<T> ContainsRouter<T>
78where
79    T: Send + Sync + AsRef<str> + 'static,
80{
81    pub fn new(pattern: T) -> Self {
82        Self { pattern }
83    }
84}
85
86pub struct EndsWithRouter<T>
87where
88    T: Send + Sync + AsRef<str> + 'static,
89{
90    pub pattern: T,
91}
92
93impl Router for EndsWithRouter<&str> {
94    fn matches(&self, event: &dyn Event) -> bool {
95        if let Ok(val) = event.content().downcast::<&str>() {
96            val.ends_with(self.pattern)
97        } else {
98            false
99        }
100    }
101}
102
103impl<T> EndsWithRouter<T>
104where
105    T: Send + Sync + AsRef<str> + 'static,
106{
107    pub fn new(pattern: T) -> Self {
108        Self { pattern }
109    }
110}