Merge pull request #71 from telebofr/rename_dispatching

Refactor crate::dispatching
This commit is contained in:
Temirkhan Myrzamadi 2019-10-20 17:24:48 +00:00 committed by GitHub
commit 5e40bbf173
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 94 additions and 86 deletions

View file

@ -3,9 +3,9 @@ use futures::StreamExt;
use async_trait::async_trait;
use crate::{
dispatcher::{
filter::Filter, handler::Handler, simple::error_policy::ErrorPolicy,
updater::Updater,
dispatching::{
dispatchers::filter::error_policy::ErrorPolicy, filters::Filter,
handler::Handler, updater::Updater,
},
types::{CallbackQuery, ChosenInlineResult, Message, Update, UpdateKind},
};
@ -17,13 +17,13 @@ type Handlers<'a, T, E> =
/// Dispatcher that dispatches updates from telegram.
///
/// This is 'simple' implementation with following limitations:
/// This is 'filter' implementation with following limitations:
/// - Error (`E` generic parameter) _must_ implement [`std::fmt::Debug`]
/// - All 'handlers' are boxed
/// - Handler's fututres are also boxed
/// - [Custom error policy] is also boxed
/// - All errors from [updater] are ignored (TODO: remove this limitation)
/// - All handlers executed in order (this means that in dispatcher have 2
/// - All handlers executed in order (this means that in dispatching have 2
/// upadtes it will first execute some handler into complition with first
/// update and **then** search for handler for second update, this is probably
/// wrong)
@ -37,8 +37,8 @@ type Handlers<'a, T, E> =
/// async fn run() {
/// use std::convert::Infallible;
/// use telebofr::{
/// dispatcher::{
/// simple::{error_policy::ErrorPolicy, Dispatcher},
/// dispatching::{
/// dispatchers::filter::{error_policy::ErrorPolicy, FilterDispatcher},
/// updater::polling,
/// },
/// };
@ -49,9 +49,9 @@ type Handlers<'a, T, E> =
///
/// let bot = Bot::new("TOKEN");
///
/// // create dispatcher which handlers can't fail
/// // create dispatching which handlers can't fail
/// // with error policy that just ignores all errors (that can't ever happen)
/// let mut dp = Dispatcher::<Infallible>::new(ErrorPolicy::Ignore)
/// let mut dp = FilterDispatcher::<Infallible>::new(ErrorPolicy::Ignore)
/// // Add 'handler' that will handle all messages sent to the bot
/// .message_handler(true, |mes: Message| {
/// async move { println!("New message: {:?}", mes) }
@ -67,9 +67,9 @@ type Handlers<'a, T, E> =
///
/// [`std::fmt::Debug`]: std::fmt::Debug
/// [Custom error policy]:
/// crate::dispatcher::simple::error_policy::ErrorPolicy::Custom [updater]:
/// crate::dispatcher::updater
pub struct Dispatcher<'a, E> {
/// crate::dispatching::filter::error_policy::ErrorPolicy::Custom [updater]:
/// crate::dispatching::updater
pub struct FilterDispatcher<'a, E> {
message_handlers: Handlers<'a, Message, E>,
edited_message_handlers: Handlers<'a, Message, E>,
channel_post_handlers: Handlers<'a, Message, E>,
@ -80,12 +80,12 @@ pub struct Dispatcher<'a, E> {
error_policy: ErrorPolicy<'a, E>,
}
impl<'a, E> Dispatcher<'a, E>
impl<'a, E> FilterDispatcher<'a, E>
where
E: std::fmt::Debug, // TODO: Is this really necessary?
{
pub fn new(error_policy: ErrorPolicy<'a, E>) -> Self {
Dispatcher {
FilterDispatcher {
message_handlers: Vec::new(),
edited_message_handlers: Vec::new(),
channel_post_handlers: Vec::new(),
@ -293,13 +293,13 @@ where
}
#[async_trait(? Send)]
impl<'a, U, E> crate::dispatcher::Dispatcher<'a, U> for Dispatcher<'a, E>
impl<'a, U, E> crate::dispatching::Dispatcher<'a, U> for FilterDispatcher<'a, E>
where
E: std::fmt::Debug,
U: Updater + 'a,
{
async fn dispatch(&'a mut self, updater: U) {
Dispatcher::dispatch(self, updater).await
FilterDispatcher::dispatch(self, updater).await
}
}
@ -313,8 +313,10 @@ mod tests {
use futures::Stream;
use crate::{
dispatcher::{
simple::{error_policy::ErrorPolicy, Dispatcher},
dispatching::{
dispatchers::filter::{
error_policy::ErrorPolicy, FilterDispatcher,
},
updater::StreamUpdater,
},
types::{
@ -328,7 +330,7 @@ mod tests {
let counter = &AtomicI32::new(0);
let counter2 = &AtomicI32::new(0);
let mut dp = Dispatcher::<Infallible>::new(ErrorPolicy::Ignore)
let mut dp = FilterDispatcher::<Infallible>::new(ErrorPolicy::Ignore)
.message_handler(true, |_mes: Message| {
async move {
counter.fetch_add(1, Ordering::SeqCst);

View file

@ -0,0 +1,3 @@
pub use filter::FilterDispatcher;
pub mod filter;

View file

@ -6,7 +6,7 @@ pub trait Filter<T> {
}
/// ```
/// use telebofr::dispatcher::filter::Filter;
/// use telebofr::dispatching::filters::Filter;
///
/// let closure = |i: &i32| -> bool { *i >= 42 };
/// assert!(closure.test(&42));
@ -22,7 +22,7 @@ impl<T, F: Fn(&T) -> bool> Filter<T> for F {
}
/// ```
/// use telebofr::dispatcher::filter::Filter;
/// use telebofr::dispatching::filters::Filter;
///
/// assert!(true.test(&()));
/// assert_eq!(false.test(&()), false);
@ -42,7 +42,7 @@ impl<T> Filter<T> for bool {
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{And, Filter};
/// use telebofr::dispatching::filters::{And, Filter};
///
/// // Note: bool can be treated as `Filter` that always return self.
/// assert_eq!(And::new(true, false).test(&()), false);
@ -73,13 +73,13 @@ where
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{and, Filter};
/// use telebofr::dispatching::filters::{and, Filter};
///
/// assert!(and(true, true).test(&()));
/// assert_eq!(and(true, false).test(&()), false);
/// ```
///
/// [`And::new`]: crate::dispatcher::filter::And::new
/// [`And::new`]: crate::dispatching::filter::And::new
pub fn and<A, B>(a: A, b: B) -> And<A, B> {
And::new(a, b)
}
@ -93,7 +93,7 @@ pub fn and<A, B>(a: A, b: B) -> And<A, B> {
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{Filter, Or};
/// use telebofr::dispatching::filters::{Filter, Or};
///
/// // Note: bool can be treated as `Filter` that always return self.
/// assert!(Or::new(true, false).test(&()));
@ -124,13 +124,13 @@ where
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{or, Filter};
/// use telebofr::dispatching::filters::{or, Filter};
///
/// assert!(or(true, false).test(&()));
/// assert_eq!(or(false, false).test(&()), false);
/// ```
///
/// [`Or::new`]: crate::dispatcher::filter::Or::new
/// [`Or::new`]: crate::dispatching::filter::Or::new
pub fn or<A, B>(a: A, b: B) -> Or<A, B> {
Or::new(a, b)
}
@ -141,7 +141,7 @@ pub fn or<A, B>(a: A, b: B) -> Or<A, B> {
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{Filter, Not};
/// use telebofr::dispatching::filters::{Filter, Not};
///
/// // Note: bool can be treated as `Filter` that always return self.
/// assert!(Not::new(false).test(&()));
@ -169,13 +169,13 @@ where
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{not, Filter};
/// use telebofr::dispatching::filters::{not, Filter};
///
/// assert!(not(false).test(&()));
/// assert_eq!(not(true).test(&()), false);
/// ```
///
/// [`Not::new`]: crate::dispatcher::filter::Not::new
/// [`Not::new`]: crate::dispatching::filter::Not::new
pub fn not<A>(a: A) -> Not<A> {
Not::new(a)
}
@ -187,7 +187,7 @@ pub fn not<A>(a: A) -> Not<A> {
///
/// ## Examples
/// ```
/// use telebofr::{all, dispatcher::filter::Filter};
/// use telebofr::{all, dispatching::filters::Filter};
///
/// assert!(all![true].test(&()));
/// assert!(all![true, true].test(&()));
@ -199,12 +199,12 @@ pub fn not<A>(a: A) -> Not<A> {
/// assert_eq!(all![false, false].test(&()), false);
/// ```
///
/// [filter]: crate::dispatcher::filter::Filter
/// [filter]: crate::dispatching::filter::Filter
#[macro_export]
macro_rules! all {
($one:expr) => { $one };
($head:expr, $($tail:tt)+) => {
$crate::dispatcher::filter::And::new(
$crate::dispatching::filters::And::new(
$head,
$crate::all!($($tail)+)
)
@ -218,7 +218,7 @@ macro_rules! all {
///
/// ## Examples
/// ```
/// use telebofr::{any, dispatcher::filter::Filter};
/// use telebofr::{any, dispatching::filters::Filter};
///
/// assert!(any![true].test(&()));
/// assert!(any![true, true].test(&()));
@ -230,12 +230,12 @@ macro_rules! all {
/// assert_eq!(any![false, false, false].test(&()), false);
/// ```
///
/// [filter]: crate::dispatcher::filter::Filter
/// [filter]: crate::dispatching::filter::Filter
#[macro_export]
macro_rules! any {
($one:expr) => { $one };
($head:expr, $($tail:tt)+) => {
$crate::dispatcher::filter::Or::new(
$crate::dispatching::filters::Or::new(
$head,
$crate::all!($($tail)+)
)
@ -246,7 +246,7 @@ macro_rules! any {
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{f, And, Filter, Or, F};
/// use telebofr::dispatching::filters::{f, And, Filter, Or, F};
///
/// let flt1 = |i: &i32| -> bool { *i > 17 };
/// let flt2 = |i: &i32| -> bool { *i < 42 };
@ -277,7 +277,7 @@ pub struct F<A>(A);
/// Constructor fn for [F]
///
/// [F]: crate::dispatcher::filter::F;
/// [F]: crate::dispatching::filter::F;
pub fn f<A>(a: A) -> F<A> {
F(a)
}
@ -314,7 +314,7 @@ pub trait FilterExt<T> {
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{Filter, FilterExt};
/// use telebofr::dispatching::filters::{Filter, FilterExt};
///
/// let flt = |i: &i32| -> bool { *i > 0 };
/// let flt = flt.not();
@ -322,7 +322,7 @@ pub trait FilterExt<T> {
/// assert_eq!(flt.test(&1), false);
/// ```
///
/// [`Not::new`]: crate::dispatcher::filter::Not::new
/// [`Not::new`]: crate::dispatching::filter::Not::new
fn not(self) -> Not<Self>
where
Self: Sized,
@ -334,7 +334,7 @@ pub trait FilterExt<T> {
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{Filter, FilterExt};
/// use telebofr::dispatching::filters::{Filter, FilterExt};
///
/// let flt = |i: &i32| -> bool { *i > 0 };
/// let flt = flt.and(|i: &i32| *i < 42);
@ -344,7 +344,7 @@ pub trait FilterExt<T> {
/// assert_eq!(flt.test(&43), false);
/// ```
///
/// [`Not::new`]: crate::dispatcher::filter::And::new
/// [`Not::new`]: crate::dispatching::filter::And::new
fn and<B>(self, other: B) -> And<Self, B>
where
Self: Sized,
@ -356,7 +356,7 @@ pub trait FilterExt<T> {
///
/// ## Examples
/// ```
/// use telebofr::dispatcher::filter::{Filter, FilterExt};
/// use telebofr::dispatching::filters::{Filter, FilterExt};
///
/// let flt = |i: &i32| -> bool { *i < 0 };
/// let flt = flt.or(|i: &i32| *i > 42);
@ -366,7 +366,7 @@ pub trait FilterExt<T> {
/// assert_eq!(flt.test(&17), false);
/// ```
///
/// [`Not::new`]: crate::dispatcher::filter::Or::new
/// [`Not::new`]: crate::dispatching::filter::Or::new
fn or<B>(self, other: B) -> Or<Self, B>
where
Self: Sized,

View file

@ -1,12 +1,12 @@
//! Update dispatching.
use async_trait::async_trait;
pub use filter::Filter;
pub use filters::Filter;
pub use handler::Handler;
pub mod filter;
pub mod dispatchers;
pub mod filters;
pub mod handler;
pub mod simple;
pub mod updater;
#[async_trait(? Send)]

View file

@ -12,6 +12,6 @@ mod errors;
mod network;
mod bot;
pub mod dispatcher;
pub mod dispatching;
pub mod requests;
pub mod types;

View file

@ -1,10 +1,11 @@
use async_trait::async_trait;
use crate::{
bot::Bot,
network,
requests::{Request, ResponseResult},
types::True,
};
use async_trait::async_trait;
/// Use this method to send answers to callback queries sent from inline
/// keyboards. The answer will be displayed to the user as a notification at the

View file

@ -1,10 +1,11 @@
use async_trait::async_trait;
use crate::{
bot::Bot,
network,
requests::{Request, ResponseResult},
types::{ChatId, True},
};
use async_trait::async_trait;
/// Use this method to delete a group sticker set from a supergroup. The bot
/// must be an administrator in the chat for this to work and must have the

View file

@ -1,10 +1,11 @@
use async_trait::async_trait;
use crate::{
bot::Bot,
network,
requests::{Request, ResponseResult},
types::{ChatId, ChatMember},
};
use async_trait::async_trait;
/// Use this method to get a list of administrators in a chat. On success,
/// returns an Array of ChatMember objects that contains information about all

View file

@ -1,10 +1,11 @@
use async_trait::async_trait;
use crate::{
bot::Bot,
network,
requests::{Request, ResponseResult},
types::{ChatId, ChatMember},
};
use async_trait::async_trait;
/// Use this method to get information about a member of a chat. Returns a
/// ChatMember object on success.

View file

@ -8,7 +8,7 @@ use crate::{
};
#[derive(Debug, Clone)]
/// A simple method for testing your bot's auth token. Requires no parameters.
/// A filter method for testing your bot's auth token. Requires no parameters.
/// Returns basic information about the bot in form of a [`User`] object.
pub struct GetMe<'a> {
bot: &'a Bot,

View file

@ -1,8 +1,5 @@
//! API requests.
use async_trait::async_trait;
use serde::de::DeserializeOwned;
pub use answer_callback_query::*;
pub use answer_pre_checkout_query::*;
pub use answer_shipping_query::*;
@ -94,6 +91,9 @@ mod stop_message_live_location;
mod unban_chat_member;
mod unpin_chat_message;
use async_trait::async_trait;
use serde::de::DeserializeOwned;
/// A type that is returned from `Request::send_boxed`.
pub type ResponseResult<T> = Result<T, crate::RequestError>;

View file

@ -1,10 +1,11 @@
use async_trait::async_trait;
use crate::{
bot::Bot,
network,
requests::{Request, ResponseResult},
types::{ChatId, True},
};
use async_trait::async_trait;
/// Use this method to set a new group sticker set for a supergroup. The bot
/// must be an administrator in the chat for this to work and must have the

View file

@ -1,4 +1,4 @@
/// This object represents one button of the reply keyboard. For simple text
/// This object represents one button of the reply keyboard. For filter text
/// buttons String can be used instead of this object to specify text of the
/// button. Optional fields are mutually exclusive.
#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)]

View file

@ -13,12 +13,36 @@ pub use chat_photo::*;
pub use chosen_inline_result::*;
pub use contact::*;
pub use document::*;
pub use encrypted_credentials::*;
pub use encrypted_passport_element::*;
pub use file::*;
pub use force_reply::*;
pub use game::*;
pub use game_high_score::*;
pub use inline_keyboard_button::*;
pub use inline_keyboard_markup::*;
pub use inline_query::*;
pub use inline_query_result::*;
pub use inline_query_result_article::*;
pub use inline_query_result_audio::*;
pub use inline_query_result_cached_audio::*;
pub use inline_query_result_cached_document::*;
pub use inline_query_result_cached_gif::*;
pub use inline_query_result_cached_mpeg4_gif::*;
pub use inline_query_result_cached_photo::*;
pub use inline_query_result_cached_sticker::*;
pub use inline_query_result_cached_video::*;
pub use inline_query_result_cached_voice::*;
pub use inline_query_result_contact::*;
pub use inline_query_result_document::*;
pub use inline_query_result_game::*;
pub use inline_query_result_gif::*;
pub use inline_query_result_location::*;
pub use inline_query_result_mpeg4_gif::*;
pub use inline_query_result_photo::*;
pub use inline_query_result_venue::*;
pub use inline_query_result_video::*;
pub use inline_query_result_voice::*;
pub use input_file::*;
pub use input_media::*;
pub use input_message_content::*;
@ -32,6 +56,8 @@ pub use message::*;
pub use message_entity::*;
pub use order_info::*;
pub use parse_mode::*;
pub use passport_data::*;
pub use passport_file::*;
pub use photo_size::*;
pub use poll::*;
pub use pre_checkout_query::*;
@ -57,34 +83,6 @@ pub use video_note::*;
pub use voice::*;
pub use webhook_info::*;
pub use inline_query::*;
pub use inline_query_result::*;
pub use inline_query_result_article::*;
pub use inline_query_result_audio::*;
pub use inline_query_result_cached_audio::*;
pub use inline_query_result_cached_document::*;
pub use inline_query_result_cached_gif::*;
pub use inline_query_result_cached_mpeg4_gif::*;
pub use inline_query_result_cached_photo::*;
pub use inline_query_result_cached_sticker::*;
pub use inline_query_result_cached_video::*;
pub use inline_query_result_cached_voice::*;
pub use inline_query_result_contact::*;
pub use inline_query_result_document::*;
pub use inline_query_result_game::*;
pub use inline_query_result_gif::*;
pub use inline_query_result_location::*;
pub use inline_query_result_mpeg4_gif::*;
pub use inline_query_result_photo::*;
pub use inline_query_result_venue::*;
pub use inline_query_result_video::*;
pub use inline_query_result_voice::*;
pub use encrypted_credentials::*;
pub use encrypted_passport_element::*;
pub use passport_data::*;
pub use passport_file::*;
mod animation;
mod audio;
mod callback_game;