Initial commit.

1. Added `dptree` dependency.
2. Added `dispatching2` module.
2. Added `Dispatcher` and `Handlers` types, which is similar to `dispatching::Dispatcher` type.
This commit is contained in:
p0lunin 2021-11-13 12:35:56 +02:00
parent 2b55ee8482
commit 887b24d27f
5 changed files with 108 additions and 0 deletions

View file

@ -75,6 +75,8 @@ teloxide-macros = { version = "0.4", optional = true }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
dptree = { git = "https://github.com/p0lunin/dptree", branch = "kiss3" }
tokio = { version = "1.8", features = ["fs"] }
tokio-util = "0.6"
tokio-stream = "0.1"

View file

@ -0,0 +1,44 @@
use crate::{
dispatching2::handlers::{Handlers, Replaced, UpdateHandler},
types,
types::Update,
};
use dptree::{di::DependencySupplier, Replace};
pub struct Dispatcher<C, Err>
where
C: Replace<Update, types::Message>,
{
handlers: Handlers<C, Err>,
}
impl<C, IR, Err> Dispatcher<C, Err>
where
C: DependencySupplier<Update>
+ Send
+ Sync
+ 'static
+ Replace<Update, types::Message, Out = IR>,
IR: Send + Sync + 'static + Replace<types::Message, Update, Out = C>,
Err: Send + Sync + 'static,
{
pub fn new() -> Self {
Dispatcher { handlers: Handlers::new() }
}
pub fn message_handler(
mut self,
handler: UpdateHandler<Replaced<C, types::Message>, Err>,
) -> Self {
self.handlers.message_handler(handler);
self
}
pub fn edited_message_handler(
mut self,
handler: UpdateHandler<Replaced<C, types::Message>, Err>,
) -> Self {
self.handlers.edited_message_handler(handler);
self
}
}

View file

@ -0,0 +1,57 @@
use crate::{
types,
types::{Update, UpdateKind},
};
use dptree::{di::DependencySupplier, Handler, Replace};
pub type Replaced<C, T2> = <C as Replace<Update, T2>>::Out;
pub struct Handlers<C, Err>
where
C: Replace<Update, types::Message>,
{
message_handler: UpdateHandler<C, Err, Replaced<C, types::Message>>,
edited_message_handler: UpdateHandler<C, Err, Replaced<C, types::Message>>,
}
macro_rules! new_handler {
($kind:ident) => {
dptree::parser(|upd: &Update| match &upd.kind {
UpdateKind::$kind(u) => Some(u.clone()),
_ => None,
})
};
}
impl<C, IR, Err> Handlers<C, Err>
where
C: DependencySupplier<Update>
+ Send
+ Sync
+ 'static
+ Replace<Update, types::Message, Out = IR>,
IR: Send + Sync + 'static + Replace<types::Message, Update, Out = C>,
Err: Send + Sync + 'static,
{
pub fn new() -> Self {
Handlers {
message_handler: new_handler!(Message),
edited_message_handler: new_handler!(EditedMessage),
}
}
pub fn message_handler(&mut self, handler: UpdateHandler<Replaced<C, types::Message>, Err>) {
self.message_handler = self.message_handler.clone().branch(handler);
}
pub fn edited_message_handler(
&mut self,
handler: UpdateHandler<Replaced<C, types::Message>, Err>,
) {
self.edited_message_handler = self.edited_message_handler.clone().branch(handler);
}
}
// TODO: it is allowed to return message as answer on telegram request in
// webhooks, so we can allow this too. See more there: https://core.telegram.org/bots/api#making-requests-when-getting-updates
pub type UpdateHandler<C, Err, IR = C> = Handler<'static, C, Result<(), Err>, IR>;

4
src/dispatching2/mod.rs Normal file
View file

@ -0,0 +1,4 @@
mod dispatcher;
mod handlers;
pub use dispatcher::Dispatcher;

View file

@ -68,6 +68,7 @@ pub use dispatching::repls::{
mod logging;
pub mod dispatching;
pub mod dispatching2;
pub mod error_handlers;
pub mod prelude;
pub mod utils;