Update dptree

Use the new naming of things for handler descriptions :)
This commit is contained in:
Maybe Waffle 2022-04-15 12:46:53 +04:00
parent 4db52436f3
commit dbe45fb49f
9 changed files with 78 additions and 77 deletions

View file

@ -63,7 +63,7 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
#dptree = { version = "0.1.0" } #dptree = { version = "0.1.0" }
dptree = { git = "https://github.com/WaffleLapkin/dptree", rev = "192f3fe" } dptree = { git = "https://github.com/WaffleLapkin/dptree", rev = "ec7d4ec" }
tokio = { version = "1.8", features = ["fs"] } tokio = { version = "1.8", features = ["fs"] }
tokio-util = "0.6" tokio-util = "0.6"

View file

@ -1,58 +0,0 @@
use std::collections::HashSet;
use dptree::{MaybeSpecial, UpdateSet};
use teloxide_core::types::AllowedUpdate;
pub struct AllowedUpdates {
inner: MaybeSpecial<HashSet<AllowedUpdate>>,
}
impl AllowedUpdates {
pub(crate) fn of(allowed: AllowedUpdate) -> Self {
let mut set = HashSet::with_capacity(1);
set.insert(allowed);
Self { inner: MaybeSpecial::Known(set) }
}
pub(crate) fn get_param(&self) -> Vec<AllowedUpdate> {
use AllowedUpdate::*;
match &self.inner {
MaybeSpecial::Known(set) => set.iter().cloned().collect(),
MaybeSpecial::Invisible => panic!("No updates were allowed"),
MaybeSpecial::Unknown => vec![
Message,
EditedMessage,
ChannelPost,
EditedChannelPost,
InlineQuery,
ChosenInlineResult,
CallbackQuery,
ShippingQuery,
PreCheckoutQuery,
Poll,
PollAnswer,
MyChatMember,
ChatMember,
],
}
}
}
impl UpdateSet for AllowedUpdates {
fn unknown() -> Self {
Self { inner: UpdateSet::unknown() }
}
fn invisible() -> Self {
Self { inner: UpdateSet::invisible() }
}
fn union(&self, other: &Self) -> Self {
Self { inner: self.inner.union(&other.inner) }
}
fn intersection(&self, other: &Self) -> Self {
Self { inner: self.inner.intersection(&other.inner) }
}
}

View file

