From 18651a825376ec3d9b67f9a3ec7d377f238ba0f5 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Tue, 29 Mar 2022 02:10:19 +0600 Subject: [PATCH] Some REPLs twiddling --- src/dispatching/repls/commands_repl.rs | 23 +++--- src/dispatching/repls/dialogues_repl.rs | 96 ------------------------- src/dispatching/repls/repl.rs | 14 ++-- 3 files changed, 12 insertions(+), 121 deletions(-) delete mode 100644 src/dispatching/repls/dialogues_repl.rs diff --git a/src/dispatching/repls/commands_repl.rs b/src/dispatching/repls/commands_repl.rs index 7a196f8f..63fb077b 100644 --- a/src/dispatching/repls/commands_repl.rs +++ b/src/dispatching/repls/commands_repl.rs @@ -73,23 +73,16 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>( // commands. See . let ignore_update = |_upd| Box::pin(async {}); - let mut dispatcher = Dispatcher::builder( + Dispatcher::builder( bot, Update::filter_message().filter_command::().branch(dptree::endpoint(handler)), ) .default_handler(ignore_update) - .build(); - - #[cfg(feature = "ctrlc_handler")] - dispatcher.setup_ctrlc_handler(); - - // To make mutable var from immutable. - let mut dispatcher = dispatcher; - - dispatcher - .dispatch_with_listener( - listener, - LoggingErrorHandler::with_custom_text("An error from the update listener"), - ) - .await; + .build() + .setup_ctrlc_handler() + .dispatch_with_listener( + listener, + LoggingErrorHandler::with_custom_text("An error from the update listener"), + ) + .await; } diff --git a/src/dispatching/repls/dialogues_repl.rs b/src/dispatching/repls/dialogues_repl.rs deleted file mode 100644 index 755d4dd3..00000000 --- a/src/dispatching/repls/dialogues_repl.rs +++ /dev/null @@ -1,96 +0,0 @@ -use crate::{ - dispatching::{ - dialogue::{DialogueDispatcher, DialogueStage, DialogueWithCx, InMemStorageError}, - update_listeners, - update_listeners::UpdateListener, - Dispatcher, UpdateWithCx, - }, - error_handlers::LoggingErrorHandler, -}; -use std::{fmt::Debug, future::Future, sync::Arc}; -use teloxide_core::{requests::Requester, types::Message}; - -/// A [REPL] for dialogues. -/// -/// All errors from an update listener and handler will be logged. This function -/// uses [`InMemStorage`]. -/// -/// # Caution -/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, -/// because Telegram disallow multiple requests at the same time from the same -/// bot. -/// -/// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop -/// [`Dispatcher`]: crate::dispatching::Dispatcher -/// [`InMemStorage`]: crate::dispatching::dialogue::InMemStorage -#[cfg(feature = "ctrlc_handler")] -pub async fn dialogues_repl<'a, R, H, D, Fut>(requester: R, handler: H) -where - H: Fn(UpdateWithCx, D) -> Fut + Send + Sync + 'static, - D: Clone + Default + Send + 'static, - Fut: Future> + Send + 'static, - R: Requester + Send + Clone + 'static, - ::GetUpdatesFaultTolerant: Send, -{ - let cloned_requester = requester.clone(); - - dialogues_repl_with_listener( - requester, - handler, - update_listeners::polling_default(cloned_requester).await, - ) - .await; -} - -/// Like [`dialogues_repl`], but with a custom [`UpdateListener`]. -/// -/// All errors from an update listener and handler will be logged. This function -/// uses [`InMemStorage`]. -/// -/// # Caution -/// **DO NOT** use this function together with [`Dispatcher`] and other REPLs, -/// because Telegram disallow multiple requests at the same time from the same -/// bot. -/// -/// [`Dispatcher`]: crate::dispatching::Dispatcher -/// [`dialogues_repl`]: crate::dispatching::repls::dialogues_repl() -/// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener -/// [`InMemStorage`]: crate::dispatching::dialogue::InMemStorage -#[cfg(feature = "ctrlc_handler")] -pub async fn dialogues_repl_with_listener<'a, R, H, D, Fut, L, ListenerE>( - requester: R, - handler: H, - listener: L, -) where - H: Fn(UpdateWithCx, D) -> Fut + Send + Sync + 'static, - D: Clone + Default + Send + 'static, - Fut: Future> + Send + 'static, - L: UpdateListener + Send + 'a, - ListenerE: Debug + Send + 'a, - R: Requester + Send + Clone + 'static, -{ - let handler = Arc::new(handler); - - Dispatcher::new(requester) - .messages_handler(DialogueDispatcher::new( - move |DialogueWithCx { cx, dialogue }: DialogueWithCx< - R, - Message, - D, - InMemStorageError, - >| { - let handler = Arc::clone(&handler); - - async move { - let dialogue = dialogue.expect("std::convert::Infallible"); - handler(cx, dialogue).await - } - }, - )) - .setup_ctrlc_handler() - .dispatch_with_listener( - listener, - LoggingErrorHandler::with_custom_text("An error from the update listener"), - ) - .await; -} diff --git a/src/dispatching/repls/repl.rs b/src/dispatching/repls/repl.rs index ca5622a4..cbd4fed1 100644 --- a/src/dispatching/repls/repl.rs +++ b/src/dispatching/repls/repl.rs @@ -59,16 +59,10 @@ where // messages. See . let ignore_update = |_upd| Box::pin(async {}); - #[allow(unused_mut)] - let mut dispatcher = - Dispatcher::builder(bot, Update::filter_message().branch(dptree::endpoint(handler))) - .default_handler(ignore_update) - .build(); - - #[cfg(feature = "ctrlc_handler")] - dispatcher.setup_ctrlc_handler(); - - dispatcher + Dispatcher::builder(bot, Update::filter_message().branch(dptree::endpoint(handler))) + .default_handler(ignore_update) + .build() + .setup_ctrlc_handler() .dispatch_with_listener( listener, LoggingErrorHandler::with_custom_text("An error from the update listener"),