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"] }
#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-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 {
use std::ops::ControlFlow;
use crate::dispatching::UpdateHandler;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum State {
A,

View file

@ -1,7 +1,7 @@
use crate::{
dispatching::{
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},
requests::{Request, Requester},
@ -178,7 +178,7 @@ struct Worker {
/// A handler that processes updates from Telegram.
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>;
@ -265,9 +265,9 @@ where
self.dependencies.insert(me);
self.dependencies.insert(self.bot.clone());
update_listener.hint_allowed_updates(
&mut self.handler.required_update_kinds_set().get_param().into_iter(),
);
let description = self.handler.description();
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 mut stop_token = Some(update_listener.stop_token());

View file

@ -3,7 +3,7 @@
use dptree::{di::DependencyMap, Handler};
use crate::{
dispatching::AllowedUpdates,
dispatching::DpHandlerDescription,
types::{AllowedUpdate, Message, Update, UpdateKind},
};
@ -24,19 +24,19 @@ macro_rules! define_ext {
(@sig $func:ident, $fn_doc:expr) => {
#[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) => {
fn $func() -> Handler<'static, DependencyMap, Out, AllowedUpdates> {
dptree::filter_map_with_requirements(AllowedUpdates::of(AllowedUpdate::$Allowed), move |input: $for_ty| {
fn $func() -> Handler<'static, DependencyMap, Out, DpHandlerDescription> {
dptree::filter_map_with_description(DpHandlerDescription::of(AllowedUpdate::$Allowed), move |input: $for_ty| {
$proj_fn(input)
})
}
};
(@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| {
$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::{
dispatching::{
dialogue::{Dialogue, GetChatId, Storage},
AllowedUpdates,
DpHandlerDescription,
},
types::{Me, Message},
utils::command::BotCommands,
@ -61,7 +61,7 @@ pub trait HandlerExt<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
Output: Send + Sync + 'static,
{

View file

@ -1,11 +1,11 @@
use dptree::{di::DependencyMap, Handler};
use crate::dispatching::AllowedUpdates;
use crate::dispatching::DpHandlerDescription;
/// Something that can construct a handler.
#[deprecated(note = "Use the teloxide::handler! API")]
pub trait HandlerFactory {
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"))]
pub mod repls;
mod allowed_updates;
pub mod dialogue;
mod dispatcher;
mod distribution;
mod filter_ext;
mod handler_description;
mod handler_ext;
mod handler_factory;
pub mod stop_token;
pub mod update_listeners;
pub use crate::utils::shutdown_token::{IdleShutdownError, ShutdownToken};
pub use allowed_updates::AllowedUpdates;
pub use dispatcher::{Dispatcher, DispatcherBuilder, UpdateHandler};
pub use distribution::DefaultKey;
pub use filter_ext::{MessageFilterExt, UpdateFilterExt};
pub use handler_description::DpHandlerDescription;
pub use handler_ext::HandlerExt;
#[allow(deprecated)]
pub use handler_factory::HandlerFactory;