Use the generic HandlerE in REPLs

This commit is contained in:
Temirkhan Myrzamadi 2020-07-31 00:16:21 +06:00
parent 2849c1cbde
commit 02c71f72cb
5 changed files with 22 additions and 12 deletions

View file

@ -103,7 +103,7 @@ async fn main() {
teloxide::repl(bot, |message| async move { teloxide::repl(bot, |message| async move {
message.answer_dice().send().await?; message.answer_dice().send().await?;
Ok(()) ResponseResult::<()>::Ok(())
}) })
.await; .await;
} }

View file

@ -15,7 +15,7 @@ async fn run() {
teloxide::repl(bot, |message| async move { teloxide::repl(bot, |message| async move {
message.answer_dice().send().await?; message.answer_dice().send().await?;
Ok(()) ResponseResult::<()>::Ok(())
}) })
.await; .await;
} }

View file

@ -4,7 +4,6 @@ use crate::{
DispatcherHandlerRxExt, UpdateWithCx, DispatcherHandlerRxExt, UpdateWithCx,
}, },
error_handlers::{LoggingErrorHandler, OnError}, error_handlers::{LoggingErrorHandler, OnError},
requests::ResponseResult,
types::Message, types::Message,
utils::command::BotCommand, utils::command::BotCommand,
Bot, Bot,
@ -14,6 +13,8 @@ use std::{fmt::Debug, future::Future, sync::Arc};
/// A [REPL] for commands. /// A [REPL] for commands.
/// ///
/// All errors from an update listener will be logged.
///
/// # Caution /// # Caution
/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, /// **DO NOT** use this function together with [`Dispatcher`] and other REPLs,
/// because Telegram disallow multiple requests at the same time from the same /// because Telegram disallow multiple requests at the same time from the same
@ -21,7 +22,7 @@ use std::{fmt::Debug, future::Future, sync::Arc};
/// ///
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop /// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
/// [`Dispatcher`]: crate::dispatching::Dispatcher /// [`Dispatcher`]: crate::dispatching::Dispatcher
pub fn commands_repl<Cmd, H, Fut>( pub fn commands_repl<Cmd, H, Fut, HandlerE>(
bot: Bot, bot: Bot,
bot_name: &'static str, bot_name: &'static str,
handler: H, handler: H,
@ -29,7 +30,9 @@ pub fn commands_repl<Cmd, H, Fut>(
where where
Cmd: BotCommand + Send + 'static, Cmd: BotCommand + Send + 'static,
H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static, H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ResponseResult<()>> + Send + 'static, Fut: Future<Output = Result<(), HandlerE>> + Send + 'static,
Result<(), HandlerE>: OnError<HandlerE>,
HandlerE: Debug + Send,
{ {
let cloned_bot = bot.clone(); let cloned_bot = bot.clone();
@ -43,6 +46,8 @@ where
/// Like [`commands_repl`], but with a custom [`UpdateListener`]. /// Like [`commands_repl`], but with a custom [`UpdateListener`].
/// ///
/// All errors from an update listener will be logged.
///
/// # Caution /// # Caution
/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, /// **DO NOT** use this function together with [`Dispatcher`] and other REPLs,
/// because Telegram disallow multiple requests at the same time from the same /// because Telegram disallow multiple requests at the same time from the same
@ -51,7 +56,7 @@ where
/// [`Dispatcher`]: crate::dispatching::Dispatcher /// [`Dispatcher`]: crate::dispatching::Dispatcher
/// [`commands_repl`]: crate::dispatching::commands_repl() /// [`commands_repl`]: crate::dispatching::commands_repl()
/// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener /// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener
pub fn commands_repl_with_listener<'a, Cmd, H, Fut, UL, ListenerE>( pub fn commands_repl_with_listener<'a, Cmd, H, Fut, UL, ListenerE, HandlerE>(
bot: Bot, bot: Bot,
bot_name: &'static str, bot_name: &'static str,
handler: H, handler: H,
@ -60,9 +65,11 @@ pub fn commands_repl_with_listener<'a, Cmd, H, Fut, UL, ListenerE>(
where where
Cmd: BotCommand + Send + 'static, Cmd: BotCommand + Send + 'static,
H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static, H: Fn(UpdateWithCx<Message>, Cmd) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ResponseResult<()>> + Send + 'static, Fut: Future<Output = Result<(), HandlerE>> + Send + 'static,
UL: UpdateListener<ListenerE> + Send + 'a, UL: UpdateListener<ListenerE> + Send + 'a,
ListenerE: Debug + Send + 'a, ListenerE: Debug + Send + 'a,
Result<(), HandlerE>: OnError<HandlerE>,
HandlerE: Debug + Send,
{ {
let handler = Arc::new(handler); let handler = Arc::new(handler);

View file

@ -1,15 +1,16 @@
use crate::{ use crate::{
dispatching::{Dispatcher, DispatcherHandlerRx, UpdateWithCx}, dispatching::{Dispatcher, DispatcherHandlerRx, UpdateWithCx},
error_handlers::OnError, error_handlers::OnError,
requests::ResponseResult,
types::Message, types::Message,
Bot, Bot,
}; };
use futures::StreamExt; use futures::StreamExt;
use std::{future::Future, sync::Arc}; use std::{fmt::Debug, future::Future, sync::Arc};
/// A [REPL] for messages. /// A [REPL] for messages.
/// ///
/// All errors from an update listener will be logged.
///
/// # Caution /// # Caution
/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, /// **DO NOT** use this function together with [`Dispatcher`] and other REPLs,
/// because Telegram disallow multiple requests at the same time from the same /// because Telegram disallow multiple requests at the same time from the same
@ -17,10 +18,12 @@ use std::{future::Future, sync::Arc};
/// ///
/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop /// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop
/// [`Dispatcher`]: crate::dispatching::Dispatcher /// [`Dispatcher`]: crate::dispatching::Dispatcher
pub async fn repl<H, Fut>(bot: Bot, handler: H) pub async fn repl<H, Fut, E>(bot: Bot, handler: H)
where where
H: Fn(UpdateWithCx<Message>) -> Fut + Send + Sync + 'static, H: Fn(UpdateWithCx<Message>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ResponseResult<()>> + Send + 'static, Fut: Future<Output = Result<(), E>> + Send + 'static,
Result<(), E>: OnError<E>,
E: Debug + Send,
{ {
let handler = Arc::new(handler); let handler = Arc::new(handler);

View file

@ -17,7 +17,7 @@
//! //!
//! teloxide::repl(bot, |message| async move { //! teloxide::repl(bot, |message| async move {
//! message.answer_dice().send().await?; //! message.answer_dice().send().await?;
//! Ok(()) //! ResponseResult::<()>::Ok(())
//! }) //! })
//! .await; //! .await;
//! # } //! # }