mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-05 10:24:32 +01:00
Use Either instead of ErrorKind
This commit is contained in:
parent
43b417b9f3
commit
52f1b6d8a9
3 changed files with 24 additions and 35 deletions
|
@ -18,6 +18,7 @@ futures-preview = "0.3.0-alpha.19"
|
||||||
async-trait = "0.1.13"
|
async-trait = "0.1.13"
|
||||||
thiserror = "1.0.2"
|
thiserror = "1.0.2"
|
||||||
serde_with_macros = "1.0.1"
|
serde_with_macros = "1.0.1"
|
||||||
|
either = "1.5.3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|
|
@ -9,6 +9,7 @@ use crate::{
|
||||||
UpdateKind,
|
UpdateKind,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use either::Either;
|
||||||
|
|
||||||
type FilterWithHandler<'a, T, E> =
|
type FilterWithHandler<'a, T, E> =
|
||||||
(Box<dyn Filter<T> + 'a>, Box<dyn Handler<T, E> + 'a>);
|
(Box<dyn Filter<T> + 'a>, Box<dyn Handler<T, E> + 'a>);
|
||||||
|
@ -38,9 +39,7 @@ type FiltersWithHandlers<'a, T, E> = Vec<FilterWithHandler<'a, T, E>>;
|
||||||
/// use std::convert::Infallible;
|
/// use std::convert::Infallible;
|
||||||
///
|
///
|
||||||
/// use teloxide::{
|
/// use teloxide::{
|
||||||
/// dispatching::{
|
/// dispatching::{updaters::polling_basic, FilterDispatcher},
|
||||||
/// dispatchers::filter_dp::FilterDispatcher, updaters::polling_basic,
|
|
||||||
/// },
|
|
||||||
/// types::Message,
|
/// types::Message,
|
||||||
/// Bot,
|
/// Bot,
|
||||||
/// };
|
/// };
|
||||||
|
@ -83,19 +82,10 @@ pub struct FilterDispatcher<'a, E, Eh> {
|
||||||
error_handler: Eh,
|
error_handler: Eh,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An error produced either from [`Updater`] or [`Handler`].
|
impl<'a, HandlerE, Eh> FilterDispatcher<'a, HandlerE, Eh> {
|
||||||
///
|
pub fn new<UpdaterE>(error_handler: Eh) -> Self
|
||||||
/// [`Updater`]: crate::dispatching::Updater
|
|
||||||
/// [`Handler`]: crate::dispatching::Handler
|
|
||||||
pub enum ErrorKind<E1, E2> {
|
|
||||||
FromUpdater(E1),
|
|
||||||
FromHandler(E2),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
|
||||||
pub fn new<E1>(error_handler: Eh) -> Self
|
|
||||||
where
|
where
|
||||||
Eh: ErrorHandler<ErrorKind<E1, E2>>,
|
Eh: ErrorHandler<Either<UpdaterE, HandlerE>>,
|
||||||
{
|
{
|
||||||
FilterDispatcher {
|
FilterDispatcher {
|
||||||
message_handlers: Vec::new(),
|
message_handlers: Vec::new(),
|
||||||
|
@ -112,7 +102,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
pub fn message_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
pub fn message_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
||||||
where
|
where
|
||||||
F: Filter<Message> + 'a,
|
F: Filter<Message> + 'a,
|
||||||
H: Handler<Message, E2> + 'a,
|
H: Handler<Message, HandlerE> + 'a,
|
||||||
{
|
{
|
||||||
self.message_handlers
|
self.message_handlers
|
||||||
.push((Box::new(filter), Box::new(handler)));
|
.push((Box::new(filter), Box::new(handler)));
|
||||||
|
@ -122,7 +112,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
pub fn edited_message_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
pub fn edited_message_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
||||||
where
|
where
|
||||||
F: Filter<Message> + 'a,
|
F: Filter<Message> + 'a,
|
||||||
H: Handler<Message, E2> + 'a,
|
H: Handler<Message, HandlerE> + 'a,
|
||||||
{
|
{
|
||||||
self.edited_message_handlers
|
self.edited_message_handlers
|
||||||
.push((Box::new(filter), Box::new(handler)));
|
.push((Box::new(filter), Box::new(handler)));
|
||||||
|
@ -132,7 +122,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
pub fn channel_post_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
pub fn channel_post_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
||||||
where
|
where
|
||||||
F: Filter<Message> + 'a,
|
F: Filter<Message> + 'a,
|
||||||
H: Handler<Message, E2> + 'a,
|
H: Handler<Message, HandlerE> + 'a,
|
||||||
{
|
{
|
||||||
self.channel_post_handlers
|
self.channel_post_handlers
|
||||||
.push((Box::new(filter), Box::new(handler)));
|
.push((Box::new(filter), Box::new(handler)));
|
||||||
|
@ -146,7 +136,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
F: Filter<Message> + 'a,
|
F: Filter<Message> + 'a,
|
||||||
H: Handler<Message, E2> + 'a,
|
H: Handler<Message, HandlerE> + 'a,
|
||||||
{
|
{
|
||||||
self.edited_channel_post_handlers
|
self.edited_channel_post_handlers
|
||||||
.push((Box::new(filter), Box::new(handler)));
|
.push((Box::new(filter), Box::new(handler)));
|
||||||
|
@ -156,7 +146,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
pub fn inline_query_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
pub fn inline_query_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
||||||
where
|
where
|
||||||
F: Filter<InlineQuery> + 'a,
|
F: Filter<InlineQuery> + 'a,
|
||||||
H: Handler<InlineQuery, E2> + 'a,
|
H: Handler<InlineQuery, HandlerE> + 'a,
|
||||||
{
|
{
|
||||||
self.inline_query_handlers
|
self.inline_query_handlers
|
||||||
.push((Box::new(filter), Box::new(handler)));
|
.push((Box::new(filter), Box::new(handler)));
|
||||||
|
@ -170,7 +160,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
F: Filter<ChosenInlineResult> + 'a,
|
F: Filter<ChosenInlineResult> + 'a,
|
||||||
H: Handler<ChosenInlineResult, E2> + 'a,
|
H: Handler<ChosenInlineResult, HandlerE> + 'a,
|
||||||
{
|
{
|
||||||
self.chosen_inline_result_handlers
|
self.chosen_inline_result_handlers
|
||||||
.push((Box::new(filter), Box::new(handler)));
|
.push((Box::new(filter), Box::new(handler)));
|
||||||
|
@ -180,17 +170,17 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
pub fn callback_query_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
pub fn callback_query_handler<F, H>(mut self, filter: F, handler: H) -> Self
|
||||||
where
|
where
|
||||||
F: Filter<CallbackQuery> + 'a,
|
F: Filter<CallbackQuery> + 'a,
|
||||||
H: Handler<CallbackQuery, E2> + 'a,
|
H: Handler<CallbackQuery, HandlerE> + 'a,
|
||||||
{
|
{
|
||||||
self.callback_query_handlers
|
self.callback_query_handlers
|
||||||
.push((Box::new(filter), Box::new(handler)));
|
.push((Box::new(filter), Box::new(handler)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dispatch<E1, U>(&mut self, updater: U)
|
pub async fn dispatch<UpdaterE, U>(&mut self, updater: U)
|
||||||
where
|
where
|
||||||
U: Updater<E1> + 'a,
|
U: Updater<UpdaterE> + 'a,
|
||||||
Eh: ErrorHandler<ErrorKind<E1, E2>>,
|
Eh: ErrorHandler<Either<UpdaterE, HandlerE>>,
|
||||||
{
|
{
|
||||||
updater
|
updater
|
||||||
.for_each_concurrent(None, |res| async {
|
.for_each_concurrent(None, |res| async {
|
||||||
|
@ -198,7 +188,7 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
Ok(upd) => upd,
|
Ok(upd) => upd,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
self.error_handler
|
self.error_handler
|
||||||
.handle_error(ErrorKind::FromUpdater(err))
|
.handle_error(Either::Left(err))
|
||||||
.await;
|
.await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -272,20 +262,18 @@ impl<'a, E2, Eh> FilterDispatcher<'a, E2, Eh> {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle<T, E1>(
|
async fn handle<T, UpdaterE>(
|
||||||
update: T,
|
update: T,
|
||||||
handlers: &FiltersWithHandlers<'a, T, E2>,
|
handlers: &FiltersWithHandlers<'a, T, HandlerE>,
|
||||||
error_handler: &Eh,
|
error_handler: &Eh,
|
||||||
) where
|
) where
|
||||||
T: std::fmt::Debug,
|
T: std::fmt::Debug,
|
||||||
Eh: ErrorHandler<ErrorKind<E1, E2>>,
|
Eh: ErrorHandler<Either<UpdaterE, HandlerE>>,
|
||||||
{
|
{
|
||||||
for x in handlers {
|
for x in handlers {
|
||||||
if x.0.test(&update) {
|
if x.0.test(&update) {
|
||||||
if let Err(err) = x.1.handle(update).await {
|
if let Err(err) = x.1.handle(update).await {
|
||||||
error_handler
|
error_handler.handle_error(Either::Right(err)).await
|
||||||
.handle_error(ErrorKind::FromHandler(err))
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -304,7 +292,7 @@ mod tests {
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
dispatching::{dispatchers::filter_dp::FilterDispatcher, Updater},
|
dispatching::{FilterDispatcher, Updater},
|
||||||
types::{
|
types::{
|
||||||
Chat, ChatKind, ForwardKind, MediaKind, Message, MessageKind,
|
Chat, ChatKind, ForwardKind, MediaKind, Message, MessageKind,
|
||||||
Sender, Update, UpdateKind, User,
|
Sender, Update, UpdateKind, User,
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
//! Update dispatching.
|
//! Update dispatching.
|
||||||
|
|
||||||
mod filter_dp;
|
|
||||||
pub mod error_handlers;
|
pub mod error_handlers;
|
||||||
|
mod filter_dp;
|
||||||
pub mod filters;
|
pub mod filters;
|
||||||
mod handler;
|
mod handler;
|
||||||
pub mod updaters;
|
pub mod updaters;
|
||||||
|
|
||||||
pub use error_handlers::ErrorHandler;
|
pub use error_handlers::ErrorHandler;
|
||||||
|
pub use filter_dp::FilterDispatcher;
|
||||||
pub use filters::Filter;
|
pub use filters::Filter;
|
||||||
pub use handler::Handler;
|
pub use handler::Handler;
|
||||||
pub use updaters::Updater;
|
pub use updaters::Updater;
|
||||||
pub use filter_dp::FilterDispatcher;
|
|
Loading…
Reference in a new issue