Reduce the Dispatcher::dispatch_with_listener complexity

This commit is contained in:
Temirkhan Myrzamadi 2020-02-03 01:32:01 +06:00
parent 5dafe892ac
commit b7b6cd988c

View file

@ -195,154 +195,50 @@ where
match update.kind { match update.kind {
UpdateKind::Message(message) => { UpdateKind::Message(message) => {
if let Some(message_handler) = &self.message_handler { self.handle(&self.message_handler, message).await
if let Err(error) = message_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: message,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::EditedMessage(message) => { UpdateKind::EditedMessage(message) => {
if let Some(edited_message_handler) = self.handle(&self.edited_message_handler, message).await
&self.edited_message_handler
{
if let Err(error) = edited_message_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: message,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::ChannelPost(post) => { UpdateKind::ChannelPost(post) => {
if let Some(channel_post_handler) = self.handle(&self.channel_post_handler, post).await
&self.channel_post_handler
{
if let Err(error) = channel_post_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: post,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::EditedChannelPost(post) => { UpdateKind::EditedChannelPost(post) => {
if let Some(edited_channel_post_handler) = self.handle(&self.edited_channel_post_handler, post)
&self.edited_channel_post_handler .await
{
if let Err(error) = edited_channel_post_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: post,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::InlineQuery(query) => { UpdateKind::InlineQuery(query) => {
if let Some(inline_query_handler) = self.handle(&self.inline_query_handler, query).await
&self.inline_query_handler
{
if let Err(error) = inline_query_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: query,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::ChosenInlineResult(result) => { UpdateKind::ChosenInlineResult(result) => {
if let Some(chosen_inline_result_handler) = self.handle(&self.chosen_inline_result_handler, result)
&self.chosen_inline_result_handler .await
{
if let Err(error) = chosen_inline_result_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: result,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::CallbackQuery(query) => { UpdateKind::CallbackQuery(query) => {
if let Some(callback_query_handler) = self.handle(&self.callback_query_handler, query).await
&self.callback_query_handler
{
if let Err(error) = callback_query_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: query,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::ShippingQuery(query) => { UpdateKind::ShippingQuery(query) => {
if let Some(shipping_query_handler) = self.handle(&self.shipping_query_handler, query).await
&self.shipping_query_handler
{
if let Err(error) = shipping_query_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: query,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::PreCheckoutQuery(query) => { UpdateKind::PreCheckoutQuery(query) => {
if let Some(pre_checkout_query_handler) = self.handle(&self.pre_checkout_query_handler, query)
&self.pre_checkout_query_handler .await
{
if let Err(error) = pre_checkout_query_handler
.handle(HandlerCtx {
bot: Arc::clone(&self.bot),
update: query,
})
.await
{
self.handlers_error_handler.handle(error).await;
}
}
} }
UpdateKind::Poll(poll) => { UpdateKind::Poll(poll) => {
self.handle(&self.poll_handler, poll).await; self.handle(&self.poll_handler, poll).await
} }
} }
}) })
.await .await
} }
async fn handle<U>(&self, handler: &Option<Box<dyn AsyncHandler<HandlerCtx<U>, Result<(), HandlerE>> + 'a>>, update: U) // Handles a single update.
{ async fn handle<Upd>(&self, handler: &H<'a, Upd, HandlerE>, update: Upd) {
if let Some(handler) = &handler { if let Some(handler) = &handler {
if let Err(error) = if let Err(error) = handler
handler
.handle(HandlerCtx { .handle(HandlerCtx {
bot: Arc::clone(&self.bot), bot: Arc::clone(&self.bot),
update update,
}) })
.await .await
{ {