From 02c71f72cba05a75e26b74d30de9e7f6f3bd562a Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 31 Jul 2020 00:16:21 +0600 Subject: [PATCH] Use the generic HandlerE in REPLs --- README.md | 2 +- examples/dices_bot/src/main.rs | 2 +- src/dispatching/commands_repl.rs | 17 ++++++++++++----- src/dispatching/repl.rs | 11 +++++++---- src/lib.rs | 2 +- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 790828be..bc32b491 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ async fn main() { teloxide::repl(bot, |message| async move { message.answer_dice().send().await?; - Ok(()) + ResponseResult::<()>::Ok(()) }) .await; } diff --git a/examples/dices_bot/src/main.rs b/examples/dices_bot/src/main.rs index e73b4197..ea7f4424 100644 --- a/examples/dices_bot/src/main.rs +++ b/examples/dices_bot/src/main.rs @@ -15,7 +15,7 @@ async fn run() { teloxide::repl(bot, |message| async move { message.answer_dice().send().await?; - Ok(()) + ResponseResult::<()>::Ok(()) }) .await; } diff --git a/src/dispatching/commands_repl.rs b/src/dispatching/commands_repl.rs index dd6964b7..ba801445 100644 --- a/src/dispatching/commands_repl.rs +++ b/src/dispatching/commands_repl.rs @@ -4,7 +4,6 @@ use crate::{ DispatcherHandlerRxExt, UpdateWithCx, }, error_handlers::{LoggingErrorHandler, OnError}, - requests::ResponseResult, types::Message, utils::command::BotCommand, Bot, @@ -14,6 +13,8 @@ use std::{fmt::Debug, future::Future, sync::Arc}; /// A [REPL] for commands. /// +/// All errors from an update listener will be logged. +/// /// # Caution /// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, /// 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 /// [`Dispatcher`]: crate::dispatching::Dispatcher -pub fn commands_repl( +pub fn commands_repl( bot: Bot, bot_name: &'static str, handler: H, @@ -29,7 +30,9 @@ pub fn commands_repl( where Cmd: BotCommand + Send + 'static, H: Fn(UpdateWithCx, Cmd) -> Fut + Send + Sync + 'static, - Fut: Future> + Send + 'static, + Fut: Future> + Send + 'static, + Result<(), HandlerE>: OnError, + HandlerE: Debug + Send, { let cloned_bot = bot.clone(); @@ -43,6 +46,8 @@ where /// Like [`commands_repl`], but with a custom [`UpdateListener`]. /// +/// All errors from an update listener will be logged. +/// /// # Caution /// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, /// because Telegram disallow multiple requests at the same time from the same @@ -51,7 +56,7 @@ where /// [`Dispatcher`]: crate::dispatching::Dispatcher /// [`commands_repl`]: crate::dispatching::commands_repl() /// [`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_name: &'static str, handler: H, @@ -60,9 +65,11 @@ pub fn commands_repl_with_listener<'a, Cmd, H, Fut, UL, ListenerE>( where Cmd: BotCommand + Send + 'static, H: Fn(UpdateWithCx, Cmd) -> Fut + Send + Sync + 'static, - Fut: Future> + Send + 'static, + Fut: Future> + Send + 'static, UL: UpdateListener + Send + 'a, ListenerE: Debug + Send + 'a, + Result<(), HandlerE>: OnError, + HandlerE: Debug + Send, { let handler = Arc::new(handler); diff --git a/src/dispatching/repl.rs b/src/dispatching/repl.rs index 5c3bf7e8..f9fa3019 100644 --- a/src/dispatching/repl.rs +++ b/src/dispatching/repl.rs @@ -1,15 +1,16 @@ use crate::{ dispatching::{Dispatcher, DispatcherHandlerRx, UpdateWithCx}, error_handlers::OnError, - requests::ResponseResult, types::Message, Bot, }; use futures::StreamExt; -use std::{future::Future, sync::Arc}; +use std::{fmt::Debug, future::Future, sync::Arc}; /// A [REPL] for messages. /// +/// All errors from an update listener will be logged. +/// /// # Caution /// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, /// 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 /// [`Dispatcher`]: crate::dispatching::Dispatcher -pub async fn repl(bot: Bot, handler: H) +pub async fn repl(bot: Bot, handler: H) where H: Fn(UpdateWithCx) -> Fut + Send + Sync + 'static, - Fut: Future> + Send + 'static, + Fut: Future> + Send + 'static, + Result<(), E>: OnError, + E: Debug + Send, { let handler = Arc::new(handler); diff --git a/src/lib.rs b/src/lib.rs index 6e379d13..4b0542c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ //! //! teloxide::repl(bot, |message| async move { //! message.answer_dice().send().await?; -//! Ok(()) +//! ResponseResult::<()>::Ok(()) //! }) //! .await; //! # }