From 86253edc920ed22edc1bf8ac27c2a76768334389 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 19 Mar 2021 17:58:22 +0600 Subject: [PATCH 1/2] Use Box in the examples --- CHANGELOG.md | 1 + README.md | 13 +++++++++---- examples/admin_bot/src/main.rs | 10 +++++----- examples/simple_commands_bot/src/main.rs | 7 ++++++- examples/sqlite_remember_bot/src/main.rs | 2 +- src/lib.rs | 3 ++- src/prelude.rs | 2 +- 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 060024a8..37b67e56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `#[non_exhaustive]` annotation is removed from the enum, type of `TargetMessage::Inline::inline_message_id` changed `i32` => `String`. `TargetMessage` now implements `From`, `get_game_high_scores` and `set_game_score` use `Into` to accept `String`s. ([issue 253], [pr 257]) + - Remove `ResponseResult` from `prelude`. [issue 253]: https://github.com/teloxide/teloxide/issues/253 [pr 257]: https://github.com/teloxide/teloxide/pull/257 diff --git a/README.md b/README.md index d41c3ce5..fb83e646 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,9 @@ Commands are strongly typed and defined declaratively, similar to how we define ([Full](./examples/simple_commands_bot/src/main.rs)) ```rust,no_run -use teloxide::{utils::command::BotCommand, prelude::*}; +use teloxide::{prelude::*, utils::command::BotCommand}; + +use std::error::Error; #[derive(BotCommand)] #[command(rename = "lowercase", description = "These commands are supported:")] @@ -143,14 +145,17 @@ enum Command { UsernameAndAge { username: String, age: u8 }, } -async fn answer(cx: UpdateWithCx, Message>, command: Command) -> ResponseResult<()> { +async fn answer( + cx: UpdateWithCx, Message>, + command: Command, +) -> Result<(), Box> { match command { Command::Help => cx.answer(Command::descriptions()).send().await?, Command::Username(username) => { - cx.answer_str(format!("Your username is @{}.", username)).await? + cx.answer(format!("Your username is @{}.", username)).await? } Command::UsernameAndAge { username, age } => { - cx.answer_str(format!("Your username is @{} and age is {}.", username, age)).await? + cx.answer(format!("Your username is @{} and age is {}.", username, age)).await? } }; diff --git a/examples/admin_bot/src/main.rs b/examples/admin_bot/src/main.rs index 3b427c72..0351f9bd 100644 --- a/examples/admin_bot/src/main.rs +++ b/examples/admin_bot/src/main.rs @@ -1,4 +1,4 @@ -use std::{convert::TryInto, str::FromStr}; +use std::{convert::TryInto, error::Error, str::FromStr}; use teloxide::{prelude::*, utils::command::BotCommand}; @@ -65,7 +65,7 @@ fn calc_restrict_time(time: u32, unit: UnitOfTime) -> u32 { type Cx = UpdateWithCx, Message>; // Mute a user with a replied message. -async fn mute_user(cx: &Cx, time: u32) -> ResponseResult<()> { +async fn mute_user(cx: &Cx, time: u32) -> Result<(), Box> { match cx.update.reply_to_message() { Some(msg1) => { cx.requester @@ -85,7 +85,7 @@ async fn mute_user(cx: &Cx, time: u32) -> ResponseResult<()> { } // Kick a user with a replied message. -async fn kick_user(cx: &Cx) -> ResponseResult<()> { +async fn kick_user(cx: &Cx) -> Result<(), Box> { match cx.update.reply_to_message() { Some(mes) => { // bot.unban_chat_member can also kicks a user from a group chat. @@ -102,7 +102,7 @@ async fn kick_user(cx: &Cx) -> ResponseResult<()> { } // Ban a user with replied message. -async fn ban_user(cx: &Cx, time: u32) -> ResponseResult<()> { +async fn ban_user(cx: &Cx, time: u32) -> Result<(), Box> { match cx.update.reply_to_message() { Some(message) => { cx.requester @@ -120,7 +120,7 @@ async fn ban_user(cx: &Cx, time: u32) -> ResponseResult<()> { Ok(()) } -async fn action(cx: Cx, command: Command) -> ResponseResult<()> { +async fn action(cx: Cx, command: Command) -> Result<(), Box> { match command { Command::Help => cx.answer(Command::descriptions()).send().await.map(|_| ())?, Command::Kick => kick_user(&cx).await?, diff --git a/examples/simple_commands_bot/src/main.rs b/examples/simple_commands_bot/src/main.rs index a510a72c..8b34c7b0 100644 --- a/examples/simple_commands_bot/src/main.rs +++ b/examples/simple_commands_bot/src/main.rs @@ -1,5 +1,7 @@ use teloxide::{prelude::*, utils::command::BotCommand}; +use std::error::Error; + #[derive(BotCommand)] #[command(rename = "lowercase", description = "These commands are supported:")] enum Command { @@ -11,7 +13,10 @@ enum Command { UsernameAndAge { username: String, age: u8 }, } -async fn answer(cx: UpdateWithCx, Message>, command: Command) -> ResponseResult<()> { +async fn answer( + cx: UpdateWithCx, Message>, + command: Command, +) -> Result<(), Box> { match command { Command::Help => cx.answer(Command::descriptions()).send().await?, Command::Username(username) => { diff --git a/examples/sqlite_remember_bot/src/main.rs b/examples/sqlite_remember_bot/src/main.rs index ba13ad1e..a6e62b79 100644 --- a/examples/sqlite_remember_bot/src/main.rs +++ b/examples/sqlite_remember_bot/src/main.rs @@ -29,7 +29,7 @@ async fn handle_message( cx: UpdateWithCx, Message>, dialogue: Dialogue, ) -> TransitionOut { - match cx.update.text_owned() { + match cx.update.text().map(ToOwned::to_owned) { None => { cx.answer("Send me a text message.").await?; next(dialogue) diff --git a/src/lib.rs b/src/lib.rs index 8e1bbd8a..7a3403da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,7 +66,6 @@ pub mod utils; #[doc(inline)] pub use teloxide_core::*; -use teloxide_core::requests::ResponseResult; #[cfg(feature = "macros")] // FIXME(waffle): use `docsrs` here when issue with combine is resolved #[cfg_attr(all(teloxide_docsrs, feature = "nightly"), doc(cfg(feature = "macros")))] @@ -76,6 +75,8 @@ pub use teloxide_macros::teloxide; #[doc(include = "../README.md")] enum ReadmeDocTests {} +use teloxide_core::requests::ResponseResult; + /// A shortcut for `ResponseResult::Ok(val)`. pub fn respond(val: T) -> ResponseResult { ResponseResult::Ok(val) diff --git a/src/prelude.rs b/src/prelude.rs index d246a85e..4ccebaa4 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -14,7 +14,7 @@ pub use crate::{ pub use teloxide_core::{ adaptors::AutoSend, - requests::{Request, ResponseResult}, + requests::Request, types::{ CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll, PollAnswer, PreCheckoutQuery, ShippingQuery, From 68e3c5e089d1fc2bbe42449bdf66fb33162223ea Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Sat, 20 Mar 2021 22:57:32 +0600 Subject: [PATCH 2/2] Update src/prelude.rs Co-authored-by: Waffle Lapkin --- src/prelude.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/prelude.rs b/src/prelude.rs index 4ccebaa4..211a1b49 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -14,7 +14,6 @@ pub use crate::{ pub use teloxide_core::{ adaptors::AutoSend, - requests::Request, types::{ CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll, PollAnswer, PreCheckoutQuery, ShippingQuery,