Use Box<dyn Error + Send + Sync> in the examples

This commit is contained in:
Temirkhan Myrzamadi 2021-03-19 17:58:22 +06:00 committed by Eric S. Londres
parent d625b69938
commit 09cca0faff
7 changed files with 25 additions and 13 deletions

View file

@ -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 `#[non_exhaustive]` annotation is removed from the enum, type of `TargetMessage::Inline::inline_message_id` changed
`i32` => `String`. `TargetMessage` now implements `From<String>`, `get_game_high_scores` and `set_game_score` use `i32` => `String`. `TargetMessage` now implements `From<String>`, `get_game_high_scores` and `set_game_score` use
`Into<TargetMessage>` to accept `String`s. ([issue 253], [pr 257]) `Into<TargetMessage>` to accept `String`s. ([issue 253], [pr 257])
- Remove `ResponseResult` from `prelude`.
[issue 253]: https://github.com/teloxide/teloxide/issues/253 [issue 253]: https://github.com/teloxide/teloxide/issues/253
[pr 257]: https://github.com/teloxide/teloxide/pull/257 [pr 257]: https://github.com/teloxide/teloxide/pull/257

View file

@ -130,7 +130,9 @@ Commands are strongly typed and defined declaratively, similar to how we define
([Full](./examples/simple_commands_bot/src/main.rs)) ([Full](./examples/simple_commands_bot/src/main.rs))
```rust,no_run ```rust,no_run
use teloxide::{utils::command::BotCommand, prelude::*}; use teloxide::{prelude::*, utils::command::BotCommand};
use std::error::Error;
#[derive(BotCommand)] #[derive(BotCommand)]
#[command(rename = "lowercase", description = "These commands are supported:")] #[command(rename = "lowercase", description = "These commands are supported:")]
@ -143,14 +145,17 @@ enum Command {
UsernameAndAge { username: String, age: u8 }, UsernameAndAge { username: String, age: u8 },
} }
async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> ResponseResult<()> { async fn answer(
cx: UpdateWithCx<AutoSend<Bot>, Message>,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match command { match command {
Command::Help => cx.answer(Command::descriptions()).send().await?, Command::Help => cx.answer(Command::descriptions()).send().await?,
Command::Username(username) => { Command::Username(username) => {
cx.answer_str(format!("Your username is @{}.", username)).await? cx.answer(format!("Your username is @{}.", username)).await?
} }
Command::UsernameAndAge { username, age } => { 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?
} }
}; };

View file

@ -1,4 +1,4 @@
use std::{convert::TryInto, str::FromStr}; use std::{convert::TryInto, error::Error, str::FromStr};
use teloxide::{prelude::*, utils::command::BotCommand}; use teloxide::{prelude::*, utils::command::BotCommand};
@ -65,7 +65,7 @@ fn calc_restrict_time(time: u32, unit: UnitOfTime) -> u32 {
type Cx = UpdateWithCx<AutoSend<Bot>, Message>; type Cx = UpdateWithCx<AutoSend<Bot>, Message>;
// Mute a user with a replied 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<dyn Error + Send + Sync>> {
match cx.update.reply_to_message() { match cx.update.reply_to_message() {
Some(msg1) => { Some(msg1) => {
cx.requester cx.requester
@ -85,7 +85,7 @@ async fn mute_user(cx: &Cx, time: u32) -> ResponseResult<()> {
} }
// Kick a user with a replied message. // Kick a user with a replied message.
async fn kick_user(cx: &Cx) -> ResponseResult<()> { async fn kick_user(cx: &Cx) -> Result<(), Box<dyn Error + Send + Sync>> {
match cx.update.reply_to_message() { match cx.update.reply_to_message() {
Some(mes) => { Some(mes) => {
// bot.unban_chat_member can also kicks a user from a group chat. // 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. // 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<dyn Error + Send + Sync>> {
match cx.update.reply_to_message() { match cx.update.reply_to_message() {
Some(message) => { Some(message) => {
cx.requester cx.requester
@ -120,7 +120,7 @@ async fn ban_user(cx: &Cx, time: u32) -> ResponseResult<()> {
Ok(()) Ok(())
} }
async fn action(cx: Cx, command: Command) -> ResponseResult<()> { async fn action(cx: Cx, command: Command) -> Result<(), Box<dyn Error + Send + Sync>> {
match command { match command {
Command::Help => cx.answer(Command::descriptions()).send().await.map(|_| ())?, Command::Help => cx.answer(Command::descriptions()).send().await.map(|_| ())?,
Command::Kick => kick_user(&cx).await?, Command::Kick => kick_user(&cx).await?,

View file

@ -1,5 +1,7 @@
use teloxide::{prelude::*, utils::command::BotCommand}; use teloxide::{prelude::*, utils::command::BotCommand};
use std::error::Error;
#[derive(BotCommand)] #[derive(BotCommand)]
#[command(rename = "lowercase", description = "These commands are supported:")] #[command(rename = "lowercase", description = "These commands are supported:")]
enum Command { enum Command {
@ -11,7 +13,10 @@ enum Command {
UsernameAndAge { username: String, age: u8 }, UsernameAndAge { username: String, age: u8 },
} }
async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> ResponseResult<()> { async fn answer(
cx: UpdateWithCx<AutoSend<Bot>, Message>,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match command { match command {
Command::Help => cx.answer(Command::descriptions()).send().await?, Command::Help => cx.answer(Command::descriptions()).send().await?,
Command::Username(username) => { Command::Username(username) => {

View file

@ -29,7 +29,7 @@ async fn handle_message(
cx: UpdateWithCx<AutoSend<Bot>, Message>, cx: UpdateWithCx<AutoSend<Bot>, Message>,
dialogue: Dialogue, dialogue: Dialogue,
) -> TransitionOut<Dialogue> { ) -> TransitionOut<Dialogue> {
match cx.update.text_owned() { match cx.update.text().map(ToOwned::to_owned) {
None => { None => {
cx.answer("Send me a text message.").await?; cx.answer("Send me a text message.").await?;
next(dialogue) next(dialogue)

View file

@ -66,7 +66,6 @@ pub mod utils;
#[doc(inline)] #[doc(inline)]
pub use teloxide_core::*; pub use teloxide_core::*;
use teloxide_core::requests::ResponseResult;
#[cfg(feature = "macros")] #[cfg(feature = "macros")]
// FIXME(waffle): use `docsrs` here when issue with combine is resolved <https://github.com/teloxide/teloxide/pull/305#issuecomment-716172103> // FIXME(waffle): use `docsrs` here when issue with combine is resolved <https://github.com/teloxide/teloxide/pull/305#issuecomment-716172103>
#[cfg_attr(all(teloxide_docsrs, feature = "nightly"), doc(cfg(feature = "macros")))] #[cfg_attr(all(teloxide_docsrs, feature = "nightly"), doc(cfg(feature = "macros")))]
@ -76,6 +75,8 @@ pub use teloxide_macros::teloxide;
#[doc(include = "../README.md")] #[doc(include = "../README.md")]
enum ReadmeDocTests {} enum ReadmeDocTests {}
use teloxide_core::requests::ResponseResult;
/// A shortcut for `ResponseResult::Ok(val)`. /// A shortcut for `ResponseResult::Ok(val)`.
pub fn respond<T>(val: T) -> ResponseResult<T> { pub fn respond<T>(val: T) -> ResponseResult<T> {
ResponseResult::Ok(val) ResponseResult::Ok(val)

View file

@ -14,7 +14,7 @@ pub use crate::{
pub use teloxide_core::{ pub use teloxide_core::{
adaptors::AutoSend, adaptors::AutoSend,
requests::{Request, ResponseResult}, requests::Request,
types::{ types::{
CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll, CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll,
PollAnswer, PreCheckoutQuery, ShippingQuery, PollAnswer, PreCheckoutQuery, ShippingQuery,