@ -244,8 +244,6 @@ macro_rules! handler {
mod tests { mod tests {
use std::ops::ControlFlow; use std::ops::ControlFlow;
use crate::dispatching::UpdateHandler;
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum State { enum State {
A, A,

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
dispatching::{ dispatching::{
distribution::default_distribution_function, stop_token::StopToken, update_listeners, distribution::default_distribution_function, stop_token::StopToken, update_listeners,
update_listeners::UpdateListener, AllowedUpdates, DefaultKey, ShutdownToken, update_listeners::UpdateListener, DefaultKey, DpHandlerDescription, ShutdownToken,
}, },
error_handlers::{ErrorHandler, LoggingErrorHandler}, error_handlers::{ErrorHandler, LoggingErrorHandler},
requests::{Request, Requester}, requests::{Request, Requester},
@ -178,7 +178,7 @@ struct Worker {
/// A handler that processes updates from Telegram. /// A handler that processes updates from Telegram.
pub type UpdateHandler<Err> = pub type UpdateHandler<Err> =
dptree::Handler<'static, DependencyMap, Result<(), Err>, AllowedUpdates>; dptree::Handler<'static, DependencyMap, Result<(), Err>, DpHandlerDescription>;
type DefaultHandler = Arc<dyn Fn(Arc<Update>) -> BoxFuture<'static, ()> + Send + Sync>; type DefaultHandler = Arc<dyn Fn(Arc<Update>) -> BoxFuture<'static, ()> + Send + Sync>;
@ -265,9 +265,9 @@ where
self.dependencies.insert(me); self.dependencies.insert(me);
self.dependencies.insert(self.bot.clone()); self.dependencies.insert(self.bot.clone());
update_listener.hint_allowed_updates( let description = self.handler.description();
&mut self.handler.required_update_kinds_set().get_param().into_iter(), let allowed_updates = description.allowed_updates();
); update_listener.hint_allowed_updates(&mut allowed_updates.into_iter());
let shutdown_check_timeout = shutdown_check_timeout_for(&update_listener); let shutdown_check_timeout = shutdown_check_timeout_for(&update_listener);
let mut stop_token = Some(update_listener.stop_token()); let mut stop_token = Some(update_listener.stop_token());

View file

@ -3,7 +3,7 @@
use dptree::{di::DependencyMap, Handler}; use dptree::{di::DependencyMap, Handler};
use crate::{ use crate::{
dispatching::AllowedUpdates, dispatching::DpHandlerDescription,
types::{AllowedUpdate, Message, Update, UpdateKind}, types::{AllowedUpdate, Message, Update, UpdateKind},
}; };
@ -24,19 +24,19 @@ macro_rules! define_ext {
(@sig $func:ident, $fn_doc:expr) => { (@sig $func:ident, $fn_doc:expr) => {
#[doc = $fn_doc] #[doc = $fn_doc]
fn $func() -> Handler<'static, DependencyMap, Out, AllowedUpdates>; fn $func() -> Handler<'static, DependencyMap, Out, DpHandlerDescription>;
}; };
(@impl $for_ty:ty, $func:ident, $proj_fn:expr, $Allowed:ident) => { (@impl $for_ty:ty, $func:ident, $proj_fn:expr, $Allowed:ident) => {
fn $func() -> Handler<'static, DependencyMap, Out, AllowedUpdates> { fn $func() -> Handler<'static, DependencyMap, Out, DpHandlerDescription> {
dptree::filter_map_with_requirements(AllowedUpdates::of(AllowedUpdate::$Allowed), move |input: $for_ty| { dptree::filter_map_with_description(DpHandlerDescription::of(AllowedUpdate::$Allowed), move |input: $for_ty| {
$proj_fn(input) $proj_fn(input)
}) })
} }
}; };
(@impl $for_ty:ty, $func:ident, $proj_fn:expr) => { (@impl $for_ty:ty, $func:ident, $proj_fn:expr) => {
fn $func() -> Handler<'static, DependencyMap, Out, AllowedUpdates> { fn $func() -> Handler<'static, DependencyMap, Out, DpHandlerDescription> {
dptree::filter_map(move |input: $for_ty| { dptree::filter_map(move |input: $for_ty| {
$proj_fn(input) $proj_fn(input)
}) })

View file

@ -0,0 +1,61 @@
use std::collections::HashSet;
use dptree::{EventKindDescription, HandlerDescription};
use teloxide_core::types::AllowedUpdate;
/// Handler description that is used by [`Dispatcher`].
///
/// [`Dispatcher`]: crate::dispatching::Dispatcher
pub struct DpHandlerDescription {
allowed: EventKindDescription<AllowedUpdate>,
}
impl DpHandlerDescription {
pub(crate) fn of(allowed: AllowedUpdate) -> Self {
let mut set = HashSet::with_capacity(1);
set.insert(allowed);
Self { allowed: EventKindDescription::InterestingEventKinds(set) }
}
pub(crate) fn allowed_updates(&self) -> Vec<AllowedUpdate> {
use AllowedUpdate::*;
match &self.allowed {
EventKindDescription::InterestingEventKinds(set) => set.iter().copied().collect(),
EventKindDescription::Entry => panic!("No updates were allowed"),
EventKindDescription::UserDefined => vec![
Message,
EditedMessage,
ChannelPost,
EditedChannelPost,
InlineQuery,
ChosenInlineResult,
CallbackQuery,
ShippingQuery,
PreCheckoutQuery,
Poll,
PollAnswer,
MyChatMember,
ChatMember,
],
}
}
}
impl HandlerDescription for DpHandlerDescription {
fn entry() -> Self {
Self { allowed: HandlerDescription::entry() }
}
fn user_defined() -> Self {
Self { allowed: HandlerDescription::user_defined() }
}
fn merge_chain(&self, other: &Self) -> Self {
Self { allowed: self.allowed.merge_chain(&other.allowed) }
}
fn merge_branch(&self, other: &Self) -> Self {
Self { allowed: self.allowed.merge_branch(&other.allowed) }
}
}

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use crate::{ use crate::{
dispatching::{ dispatching::{
dialogue::{Dialogue, GetChatId, Storage}, dialogue::{Dialogue, GetChatId, Storage},
AllowedUpdates, DpHandlerDescription,
}, },
types::{Me, Message}, types::{Me, Message},
utils::command::BotCommands, utils::command::BotCommands,
@ -61,7 +61,7 @@ pub trait HandlerExt<Output> {
F: HandlerFactory<Out = Output>; F: HandlerFactory<Out = Output>;
} }
impl<Output> HandlerExt<Output> for Handler<'static, DependencyMap, Output, AllowedUpdates> impl<Output> HandlerExt<Output> for Handler<'static, DependencyMap, Output, DpHandlerDescription>
where where
Output: Send + Sync + 'static, Output: Send + Sync + 'static,
{ {

View file

@ -1,11 +1,11 @@
use dptree::{di::DependencyMap, Handler}; use dptree::{di::DependencyMap, Handler};
use crate::dispatching::AllowedUpdates; use crate::dispatching::DpHandlerDescription;
/// Something that can construct a handler. /// Something that can construct a handler.
#[deprecated(note = "Use the teloxide::handler! API")] #[deprecated(note = "Use the teloxide::handler! API")]
pub trait HandlerFactory { pub trait HandlerFactory {
type Out; type Out;
fn handler() -> Handler<'static, DependencyMap, Self::Out, AllowedUpdates>; fn handler() -> Handler<'static, DependencyMap, Self::Out, DpHandlerDescription>;
} }

View file

@ -98,21 +98,21 @@
#[cfg(all(feature = "ctrlc_handler"))] #[cfg(all(feature = "ctrlc_handler"))]
pub mod repls; pub mod repls;
mod allowed_updates;
pub mod dialogue; pub mod dialogue;
mod dispatcher; mod dispatcher;
mod distribution; mod distribution;
mod filter_ext; mod filter_ext;
mod handler_description;
mod handler_ext; mod handler_ext;
mod handler_factory; mod handler_factory;
pub mod stop_token; pub mod stop_token;
pub mod update_listeners; pub mod update_listeners;
pub use crate::utils::shutdown_token::{IdleShutdownError, ShutdownToken}; pub use crate::utils::shutdown_token::{IdleShutdownError, ShutdownToken};
pub use allowed_updates::AllowedUpdates;
pub use dispatcher::{Dispatcher, DispatcherBuilder, UpdateHandler}; pub use dispatcher::{Dispatcher, DispatcherBuilder, UpdateHandler};
pub use distribution::DefaultKey; pub use distribution::DefaultKey;
pub use filter_ext::{MessageFilterExt, UpdateFilterExt}; pub use filter_ext::{MessageFilterExt, UpdateFilterExt};
pub use handler_description::DpHandlerDescription;
pub use handler_ext::HandlerExt; pub use handler_ext::HandlerExt;
#[allow(deprecated)] #[allow(deprecated)]
pub use handler_factory::HandlerFactory; pub use handler_factory::HandlerFactory;