From cde6827cee5eb482ed9c2b900a29c1f31c2f9359 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Sun, 2 Oct 2022 21:44:04 +0600 Subject: [PATCH] Use `RequestError` in REPLs --- CHANGELOG.md | 5 +++++ README.md | 10 ++-------- examples/admin.rs | 22 +++++----------------- examples/command.rs | 8 +------- examples/heroku_ping_pong.rs | 2 +- examples/ngrok_ping_pong.rs | 2 +- examples/throw_dice.rs | 2 +- src/dispatching/repls/commands_repl.rs | 11 +++++------ src/dispatching/repls/repl.rs | 17 +++++++---------- src/lib.rs | 2 +- src/prelude.rs | 17 ++++++++++------- 11 files changed, 39 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bfe7543..7e02e5c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `UpdateListener` now has an associated type `Err` instead of a generic - `AsUpdateStream` now has an associated type `StreamErr` instead of a generic - Rename `dispatching::stop_token::{AsyncStopToken, AsyncStopFlag}` => `stop::{StopToken, StopFlag}` +- Replace the generic error type `E` with `RequestError` for REPLs (`repl(_with_listener)`, `commands_repl(_with_listener)`) + +### Added + +- `requests::ResponseResult` to `prelude` ### Removed diff --git a/README.md b/README.md index 375ee95a..ed8251ac 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ async fn main() { teloxide::repl(bot, |message: Message, bot: Bot| async move { bot.send_dice(message.chat.id).await?; - respond(()) + Ok(()) }) .await; } @@ -122,8 +122,6 @@ Commands are strongly typed and defined declaratively, similar to how we define ```rust,no_run use teloxide::{prelude::*, utils::command::BotCommands}; -use std::error::Error; - #[tokio::main] async fn main() { pretty_env_logger::init(); @@ -145,11 +143,7 @@ enum Command { UsernameAndAge { username: String, age: u8 }, } -async fn answer( - bot: Bot, - message: Message, - command: Command, -) -> Result<(), Box> { +async fn answer(bot: Bot, message: Message, command: Command) -> ResponseResult<()> { match command { Command::Help => { bot.send_message(message.chat.id, Command::descriptions().to_string()).await? diff --git a/examples/admin.rs b/examples/admin.rs index db45e3fa..a9d800e6 100644 --- a/examples/admin.rs +++ b/examples/admin.rs @@ -1,4 +1,4 @@ -use std::{error::Error, str::FromStr}; +use std::str::FromStr; use chrono::Duration; use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommands}; @@ -63,11 +63,7 @@ async fn main() { teloxide::commands_repl(bot, action, Command::ty()).await; } -async fn action( - bot: Bot, - msg: Message, - command: Command, -) -> Result<(), Box> { +async fn action(bot: Bot, msg: Message, command: Command) -> ResponseResult<()> { match command { Command::Help => { bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?; @@ -81,7 +77,7 @@ async fn action( } // Kick a user with a replied message. -async fn kick_user(bot: Bot, msg: Message) -> Result<(), Box> { +async fn kick_user(bot: Bot, msg: Message) -> ResponseResult<()> { match msg.reply_to_message() { Some(replied) => { // bot.unban_chat_member can also kicks a user from a group chat. @@ -95,11 +91,7 @@ async fn kick_user(bot: Bot, msg: Message) -> Result<(), Box Result<(), Box> { +async fn ban_user(bot: Bot, msg: Message, time: Duration) -> ResponseResult<()> { match msg.reply_to_message() { Some(replied) => { bot.kick_chat_member( @@ -118,11 +110,7 @@ async fn ban_user( } // Mute a user with a replied message. -async fn mute_user( - bot: Bot, - msg: Message, - time: Duration, -) -> Result<(), Box> { +async fn mute_user(bot: Bot, msg: Message, time: Duration) -> ResponseResult<()> { match msg.reply_to_message() { Some(replied) => { bot.restrict_chat_member( diff --git a/examples/command.rs b/examples/command.rs index 11a2ce55..d0f671ec 100644 --- a/examples/command.rs +++ b/examples/command.rs @@ -1,7 +1,5 @@ use teloxide::{prelude::*, utils::command::BotCommands}; -use std::error::Error; - #[tokio::main] async fn main() { pretty_env_logger::init(); @@ -23,11 +21,7 @@ enum Command { UsernameAndAge { username: String, age: u8 }, } -async fn answer( - bot: Bot, - message: Message, - command: Command, -) -> Result<(), Box> { +async fn answer(bot: Bot, message: Message, command: Command) -> ResponseResult<()> { match command { Command::Help => { bot.send_message(message.chat.id, Command::descriptions().to_string()).await? diff --git a/examples/heroku_ping_pong.rs b/examples/heroku_ping_pong.rs index 5c8def6a..1c104eb8 100644 --- a/examples/heroku_ping_pong.rs +++ b/examples/heroku_ping_pong.rs @@ -49,7 +49,7 @@ async fn main() { bot, |msg: Message, bot: Bot| async move { bot.send_message(msg.chat.id, "pong").await?; - respond(()) + Ok(()) }, listener, ) diff --git a/examples/ngrok_ping_pong.rs b/examples/ngrok_ping_pong.rs index 6f9eefb0..21486810 100644 --- a/examples/ngrok_ping_pong.rs +++ b/examples/ngrok_ping_pong.rs @@ -20,7 +20,7 @@ async fn main() { bot, |msg: Message, bot: Bot| async move { bot.send_message(msg.chat.id, "pong").await?; - respond(()) + Ok(()) }, listener, ) diff --git a/examples/throw_dice.rs b/examples/throw_dice.rs index 39272378..0de45aec 100644 --- a/examples/throw_dice.rs +++ b/examples/throw_dice.rs @@ -11,7 +11,7 @@ async fn main() { teloxide::repl(bot, |message: Message, bot: Bot| async move { bot.send_dice(message.chat.id).await?; - respond(()) + Ok(()) }) .await; } diff --git a/src/dispatching/repls/commands_repl.rs b/src/dispatching/repls/commands_repl.rs index ceeeb917..eed3bf15 100644 --- a/src/dispatching/repls/commands_repl.rs +++ b/src/dispatching/repls/commands_repl.rs @@ -5,6 +5,7 @@ use crate::{ error_handlers::LoggingErrorHandler, types::Update, utils::command::BotCommands, + RequestError, }; use dptree::di::{DependencyMap, Injectable}; use std::{fmt::Debug, marker::PhantomData}; @@ -34,13 +35,12 @@ use teloxide_core::requests::Requester; /// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop /// [`Dispatcher`]: crate::dispatching::Dispatcher #[cfg(feature = "ctrlc_handler")] -pub async fn commands_repl<'a, R, Cmd, H, E, Args>(bot: R, handler: H, cmd: PhantomData) +pub async fn commands_repl<'a, R, Cmd, H, Args>(bot: R, handler: H, cmd: PhantomData) where Cmd: BotCommands + Send + Sync + 'static, - H: Injectable, Args> + Send + Sync + 'static, + H: Injectable, Args> + Send + Sync + 'static, R: Requester + Clone + Send + Sync + 'static, ::GetUpdates: Send, - E: Debug + Send + Sync + 'static, { let cloned_bot = bot.clone(); @@ -78,18 +78,17 @@ where /// [`commands_repl`]: crate::dispatching::repls::commands_repl() /// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener #[cfg(feature = "ctrlc_handler")] -pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, E, Args>( +pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, Args>( bot: R, handler: H, listener: L, _cmd: PhantomData, ) where Cmd: BotCommands + Send + Sync + 'static, - H: Injectable, Args> + Send + Sync + 'static, + H: Injectable, Args> + Send + Sync + 'static, L: UpdateListener + Send + 'a, L::Err: Debug + Send + 'a, R: Requester + Clone + Send + Sync + 'static, - E: Debug + Send + Sync + 'static, { use crate::dispatching::Dispatcher; diff --git a/src/dispatching/repls/repl.rs b/src/dispatching/repls/repl.rs index 529cc711..783dcb90 100644 --- a/src/dispatching/repls/repl.rs +++ b/src/dispatching/repls/repl.rs @@ -1,7 +1,8 @@ use crate::{ dispatching::{update_listeners, update_listeners::UpdateListener, UpdateFilterExt}, - error_handlers::{LoggingErrorHandler, OnError}, + error_handlers::LoggingErrorHandler, types::Update, + RequestError, }; use dptree::di::{DependencyMap, Injectable}; use std::fmt::Debug; @@ -27,11 +28,9 @@ use teloxide_core::requests::Requester; /// [REPL]: https://en.wikipedia.org/wiki/Read-eval-print_loop /// [`Dispatcher`]: crate::dispatching::Dispatcher #[cfg(feature = "ctrlc_handler")] -pub async fn repl(bot: R, handler: H) +pub async fn repl(bot: R, handler: H) where - H: Injectable, Args> + Send + Sync + 'static, - Result<(), E>: OnError, - E: Debug + Send + Sync + 'static, + H: Injectable, Args> + Send + Sync + 'static, R: Requester + Send + Sync + Clone + 'static, ::GetUpdates: Send, { @@ -60,13 +59,11 @@ where /// [`repl`]: crate::dispatching::repls::repl() /// [`UpdateListener`]: crate::dispatching::update_listeners::UpdateListener #[cfg(feature = "ctrlc_handler")] -pub async fn repl_with_listener<'a, R, H, E, L, Args>(bot: R, handler: H, listener: L) +pub async fn repl_with_listener<'a, R, H, L, Args>(bot: R, handler: H, listener: L) where - H: Injectable, Args> + Send + Sync + 'static, + H: Injectable, Args> + Send + Sync + 'static, L: UpdateListener + Send + 'a, L::Err: Debug, - Result<(), E>: OnError, - E: Debug + Send + Sync + 'static, R: Requester + Clone + Send + Sync + 'static, { use crate::dispatching::Dispatcher; @@ -89,7 +86,7 @@ where #[test] fn repl_is_send() { let bot = crate::Bot::new(""); - let repl = crate::repl(bot, || async { crate::respond(()) }); + let repl = crate::repl(bot, || async { Ok(()) }); assert_send(&repl); fn assert_send(_: &impl Send) {} diff --git a/src/lib.rs b/src/lib.rs index c7a2e1a8..b262d957 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ //! //! teloxide::repl(bot, |message: Message, bot: Bot| async move { //! bot.send_dice(message.chat.id).await?; -//! respond(()) +//! Ok(()) //! }) //! .await; //! # } diff --git a/src/prelude.rs b/src/prelude.rs index 46e4d0b7..6dbe94df 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,17 +1,20 @@ //! Commonly used items. -pub use crate::{ - error_handlers::{LoggingErrorHandler, OnError}, - respond, -}; +pub use crate::error_handlers::{LoggingErrorHandler, OnError}; + +#[allow(deprecated)] +pub use crate::respond; pub use crate::dispatching::{ dialogue::Dialogue, Dispatcher, HandlerExt as _, MessageFilterExt as _, UpdateFilterExt as _, }; -pub use teloxide_core::types::{ - CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll, PollAnswer, - PreCheckoutQuery, ShippingQuery, Update, +pub use teloxide_core::{ + requests::ResponseResult, + types::{ + CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll, + PollAnswer, PreCheckoutQuery, ShippingQuery, Update, + }, }; #[cfg(feature = "auto-send")]