Skip to main content

onebot_api/event/
meta.rs

1use std::collections::HashMap;
2
3#[cfg(feature = "selector")]
4use crate::selector::Selector;
5use serde::Deserialize;
6use serde_json::Value;
7use strum::{Display, EnumIs};
8
9#[derive(Deserialize, Debug, Copy, Clone, Display, EnumIs, Ord, PartialOrd, Eq, PartialEq)]
10pub enum LifecycleSubType {
11	#[serde(rename = "enable")]
12	Enable,
13	#[serde(rename = "disable")]
14	Disable,
15	#[serde(rename = "connect")]
16	Connect,
17}
18
19#[derive(Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
20pub struct MetaEventLifecycle {
21	pub sub_type: LifecycleSubType,
22}
23
24impl MetaEventLifecycle {
25	#[cfg(feature = "selector")]
26	pub fn selector(&'_ self) -> Selector<'_, Self> {
27		Selector { data: Some(self) }
28	}
29}
30
31#[cfg(feature = "selector")]
32impl<'a> Selector<'a, MetaEventLifecycle> {
33	pub fn filter(&mut self, f: impl FnOnce(MetaEventLifecycle) -> bool) {
34		if let Some(data) = self.data
35			&& !f(*data)
36		{
37			self.data = None
38		}
39	}
40
41	pub fn and_filter(mut self, f: impl FnOnce(MetaEventLifecycle) -> bool) -> Self {
42		self.filter(f);
43		self
44	}
45
46	pub async fn filter_async(&mut self, f: impl AsyncFnOnce(MetaEventLifecycle) -> bool) {
47		if let Some(data) = self.data
48			&& !f(*data).await
49		{
50			self.data = None
51		}
52	}
53
54	pub async fn and_filter_async(mut self, f: impl AsyncFnOnce(MetaEventLifecycle) -> bool) -> Self {
55		self.filter_async(f).await;
56		self
57	}
58
59	pub fn filter_sub_type(&mut self, f: impl FnOnce(LifecycleSubType) -> bool) {
60		if let Some(data) = self.data
61			&& !f(data.sub_type)
62		{
63			self.data = None
64		}
65	}
66
67	pub fn and_filter_sub_type(mut self, f: impl FnOnce(LifecycleSubType) -> bool) -> Self {
68		self.filter_sub_type(f);
69		self
70	}
71
72	pub async fn filter_sub_type_async(&mut self, f: impl AsyncFnOnce(LifecycleSubType) -> bool) {
73		if let Some(data) = self.data
74			&& !f(data.sub_type).await
75		{
76			self.data = None
77		}
78	}
79
80	pub async fn and_filter_sub_type_async(
81		mut self,
82		f: impl AsyncFnOnce(LifecycleSubType) -> bool,
83	) -> Self {
84		self.filter_sub_type_async(f).await;
85		self
86	}
87
88	pub fn enable(&mut self) {
89		if let Some(data) = self.data
90			&& !data.sub_type.is_enable()
91		{
92			self.data = None
93		}
94	}
95
96	pub fn and_enable(mut self) -> Self {
97		self.enable();
98		self
99	}
100
101	pub fn not_enable(&mut self) {
102		if let Some(data) = self.data
103			&& data.sub_type.is_enable()
104		{
105			self.data = None
106		}
107	}
108
109	pub fn and_not_enable(mut self) -> Self {
110		self.not_enable();
111		self
112	}
113
114	pub fn disable(&mut self) {
115		if let Some(data) = self.data
116			&& !data.sub_type.is_disable()
117		{
118			self.data = None
119		}
120	}
121
122	pub fn and_disable(mut self) -> Self {
123		self.disable();
124		self
125	}
126
127	pub fn not_disable(&mut self) {
128		if let Some(data) = self.data
129			&& data.sub_type.is_disable()
130		{
131			self.data = None
132		}
133	}
134
135	pub fn and_not_disable(mut self) -> Self {
136		self.not_disable();
137		self
138	}
139
140	pub fn connect(&mut self) {
141		if let Some(data) = self.data
142			&& !data.sub_type.is_connect()
143		{
144			self.data = None
145		}
146	}
147
148	pub fn and_connect(mut self) -> Self {
149		self.connect();
150		self
151	}
152
153	pub fn not_connect(&mut self) {
154		if let Some(data) = self.data
155			&& data.sub_type.is_connect()
156		{
157			self.data = None
158		}
159	}
160
161	pub fn and_not_connect(mut self) -> Self {
162		self.not_connect();
163		self
164	}
165}
166
167#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
168pub struct MetaEventHeartbeat {
169	pub status: HashMap<String, Value>,
170	pub interval: i64,
171}
172
173impl MetaEventHeartbeat {
174	#[cfg(feature = "selector")]
175	pub fn selector(&'_ self) -> Selector<'_, Self> {
176		Selector { data: Some(self) }
177	}
178}
179
180#[cfg(feature = "selector")]
181impl<'a> Selector<'a, MetaEventHeartbeat> {
182	pub fn filter(&mut self, f: impl FnOnce(&MetaEventHeartbeat) -> bool) {
183		if let Some(data) = self.data
184			&& !f(data)
185		{
186			self.data = None
187		}
188	}
189
190	pub fn and_filter(mut self, f: impl FnOnce(&MetaEventHeartbeat) -> bool) -> Self {
191		self.filter(f);
192		self
193	}
194
195	pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&MetaEventHeartbeat) -> bool) {
196		if let Some(data) = self.data
197			&& !f(data).await
198		{
199			self.data = None
200		}
201	}
202
203	pub async fn and_filter_async(
204		mut self,
205		f: impl AsyncFnOnce(&MetaEventHeartbeat) -> bool,
206	) -> Self {
207		self.filter_async(f).await;
208		self
209	}
210
211	pub fn filter_status(&mut self, f: impl FnOnce(&HashMap<String, Value>) -> bool) {
212		if let Some(data) = self.data
213			&& !f(&data.status)
214		{
215			self.data = None
216		}
217	}
218
219	pub fn and_filter_status(mut self, f: impl FnOnce(&HashMap<String, Value>) -> bool) -> Self {
220		self.filter_status(f);
221		self
222	}
223
224	pub async fn filter_status_async(
225		&mut self,
226		f: impl AsyncFnOnce(&HashMap<String, Value>) -> bool,
227	) {
228		if let Some(data) = self.data
229			&& !f(&data.status).await
230		{
231			self.data = None
232		}
233	}
234
235	pub async fn and_filter_status_async(
236		mut self,
237		f: impl AsyncFnOnce(&HashMap<String, Value>) -> bool,
238	) -> Self {
239		self.filter_status_async(f).await;
240		self
241	}
242
243	pub fn filter_interval(&mut self, f: impl FnOnce(i64) -> bool) {
244		if let Some(data) = self.data
245			&& !f(data.interval)
246		{
247			self.data = None
248		}
249	}
250
251	pub fn and_filter_interval(mut self, f: impl FnOnce(i64) -> bool) -> Self {
252		self.filter_interval(f);
253		self
254	}
255
256	pub async fn filter_interval_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
257		if let Some(data) = self.data
258			&& !f(data.interval).await
259		{
260			self.data = None
261		}
262	}
263
264	pub async fn and_filter_interval_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
265		self.filter_interval_async(f).await;
266		self
267	}
268}
269
270#[derive(Deserialize, Debug, Clone, Display, EnumIs, Eq, PartialEq)]
271#[serde(tag = "meta_event_type")]
272pub enum MetaEvent {
273	#[serde(rename = "lifecycle")]
274	Lifecycle(MetaEventLifecycle),
275
276	#[serde(rename = "heartbeat")]
277	Heartbeat(MetaEventHeartbeat),
278}
279
280impl MetaEvent {
281	#[cfg(feature = "selector")]
282	pub fn selector(&'_ self) -> Selector<'_, Self> {
283		Selector { data: Some(self) }
284	}
285
286	pub fn match_lifecycle(&self) -> Option<&MetaEventLifecycle> {
287		if let Self::Lifecycle(data) = self {
288			Some(data)
289		} else {
290			None
291		}
292	}
293
294	pub fn on_lifecycle<T>(&self, handler: impl FnOnce(&MetaEventLifecycle) -> T) -> Option<T> {
295		if let Self::Lifecycle(data) = self {
296			Some(handler(data))
297		} else {
298			None
299		}
300	}
301
302	pub async fn on_lifecycle_async<T>(
303		&self,
304		handler: impl AsyncFnOnce(&MetaEventLifecycle) -> T,
305	) -> Option<T> {
306		if let Self::Lifecycle(data) = self {
307			Some(handler(data).await)
308		} else {
309			None
310		}
311	}
312
313	pub fn match_heartbeat(&self) -> Option<&MetaEventHeartbeat> {
314		if let Self::Heartbeat(data) = self {
315			Some(data)
316		} else {
317			None
318		}
319	}
320
321	pub fn on_heartbeat<T>(&self, handler: impl FnOnce(&MetaEventHeartbeat) -> T) -> Option<T> {
322		if let Self::Heartbeat(data) = self {
323			Some(handler(data))
324		} else {
325			None
326		}
327	}
328
329	pub async fn on_heartbeat_async<T>(
330		&self,
331		handler: impl AsyncFnOnce(&MetaEventHeartbeat) -> T,
332	) -> Option<T> {
333		if let Self::Heartbeat(data) = self {
334			Some(handler(data).await)
335		} else {
336			None
337		}
338	}
339}
340
341#[cfg(feature = "selector")]
342impl<'a> Selector<'a, MetaEvent> {
343	pub fn lifecycle(&self) -> Selector<'a, MetaEventLifecycle> {
344		Selector {
345			data: self.data.and_then(|d| d.match_lifecycle()),
346		}
347	}
348
349	pub fn heartbeat(&self) -> Selector<'a, MetaEventHeartbeat> {
350		Selector {
351			data: self.data.and_then(|d| d.match_heartbeat()),
352		}
353	}
354}