Add UpdateFilterExt

This commit is contained in:
Hirrolot 2022-01-23 20:38:12 +06:00
parent 01c43dd360
commit ad88db96b6
3 changed files with 37 additions and 25 deletions

View file

@ -264,20 +264,4 @@ where
pub fn dependencies(self, dependencies: DependencyMap) -> Self {
Dispatcher { dependencies, ..self }
}
define_handlers! {
(messages_handler, Message),
(edited_messages_handler, EditedMessage),
(channel_posts_handler, ChannelPost),
(edited_channel_posts_handler, EditedChannelPost),
(inline_queries_handler, InlineQuery),
(chosen_inline_results_handler, ChosenInlineResult),
(callback_queries_handler, CallbackQuery),
(shipping_queries_handler, ShippingQuery),
(pre_checkout_queries_handler, PreCheckoutQuery),
(polls_handler, Poll),
(poll_answers_handler, PollAnswer),
(my_chat_members_handler, MyChatMember),
(chat_members_handler, ChatMember),
}
}

View file

@ -1,8 +1,8 @@
use dptree::{di::DependencyMap, Handler};
use teloxide_core::types::Message;
use teloxide_core::types::{Message, Update, UpdateKind};
macro_rules! define_ext {
($ext_name:ident, $for_ty:ty => $( ($func:ident, $arg_ty:ty, $get_func:expr) ,)*) => {
($ext_name:ident, $for_ty:ty => $( ($func:ident, $arg_ty:ty, $proj_fn:expr) ,)*) => {
pub trait $ext_name<Out> {
$( define_ext!(@sig $func, $arg_ty); )*
}
@ -11,16 +11,18 @@ macro_rules! define_ext {
where
Out: Send + Sync + 'static,
{
$( define_ext!(@impl $for_ty, $func, $arg_ty, $get_func); )*
$( define_ext!(@impl $for_ty, $func, $arg_ty, $proj_fn); )*
}
};
(@sig $func:ident, $arg_ty:ty) => {
fn $func<F, Fut>() -> Handler<'static, DependencyMap, Out>;
};
(@impl $for_ty:ty, $func:ident, $arg_ty:ty, $get_func:expr) => {
(@impl $for_ty:ty, $func:ident, $arg_ty:ty, $proj_fn:expr) => {
fn $func<F, Fut>() -> Handler<'static, DependencyMap, Out> {
dptree::filter_map(move |msg: $for_ty| {
let result = $get_func(&msg).map(ToOwned::to_owned);
dptree::filter_map(move |input: $for_ty| {
let result = $proj_fn(&input).map(ToOwned::to_owned);
async move { result }
})
}
@ -28,7 +30,7 @@ macro_rules! define_ext {
}
// May be expanded in the future.
define_ext!(
define_ext! {
MessageFilterExt, Message =>
(filter_from, types::User, Message::from),
(filter_animation, types::Animation, Message::animation),
@ -46,4 +48,30 @@ define_ext!(
(filter_left_chat_member, types::User, Message::left_chat_member),
(filter_pinned, Message, Message::pinned_message),
(filter_dice, types::Dice, Message::dice),
);
}
macro_rules! kind {
($kind:ident) => {
|update: &Update| match update.kind {
UpdateKind::$kind(x) => Some(&x),
_ => None,
}
};
}
define_ext! {
UpdateFilterExt, Update =>
(filter_message, types::Message, kind![Message]),
(filter_edited_message, types::EditedMessage, kind![EditedMessage]),
(filter_channel_post, types::ChannelPost, kind![ChannelPost]),
(filter_edited_channel_post, types::EditedChannelPost, kind![EditedChannelPost]),
(filter_inline_query, types::InlineQuery, kind![InlineQuery]),
(filter_chosen_inline_result, types::ChosenInlineResult, kind![ChosenInlineResult]),
(filter_callback_query, types::CallbackQuery, kind![CallbackQuery]),
(filter_shipping_query, types::ShippingQuery, kind![ShippingQuery]),
(filter_pre_checkout_query, types::PreCheckoutQuery, kind![PreCheckoutQuery]),
(filter_poll, types::Poll, kind![Poll]),
(filter_poll_answer, types::PollAnswer, kind![PollAnswer]),
(filter_my_chat_member, types::MyChatMember, kind![MyChatMember]),
(filter_chat_member, types::ChatMember, kind![ChatMember]),
}

View file

@ -7,6 +7,6 @@ mod handler_ext;
mod handler_factory;
pub use dispatcher::{DefaultHandler, Dispatcher, UpdateHandler};
pub use filter_ext::MessageFilterExt;
pub use filter_ext::{MessageFilterExt, UpdateFilterExt};
pub use handler_ext::HandlerExt;
pub use handler_factory::HandlerFactory;