From 13fb15f24eb5171bbcb377a2791d49edc9b836fc Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 31 Jul 2020 18:33:43 +0600 Subject: [PATCH] Fix compilation --- src/dispatching/repls/dialogues_repl.rs | 43 ++++++++++++------------- src/lib.rs | 3 +- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/dispatching/repls/dialogues_repl.rs b/src/dispatching/repls/dialogues_repl.rs index 91ebc60b..b03d110d 100644 --- a/src/dispatching/repls/dialogues_repl.rs +++ b/src/dispatching/repls/dialogues_repl.rs @@ -5,11 +5,11 @@ use crate::{ update_listeners::UpdateListener, Dispatcher, UpdateWithCx, }, - error_handlers::{LoggingErrorHandler, OnError}, + error_handlers::LoggingErrorHandler, types::Message, Bot, }; -use std::{convert::Infallible, fmt::Debug, future::Future}; +use std::{convert::Infallible, fmt::Debug, future::Future, sync::Arc}; /// A [REPL] for dialogues. /// @@ -23,23 +23,15 @@ use std::{convert::Infallible, fmt::Debug, future::Future}; /// /// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop /// [`Dispatcher`]: crate::dispatching::Dispatcher -pub async fn dialogues_repl<'a, H, D, Fut, HandlerE>(bot: Bot, bot_name: &'static str, handler: H) +pub async fn dialogues_repl<'a, H, D, Fut>(bot: Bot, handler: H) where H: Fn(UpdateWithCx, D) -> Fut + Send + Sync + 'static, D: Default + Send + 'static, - Fut: Future, HandlerE>> + Send + Sync + 'static, - Result, HandlerE>: OnError, - HandlerE: Debug + Send, + Fut: Future> + Send + Sync + 'static, { let cloned_bot = bot.clone(); - dialogues_repl_with_listener( - bot, - bot_name, - handler, - update_listeners::polling_default(cloned_bot), - ) - .await; + dialogues_repl_with_listener(bot, handler, update_listeners::polling_default(cloned_bot)).await; } /// Like [`dialogue_repl`], but with a custom [`UpdateListener`]. @@ -55,25 +47,30 @@ where /// [`Dispatcher`]: crate::dispatching::Dispatcher /// [`dialogue_repl`]: crate::dispatching::repls::dialogue_repl() /// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener -pub async fn dialogues_repl_with_listener<'a, H, D, Fut, HandlerE, L, ListenerE>( +pub async fn dialogues_repl_with_listener<'a, H, D, Fut, L, ListenerE>( bot: Bot, - bot_name: &'static str, handler: H, listener: L, ) where H: Fn(UpdateWithCx, D) -> Fut + Send + Sync + 'static, D: Default + Send + 'static, - Fut: Future, HandlerE>> + Send + Sync + 'static, + Fut: Future> + Send + Sync + 'static, L: UpdateListener + Send + 'a, ListenerE: Debug + Send + 'a, - Result, HandlerE>: OnError, - HandlerE: Debug + Send, { - Dispatcher::new(todo!()) - .messages_handler(DialogueDispatcher::new(|x| async move { - // let dialogue = dialogue.expect("std::convert::Infallible"); - // handler(cx, dialogue).await.log_on_error().await - })) + let handler = Arc::new(handler); + + Dispatcher::new(bot) + .messages_handler(DialogueDispatcher::new( + move |DialogueWithCx { cx, dialogue }: DialogueWithCx| { + let handler = Arc::clone(&handler); + + async move { + let dialogue = dialogue.expect("std::convert::Infallible"); + handler(cx, dialogue).await + } + }, + )) .dispatch_with_listener( listener, LoggingErrorHandler::with_custom_text("An error from the update listener"), diff --git a/src/lib.rs b/src/lib.rs index c3ae1ef4..c3116be2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,8 @@ pub use bot::{Bot, BotBuilder}; pub use dispatching::repls::{ - commands_repl, commands_repl_with_listener, repl, repl_with_listener, + commands_repl, commands_repl_with_listener, dialogues_repl, dialogues_repl_with_listener, repl, + repl_with_listener, }; pub use errors::{ApiErrorKind, DownloadError, KnownApiErrorKind, RequestError};