Move DialogueHandlerExt::add_dialogue to HandlerExt

This commit is contained in:
Hirrolot 2022-02-04 19:53:15 +06:00
parent 0000839144
commit 8eb8e3c720
5 changed files with 34 additions and 41 deletions

View file

@ -1,34 +0,0 @@
use crate::dispatching2::dialogue::{get_chat_id::GetChatId, Dialogue, Storage};
use dptree::{di::DependencyMap, Handler};
use std::sync::Arc;
pub trait DialogueHandlerExt {
#[must_use]
fn add_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + Send + Sync + 'static,
<S as Storage<D>>::Error: Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static;
}
impl<'a, Output> DialogueHandlerExt for Handler<'a, DependencyMap, Output>
where
Output: Send + Sync + 'static,
{
fn add_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + Send + Sync + 'static,
<S as Storage<D>>::Error: Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static,
{
self.chain(dptree::filter_map(|storage: Arc<S>, upd: Upd| async move {
let chat_id = upd.chat_id()?;
Some(Dialogue::new(storage, chat_id))
}))
.chain(dptree::filter_map(|dialogue: Dialogue<D, S>| async move {
dialogue.get_or_default().await.ok()
}))
}
}

View file

@ -1,7 +1,7 @@
use crate::types::CallbackQuery;
use teloxide_core::types::Message;
/// Something that maybe has a chat ID.
/// Something that may has a chat ID.
pub trait GetChatId {
#[must_use]
fn chat_id(&self) -> Option<i64>;

View file

@ -97,13 +97,11 @@ pub use storage::{RedisStorage, RedisStorageError};
#[cfg(feature = "sqlite-storage")]
pub use storage::{SqliteStorage, SqliteStorageError};
pub use get_chat_id::GetChatId;
pub use storage::{serializer, InMemStorage, InMemStorageError, Serializer, Storage, TraceStorage};
pub use dialogue_handler_ext::DialogueHandlerExt;
use std::{marker::PhantomData, sync::Arc};
mod dialogue_handler_ext;
mod get_chat_id;
mod storage;

View file

@ -1,5 +1,10 @@
use std::sync::Arc;
use crate::{
dispatching2::HandlerFactory,
dispatching2::{
dialogue::{Dialogue, GetChatId, Storage},
HandlerFactory,
},
types::{Me, Message},
utils::command::BotCommand,
};
@ -18,6 +23,14 @@ pub trait HandlerExt<Output> {
where
C: BotCommand + Send + Sync + 'static;
#[must_use]
fn add_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + Send + Sync + 'static,
<S as Storage<D>>::Error: Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static;
#[must_use]
fn dispatch_by<F>(self) -> Self
where
@ -38,6 +51,22 @@ where
}))
}
fn add_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + Send + Sync + 'static,
<S as Storage<D>>::Error: Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static,
{
self.chain(dptree::filter_map(|storage: Arc<S>, upd: Upd| async move {
let chat_id = upd.chat_id()?;
Some(Dialogue::new(storage, chat_id))
}))
.chain(dptree::filter_map(|dialogue: Dialogue<D, S>| async move {
dialogue.get_or_default().await.ok()
}))
}
fn dispatch_by<F>(self) -> Self
where
F: HandlerFactory<Out = Output>,

View file

@ -6,8 +6,8 @@ pub use crate::{
};
pub use crate::dispatching2::{
dialogue::{Dialogue, DialogueHandlerExt as _},
Dispatcher, DispatcherBuilder, HandlerExt as _, MessageFilterExt as _, UpdateFilterExt as _,
dialogue::Dialogue, Dispatcher, DispatcherBuilder, HandlerExt as _, MessageFilterExt as _,
UpdateFilterExt as _,
};
#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "macros")))]