From 52f1b6d8a9527d84ac3460db4d3e517b924b8f81 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Tue, 31 Dec 2019 14:22:15 +0600 Subject: [PATCH] Use Either instead of ErrorKind --- Cargo.toml | 1 + src/dispatching/filter_dp.rs | 54 ++++++++++++++---------------------- src/dispatching/mod.rs | 4 +-- 3 files changed, 24 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 472bdb86..abc07eaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ futures-preview = "0.3.0-alpha.19" async-trait = "0.1.13" thiserror = "1.0.2" serde_with_macros = "1.0.1" +either = "1.5.3" [features] default = [] diff --git a/src/dispatching/filter_dp.rs b/src/dispatching/filter_dp.rs index 05da6f27..78aab5a2 100644 --- a/src/dispatching/filter_dp.rs +++ b/src/dispatching/filter_dp.rs @@ -9,6 +9,7 @@ use crate::{ UpdateKind, }, }; +use either::Either; type FilterWithHandler<'a, T, E> = (Box + 'a>, Box + 'a>); @@ -38,9 +39,7 @@ type FiltersWithHandlers<'a, T, E> = Vec>; /// use std::convert::Infallible; /// /// use teloxide::{ -/// dispatching::{ -/// dispatchers::filter_dp::FilterDispatcher, updaters::polling_basic, -/// }, +/// dispatching::{updaters::polling_basic, FilterDispatcher}, /// types::Message, /// Bot, /// }; @@ -83,19 +82,10 @@ pub struct FilterDispatcher<'a, E, Eh> { error_handler: Eh, } -/// An error produced either from [`Updater`] or [`Handler`]. -/// -/// [`Updater`]: crate::dispatching::Updater -/// [`Handler`]: crate::dispatching::Handler -pub enum ErrorKind { - FromUpdater(E1), - FromHandler(E2), -} - -impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { - pub fn new(error_handler: Eh) -> Self +impl<'a, HandlerE, Eh> FilterDispatcher<'a, HandlerE, Eh> { + pub fn new(error_handler: Eh) -> Self where - Eh: ErrorHandler>, + Eh: ErrorHandler>, { FilterDispatcher { message_handlers: Vec::new(), @@ -112,7 +102,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { pub fn message_handler(mut self, filter: F, handler: H) -> Self where F: Filter + 'a, - H: Handler + 'a, + H: Handler + 'a, { self.message_handlers .push((Box::new(filter), Box::new(handler))); @@ -122,7 +112,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { pub fn edited_message_handler(mut self, filter: F, handler: H) -> Self where F: Filter + 'a, - H: Handler + 'a, + H: Handler + 'a, { self.edited_message_handlers .push((Box::new(filter), Box::new(handler))); @@ -132,7 +122,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { pub fn channel_post_handler(mut self, filter: F, handler: H) -> Self where F: Filter + 'a, - H: Handler + 'a, + H: Handler + 'a, { self.channel_post_handlers .push((Box::new(filter), Box::new(handler))); @@ -146,7 +136,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { ) -> Self where F: Filter + 'a, - H: Handler + 'a, + H: Handler + 'a, { self.edited_channel_post_handlers .push((Box::new(filter), Box::new(handler))); @@ -156,7 +146,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { pub fn inline_query_handler(mut self, filter: F, handler: H) -> Self where F: Filter + 'a, - H: Handler + 'a, + H: Handler + 'a, { self.inline_query_handlers .push((Box::new(filter), Box::new(handler))); @@ -170,7 +160,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { ) -> Self where F: Filter + 'a, - H: Handler + 'a, + H: Handler + 'a, { self.chosen_inline_result_handlers .push((Box::new(filter), Box::new(handler))); @@ -180,17 +170,17 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { pub fn callback_query_handler(mut self, filter: F, handler: H) -> Self where F: Filter + 'a, - H: Handler + 'a, + H: Handler + 'a, { self.callback_query_handlers .push((Box::new(filter), Box::new(handler))); self } - pub async fn dispatch(&mut self, updater: U) + pub async fn dispatch(&mut self, updater: U) where - U: Updater + 'a, - Eh: ErrorHandler>, + U: Updater + 'a, + Eh: ErrorHandler>, { updater .for_each_concurrent(None, |res| async { @@ -198,7 +188,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { Ok(upd) => upd, Err(err) => { self.error_handler - .handle_error(ErrorKind::FromUpdater(err)) + .handle_error(Either::Left(err)) .await; return; } @@ -272,20 +262,18 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> { .await; } - async fn handle( + async fn handle( update: T, - handlers: &FiltersWithHandlers<'a, T, E2>, + handlers: &FiltersWithHandlers<'a, T, HandlerE>, error_handler: &Eh, ) where T: std::fmt::Debug, - Eh: ErrorHandler>, + Eh: ErrorHandler>, { for x in handlers { if x.0.test(&update) { if let Err(err) = x.1.handle(update).await { - error_handler - .handle_error(ErrorKind::FromHandler(err)) - .await + error_handler.handle_error(Either::Right(err)).await } return; @@ -304,7 +292,7 @@ mod tests { }; use crate::{ - dispatching::{dispatchers::filter_dp::FilterDispatcher, Updater}, + dispatching::{FilterDispatcher, Updater}, types::{ Chat, ChatKind, ForwardKind, MediaKind, Message, MessageKind, Sender, Update, UpdateKind, User, diff --git a/src/dispatching/mod.rs b/src/dispatching/mod.rs index d92740ae..f738757d 100644 --- a/src/dispatching/mod.rs +++ b/src/dispatching/mod.rs @@ -1,13 +1,13 @@ //! Update dispatching. -mod filter_dp; pub mod error_handlers; +mod filter_dp; pub mod filters; mod handler; pub mod updaters; pub use error_handlers::ErrorHandler; +pub use filter_dp::FilterDispatcher; pub use filters::Filter; pub use handler::Handler; pub use updaters::Updater; -pub use filter_dp::FilterDispatcher; \ No newline at end of file