teloxide/dispatching/
handler_description.rs1use std::collections::HashSet;
2
3use dptree::{
4 description::{EventKind, InterestSet},
5 HandlerDescription,
6};
7use teloxide_core::types::AllowedUpdate;
8
9#[derive(Debug, Clone)]
13pub struct DpHandlerDescription {
14 allowed: InterestSet<Kind>,
15}
16
17impl DpHandlerDescription {
18 pub(crate) fn of(allowed: AllowedUpdate) -> Self {
19 let mut set = HashSet::with_capacity(1);
20 set.insert(Kind(allowed));
21 Self { allowed: InterestSet::new_filter(set) }
22 }
23
24 pub(crate) fn allowed_updates(&self) -> Vec<AllowedUpdate> {
25 self.allowed.observed.iter().map(|&Kind(x)| x).collect()
26 }
27}
28
29impl HandlerDescription for DpHandlerDescription {
30 fn entry() -> Self {
31 Self { allowed: HandlerDescription::entry() }
32 }
33
34 fn user_defined() -> Self {
35 Self { allowed: HandlerDescription::user_defined() }
36 }
37
38 fn merge_chain(&self, other: &Self) -> Self {
39 Self { allowed: self.allowed.merge_chain(&other.allowed) }
40 }
41
42 fn merge_branch(&self, other: &Self) -> Self {
43 Self { allowed: self.allowed.merge_branch(&other.allowed) }
44 }
45}
46
47#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
48struct Kind(AllowedUpdate);
49
50impl EventKind for Kind {
51 fn full_set() -> HashSet<Self> {
52 use AllowedUpdate::*;
53
54 [
65 Message,
66 GuestMessage,
67 EditedMessage,
68 ChannelPost,
69 EditedChannelPost,
70 BusinessConnection,
71 BusinessMessage,
72 EditedBusinessMessage,
73 DeletedBusinessMessages,
74 MessageReaction,
75 MessageReactionCount,
76 InlineQuery,
77 ChosenInlineResult,
78 CallbackQuery,
79 ShippingQuery,
80 PreCheckoutQuery,
81 PurchasedPaidMedia,
82 Poll,
83 PollAnswer,
84 MyChatMember,
85 ChatMember,
86 ChatJoinRequest,
87 ChatBoost,
88 RemovedChatBoost,
89 ManagedBot,
90 StoppedMessageGeneration,
91 ]
92 .into_iter()
93 .map(Kind)
94 .collect()
95 }
96
97 fn empty_set() -> HashSet<Self> {
98 HashSet::new()
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use crate::{
105 dispatching::UpdateFilterExt,
106 types::{AllowedUpdate, Update as CoreUpdate},
107 };
108
109 #[cfg(feature = "macros")]
110 use crate::{
111 self as teloxide, dispatching::{handler_description::Kind, HandlerExt},
113 types::{AllowedUpdate::*, Update},
114 utils::command::BotCommands,
115 };
116 #[cfg(feature = "macros")]
117 use dptree::description::EventKind;
118
119 #[cfg(feature = "macros")]
120 #[derive(BotCommands, Clone)]
121 #[command(rename_rule = "lowercase")]
122 enum Cmd {
123 B,
124 }
125
126 #[test]
128 #[cfg(feature = "macros")]
129 fn discussion_648() {
130 let h =
131 dptree::entry().branch(Update::filter_my_chat_member().endpoint(|| async {})).branch(
132 Update::filter_message()
133 .branch(dptree::entry().filter_command::<Cmd>().endpoint(|| async {}))
134 .endpoint(|| async {}),
135 );
136
137 let mut v = h.description().allowed_updates();
138
139 v.sort_by_key(|&a| a as u8);
141
142 assert_eq!(v, [Message, MyChatMember])
143 }
144
145 #[test]
146 #[ignore = "this test requires `macros` feature"]
147 #[cfg(not(feature = "macros"))]
148 fn discussion_648() {
149 panic!("this test requires `macros` feature")
150 }
151
152 #[test]
153 fn guest_message() {
154 let handler = dptree::entry().branch(
155 <CoreUpdate as UpdateFilterExt<()>>::filter_guest_message().endpoint(|| async {}),
156 );
157 let allowed_updates = handler.description().allowed_updates();
158
159 assert_eq!(allowed_updates, [AllowedUpdate::GuestMessage]);
160 }
161
162 #[test]
163 fn stopped_message_generation() {
164 let handler = dptree::entry().branch(
165 <CoreUpdate as UpdateFilterExt<()>>::filter_stopped_message_generation()
166 .endpoint(|| async {}),
167 );
168 let allowed_updates = handler.description().allowed_updates();
169
170 assert_eq!(allowed_updates, [AllowedUpdate::StoppedMessageGeneration]);
171 }
172
173 #[test]
175 #[cfg(feature = "macros")]
176 fn allowed_updates_full_set() {
177 let full_set = Kind::full_set();
178 let allowed_updates_reference = vec![
179 Message,
180 GuestMessage,
181 EditedMessage,
182 ChannelPost,
183 EditedChannelPost,
184 MessageReaction,
185 MessageReactionCount,
186 InlineQuery,
187 ChosenInlineResult,
188 CallbackQuery,
189 ShippingQuery,
190 PreCheckoutQuery,
191 PurchasedPaidMedia,
192 Poll,
193 PollAnswer,
194 MyChatMember,
195 ChatMember,
196 ChatJoinRequest,
197 ChatBoost,
198 RemovedChatBoost,
199 ManagedBot,
200 StoppedMessageGeneration,
201 ];
202
203 for update in allowed_updates_reference {
204 match update {
205 Message
207 | GuestMessage
208 | EditedMessage
209 | ChannelPost
210 | EditedChannelPost
211 | MessageReaction
212 | MessageReactionCount
213 | InlineQuery
214 | ChosenInlineResult
215 | CallbackQuery
216 | ShippingQuery
217 | PreCheckoutQuery
218 | PurchasedPaidMedia
219 | Poll
220 | PollAnswer
221 | MyChatMember
222 | ChatMember
223 | ChatJoinRequest
224 | ChatBoost
225 | RemovedChatBoost
226 | ManagedBot
227 | StoppedMessageGeneration
228 | BusinessMessage
229 | BusinessConnection
230 | EditedBusinessMessage
231 | DeletedBusinessMessages => {
232 assert!(full_set.contains(&Kind(update)))
233 }
234 }
235 }
236 }
237}