Merge pull request #70 from telebofr/simple_dispatcher

attempt to replace macro to function
This commit is contained in:
Temirkhan Myrzamadi 2019-10-20 16:40:39 +00:00 committed by GitHub
commit 480d1a6f57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -227,16 +227,16 @@ where
match kind {
UpdateKind::Message(mes) => {
call!(self.message_handlers, mes)
self.handle_message(mes, &self.message_handlers).await;
}
UpdateKind::EditedMessage(mes) => {
call!(self.edited_message_handlers, mes)
self.handle_message(mes, &self.edited_message_handlers).await;
}
UpdateKind::ChannelPost(post) => {
call!(self.channel_post_handlers, post)
self.handle_message(post, &self.channel_post_handlers).await;
}
UpdateKind::EditedChannelPost(post) => {
call!(self.edited_channel_post_handlers, post)
self.handle_message(post, &self.edited_channel_post_handlers).await;
}
UpdateKind::InlineQuery(query) => {
call!(self.inline_query_handlers, query)
@ -252,6 +252,27 @@ where
})
.await;
}
async fn handle_message(&self, message: Message, handlers: &Handlers<'a, Message, E>) {
let handler = handlers.iter().find_map(|e|{
let (filter, handler) = e;
if filter.test(&message) {
Some(handler)
} else {
None
}
});
match handler {
Some(handler) => {
if let Err(err) = handler.handle(message).await {
self.error_policy.handle_error(err).await
}
}
None => {
log::warn!("unhandled update {:?}", message);
}
}
}
}
#[async_trait(? Send)]