Skip to main content

teloxide_ng/dispatching/
filter_ext.rs

1#![allow(clippy::redundant_closure_call)]
2// Required for the `filter_from` currently
3#![allow(deprecated)]
4
5use dptree::Handler;
6
7use crate::{
8    dispatching::DpHandlerDescription,
9    types::{AllowedUpdate, Message, Update, UpdateKind},
10};
11
12macro_rules! define_ext {
13    ($ext_name:ident, $for_ty:ty => $( ($func:ident, $proj_fn:expr, $fn_doc:expr $(, $Allowed:ident)? ) ,)*) => {
14        #[doc = concat!("Filter methods for [`", stringify!($for_ty), "`].")]
15        pub trait $ext_name<Out>: private::Sealed {
16            $( define_ext!(@sig $func, $fn_doc); )*
17        }
18
19        impl<Out> $ext_name<Out> for $for_ty
20        where
21            Out: Send + Sync + 'static,
22        {
23            $( define_ext!(@impl $for_ty, $func, $proj_fn $(, $Allowed )? ); )*
24        }
25    };
26
27    (@sig $func:ident, $fn_doc:expr) => {
28        #[doc = $fn_doc]
29        fn $func() -> Handler<'static, Out, DpHandlerDescription>;
30    };
31
32    (@impl $for_ty:ty, $func:ident, $proj_fn:expr, $Allowed:ident) => {
33        fn $func() -> Handler<'static, Out, DpHandlerDescription> {
34            dptree::filter_map_with_description(DpHandlerDescription::of(AllowedUpdate::$Allowed), move |input: $for_ty| {
35                $proj_fn(input)
36            })
37        }
38    };
39
40    (@impl $for_ty:ty, $func:ident, $proj_fn:expr) => {
41        fn $func() -> Handler<'static, Out, DpHandlerDescription> {
42            dptree::filter_map(move |input: $for_ty| {
43                $proj_fn(input)
44            })
45        }
46    };
47}
48
49mod private {
50    use teloxide_core_ng::types::{Message, Update};
51
52    pub trait Sealed {}
53
54    impl Sealed for Update {}
55    impl Sealed for Message {}
56}
57
58// FIXME: rewrite this macro to allow the usage of functions returning small
59// values without borrowing
60macro_rules! define_message_ext {
61    ($( ($func:ident, $fn_name:path) ,)*) => {
62        define_ext! {
63            MessageFilterExt, Message =>
64            $((
65                $func,
66                (|x| $fn_name(&x).map(ToOwned::to_owned)),
67                concat!("Applies the [`", stringify!($fn_name), "`] filter.")
68            ),)*
69        }
70    }
71}
72
73// FIXME: change macro so that we can filter things without getters
74// May be expanded in the future.
75define_message_ext! {
76    // MessageCommon
77    (filter_from, Message::from),
78    // MediaKind variants of the MessageKind::Common
79    (filter_animation, Message::animation),
80    (filter_audio, Message::audio),
81    (filter_contact, Message::contact),
82    (filter_document, Message::document),
83    (filter_paid_media, Message::paid_media),
84    (filter_game, Message::game),
85    (filter_venue, Message::venue),
86    (filter_location, Message::location),
87    (filter_photo, Message::photo),
88    (filter_poll, Message::poll),
89    (filter_checklist, Message::checklist),
90    (filter_sticker, Message::sticker),
91    (filter_story, Message::story),
92    (filter_text, Message::text),
93    (filter_video, Message::video),
94    (filter_video_note, Message::video_note),
95    (filter_voice, Message::voice),
96    (filter_migration, Message::chat_migration),
97    (filter_migration_from, Message::migrate_from_chat_id),
98    (filter_migration_to, Message::migrate_to_chat_id),
99    (filter_reply_to_message, Message::reply_to_message),
100    (filter_forward_origin, Message::forward_origin),
101    (filter_reply_to_story, Message::reply_to_story),
102    // Rest variants of a MessageKind
103    (filter_new_chat_members, Message::new_chat_members),
104    (filter_left_chat_member, Message::left_chat_member),
105    (filter_new_chat_title, Message::new_chat_title),
106    (filter_new_chat_photo, Message::new_chat_photo),
107    (filter_delete_chat_photo, Message::delete_chat_photo),
108    (filter_group_chat_created, Message::group_chat_created),
109    (filter_supergroup_chat_created, Message::super_group_chat_created),
110    (filter_channel_chat_created, Message::channel_chat_created),
111    (filter_message_auto_delete_timer_changed, Message::message_auto_delete_timer_changed),
112    (filter_pinned, Message::pinned_message),
113    (filter_invoice, Message::invoice),
114    (filter_successful_payment, Message::successful_payment),
115    (filter_connected_website, Message::connected_website),
116    (filter_write_access_allowed, Message::write_access_allowed),
117    (filter_passport_data, Message::passport_data),
118    (filter_dice, Message::dice),
119    (filter_proximity_alert_triggered, Message::proximity_alert_triggered),
120    (filter_boost_added, Message::boost_added),
121    (filter_chat_background_set, Message::chat_background_set),
122    (filter_checklist_tasks_done, Message::checklist_tasks_done),
123    (filter_checklist_tasks_added, Message::checklist_tasks_added),
124    (filter_direct_message_price_changed, Message::direct_message_price_changed),
125    (filter_forum_topic_created, Message::forum_topic_created),
126    (filter_forum_topic_edited, Message::forum_topic_edited),
127    (filter_forum_topic_closed, Message::forum_topic_closed),
128    (filter_forum_topic_reopened, Message::forum_topic_reopened),
129    (filter_general_forum_topic_hidden, Message::general_forum_topic_hidden),
130    (filter_general_forum_topic_unhidden, Message::general_forum_topic_unhidden),
131    (filter_giveaway, Message::giveaway),
132    (filter_giveaway_completed, Message::giveaway_completed),
133    (filter_giveaway_created, Message::giveaway_created),
134    (filter_giveaway_winners, Message::giveaway_winners),
135    (filter_paid_message_price_changed, Message::paid_message_price_changed),
136    (filter_gift_info, Message::gift_info),
137    (filter_unique_gift_info, Message::unique_gift_info),
138    (filter_video_chat_scheduled, Message::video_chat_scheduled),
139    (filter_video_chat_started, Message::video_chat_started),
140    (filter_video_chat_ended, Message::video_chat_ended),
141    (filter_video_chat_participants_invited, Message::video_chat_participants_invited),
142    (filter_web_app_data, Message::web_app_data),
143}
144
145macro_rules! define_update_ext {
146    ($( ($func:ident, $kind:path, $Allowed:ident) ,)*) => {
147        define_ext! {
148            UpdateFilterExt, Update =>
149            $((
150                $func,
151                |update: Update| match update.kind {
152                    $kind(x) => Some(x),
153                    _ => None,
154                },
155                concat!("Filters out [`", stringify!($kind), "`] objects."),
156                $Allowed
157            ),)*
158        }
159    }
160}
161
162define_update_ext! {
163    (filter_message, UpdateKind::Message, Message),
164    (filter_edited_message, UpdateKind::EditedMessage, EditedMessage),
165    (filter_channel_post, UpdateKind::ChannelPost, ChannelPost),
166    (filter_edited_channel_post, UpdateKind::EditedChannelPost, EditedChannelPost),
167    (filter_business_connection, UpdateKind::BusinessConnection, BusinessConnection),
168    (filter_business_message, UpdateKind::BusinessMessage, BusinessMessage),
169    (filter_edited_business_message, UpdateKind::EditedBusinessMessage, EditedBusinessMessage),
170    (filter_deleted_business_messages, UpdateKind::DeletedBusinessMessages, DeletedBusinessMessages),
171    (filter_message_reaction_updated, UpdateKind::MessageReaction, MessageReaction),
172    (filter_message_reaction_count_updated, UpdateKind::MessageReactionCount, MessageReactionCount),
173    (filter_inline_query, UpdateKind::InlineQuery, InlineQuery),
174    (filter_chosen_inline_result, UpdateKind::ChosenInlineResult, ChosenInlineResult),
175    (filter_callback_query, UpdateKind::CallbackQuery, CallbackQuery),
176    (filter_shipping_query, UpdateKind::ShippingQuery, ShippingQuery),
177    (filter_pre_checkout_query, UpdateKind::PreCheckoutQuery, PreCheckoutQuery),
178    (filter_purchased_paid_media, UpdateKind::PurchasedPaidMedia, PurchasedPaidMedia),
179    (filter_poll, UpdateKind::Poll, Poll),
180    (filter_poll_answer, UpdateKind::PollAnswer, PollAnswer),
181    (filter_my_chat_member, UpdateKind::MyChatMember, MyChatMember),
182    (filter_chat_member, UpdateKind::ChatMember, ChatMember),
183    (filter_chat_join_request, UpdateKind::ChatJoinRequest, ChatJoinRequest),
184    (filter_chat_boost, UpdateKind::ChatBoost, ChatBoost),
185    (filter_removed_chat_boost, UpdateKind::RemovedChatBoost, RemovedChatBoost),
186}