Merge pull request #728 from teloxide/bot_commands_parse_and_str

Make `BotCommands::parse` accept bot username as `&str`

Former-commit-id: 51e3cafe56
This commit is contained in:
Hirrolot 2022-10-06 15:44:52 +06:00 committed by GitHub
commit b530f63608
14 changed files with 43 additions and 49 deletions

View file

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `CommandDescriptions::{new, global_description, username, username_from_me}`.
- `teloxide::filter_command`.
- `teloxide::dispatching::dialogue::enter`.
- `BotCommands::parse` now accept `bot_username` as `&str`
### Added

View file

@ -67,7 +67,7 @@ dptree = "0.3.0"
# These lines are used only for development.
teloxide-core = { git = "https://github.com/teloxide/teloxide-core", rev = "00165e6", default-features = false }
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", rev = "0e79c37", optional = true }
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", rev = "a2a79e29cb106a5d3052cf32a23e698db859cc2a", optional = true }
# dptree = { git = "https://github.com/teloxide/dptree", rev = "df578e4" }
tokio = { version = "1.8", features = ["fs"] }

View file

@ -133,7 +133,7 @@ async fn main() {
}
#[derive(BotCommands, Clone)]
#[command(rename = "lowercase", description = "These commands are supported:")]
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
enum Command {
#[command(description = "display this text.")]
Help,

View file

@ -14,7 +14,7 @@ use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommands};
// %PREFIX%%COMMAND% - %DESCRIPTION%
#[derive(BotCommands, Clone)]
#[command(
rename = "lowercase",
rename_rule = "lowercase",
description = "Use commands in format /%command% %num% %unit%",
parse_with = "split"
)]

View file

@ -10,7 +10,7 @@ use teloxide::{
};
#[derive(BotCommands)]
#[command(rename = "lowercase", description = "These commands are supported:")]
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
enum Command {
#[command(description = "Display this text")]
Help,

View file

@ -11,7 +11,7 @@ async fn main() {
}
#[derive(BotCommands, Clone)]
#[command(rename = "lowercase", description = "These commands are supported:")]
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
enum Command {
#[command(description = "display this text.")]
Help,

View file

@ -22,7 +22,7 @@ pub enum State {
}
#[derive(Clone, BotCommands)]
#[command(rename = "lowercase", description = "These commands are supported:")]
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
pub enum Command {
#[command(description = "get your number.")]
Get,

View file

@ -96,7 +96,7 @@ struct ConfigParameters {
}
#[derive(BotCommands, Clone)]
#[command(rename = "lowercase", description = "Simple commands")]
#[command(rename_rule = "lowercase", description = "Simple commands")]
enum SimpleCommand {
#[command(description = "shows this message.")]
Help,
@ -107,7 +107,7 @@ enum SimpleCommand {
}
#[derive(BotCommands, Clone)]
#[command(rename = "lowercase", description = "Maintainer commands")]
#[command(rename_rule = "lowercase", description = "Maintainer commands")]
enum MaintainerCommands {
#[command(parse_with = "split", description = "generate a number within range")]
Rand { from: u64, to: u64 },

View file

@ -33,7 +33,7 @@ pub enum State {
}
#[derive(BotCommands, Clone)]
#[command(rename = "lowercase", description = "These commands are supported:")]
#[command(rename_rule = "lowercase", description = "These commands are supported:")]
enum Command {
#[command(description = "display this text.")]
Help,

View file

@ -25,7 +25,7 @@
//! ```no_run
//! # use teloxide::utils::command::BotCommands;
//! #[derive(BotCommands, Clone)]
//! #[command(rename = "lowercase", description = "These commands are supported:")]
//! #[command(rename_rule = "lowercase", description = "These commands are supported:")]
//! enum Command {
//! #[command(description = "display this text.")]
//! Help,

View file

@ -86,7 +86,7 @@ mod tests {
use crate as teloxide; // fixup for the `BotCommands` macro
#[derive(BotCommands, Clone)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum Cmd {
B,
}

View file

@ -90,6 +90,6 @@ where
{
dptree::entry().chain(dptree::filter_map(move |message: Message, me: Me| {
let bot_name = me.user.username.expect("Bots must have a username");
message.text().and_then(|text| C::parse(text, bot_name).ok())
message.text().and_then(|text| C::parse(text, &bot_name).ok())
}))
}

View file

@ -13,7 +13,7 @@
//! type UnitOfTime = u8;
//!
//! #[derive(BotCommands, PartialEq, Debug)]
//! #[command(rename = "lowercase", parse_with = "split")]
//! #[command(rename_rule = "lowercase", parse_with = "split")]
//! enum AdminCommand {
//! Mute(UnitOfTime, char),
//! Ban(UnitOfTime, char),
@ -70,7 +70,7 @@ pub use teloxide_macros::BotCommands;
/// type UnitOfTime = u8;
///
/// #[derive(BotCommands, PartialEq, Debug)]
/// #[command(rename = "lowercase", parse_with = "split")]
/// #[command(rename_rule = "lowercase", parse_with = "split")]
/// enum AdminCommand {
/// Mute(UnitOfTime, char),
/// Ban(UnitOfTime, char),
@ -105,7 +105,7 @@ pub use teloxide_macros::BotCommands;
/// use teloxide::utils::command::BotCommands;
///
/// #[derive(BotCommands, PartialEq, Debug)]
/// #[command(rename = "lowercase")]
/// #[command(rename_rule = "lowercase")]
/// enum Command {
/// Text(String),
/// }
@ -125,7 +125,7 @@ pub use teloxide_macros::BotCommands;
/// use teloxide::utils::command::BotCommands;
///
/// #[derive(BotCommands, PartialEq, Debug)]
/// #[command(rename = "lowercase", parse_with = "split")]
/// #[command(rename_rule = "lowercase", parse_with = "split")]
/// enum Command {
/// Nums(u8, u16, i32),
/// }
@ -145,7 +145,7 @@ pub use teloxide_macros::BotCommands;
/// use teloxide::utils::command::BotCommands;
///
/// #[derive(BotCommands, PartialEq, Debug)]
/// #[command(rename = "lowercase", parse_with = "split", separator = "|")]
/// #[command(rename_rule = "lowercase", parse_with = "split", separator = "|")]
/// enum Command {
/// Nums(u8, u16, i32),
/// }
@ -192,7 +192,7 @@ pub use teloxide_macros::BotCommands;
/// }
///
/// #[derive(BotCommands, PartialEq, Debug)]
/// #[command(rename = "lowercase")]
/// #[command(rename_rule = "lowercase")]
/// enum Command {
/// #[command(parse_with = "accept_two_digits")]
/// Num(u8),
@ -218,9 +218,7 @@ pub trait BotCommands: Sized {
///
/// `bot_username` is required to parse commands like
/// `/cmd@username_of_the_bot`.
fn parse<N>(s: &str, bot_username: N) -> Result<Self, ParseError>
where
N: Into<String>;
fn parse(s: &str, bot_username: &str) -> Result<Self, ParseError>;
/// Returns descriptions of the commands suitable to be shown to the user
/// (for example when `/help` command is used).

View file

@ -11,7 +11,7 @@ use teloxide::utils::command::BotCommands;
#[cfg(feature = "macros")]
fn parse_command_with_args() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum DefaultCommands {
Start(String),
Help,
@ -27,7 +27,7 @@ fn parse_command_with_args() {
#[cfg(feature = "macros")]
fn parse_command_with_non_string_arg() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum DefaultCommands {
Start(i32),
Help,
@ -43,7 +43,7 @@ fn parse_command_with_non_string_arg() {
#[cfg(feature = "macros")]
fn attribute_prefix() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum DefaultCommands {
#[command(prefix = "!")]
Start(String),
@ -60,7 +60,7 @@ fn attribute_prefix() {
#[cfg(feature = "macros")]
fn many_attributes() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum DefaultCommands {
#[command(prefix = "!", description = "desc")]
Start,
@ -75,7 +75,7 @@ fn many_attributes() {
#[cfg(feature = "macros")]
fn global_attributes() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(prefix = "!", rename = "lowercase", description = "Bot commands")]
#[command(prefix = "!", rename_rule = "lowercase", description = "Bot commands")]
enum DefaultCommands {
#[command(prefix = "/")]
Start,
@ -91,7 +91,7 @@ fn global_attributes() {
#[cfg(feature = "macros")]
fn parse_command_with_bot_name() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum DefaultCommands {
#[command(prefix = "/")]
Start,
@ -108,7 +108,7 @@ fn parse_command_with_bot_name() {
#[cfg(feature = "macros")]
fn parse_with_split() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
#[command(parse_with = "split")]
enum DefaultCommands {
Start(u8, String),
@ -125,7 +125,7 @@ fn parse_with_split() {
#[cfg(feature = "macros")]
fn parse_with_split2() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
#[command(parse_with = "split", separator = "|")]
enum DefaultCommands {
Start(u8, String),
@ -159,7 +159,7 @@ fn parse_custom_parser() {
use parser::custom_parse_function;
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum DefaultCommands {
#[command(parse_with = "custom_parse_function")]
Start(u8, String),
@ -185,7 +185,7 @@ fn parse_custom_parser() {
#[cfg(feature = "macros")]
fn parse_named_fields() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
#[command(parse_with = "split")]
enum DefaultCommands {
Start { num: u8, data: String },
@ -202,7 +202,7 @@ fn parse_named_fields() {
#[cfg(feature = "macros")]
fn descriptions_off() {
#[derive(BotCommands, Debug, PartialEq)]
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
enum DefaultCommands {
#[command(description = "off")]
Start,
@ -217,24 +217,24 @@ fn descriptions_off() {
fn rename_rules() {
#[derive(BotCommands, Debug, PartialEq)]
enum DefaultCommands {
#[command(rename = "lowercase")]
#[command(rename_rule = "lowercase")]
AaaAaa,
#[command(rename = "UPPERCASE")]
#[command(rename_rule = "UPPERCASE")]
BbbBbb,
#[command(rename = "PascalCase")]
#[command(rename_rule = "PascalCase")]
CccCcc,
#[command(rename = "camelCase")]
#[command(rename_rule = "camelCase")]
DddDdd,
#[command(rename = "snake_case")]
#[command(rename_rule = "snake_case")]
EeeEee,
#[command(rename = "SCREAMING_SNAKE_CASE")]
#[command(rename_rule = "SCREAMING_SNAKE_CASE")]
FffFff,
#[command(rename = "kebab-case")]
#[command(rename_rule = "kebab-case")]
GggGgg,
#[command(rename = "SCREAMING-KEBAB-CASE")]
#[command(rename_rule = "SCREAMING-KEBAB-CASE")]
HhhHhh,
//#[command(rename = "Bar")]
//Foo,
#[command(rename = "Bar")]
Foo,
}
assert_eq!(DefaultCommands::AaaAaa, DefaultCommands::parse("/aaaaaa", "").unwrap());
@ -245,15 +245,10 @@ fn rename_rules() {
assert_eq!(DefaultCommands::FffFff, DefaultCommands::parse("/FFF_FFF", "").unwrap());
assert_eq!(DefaultCommands::GggGgg, DefaultCommands::parse("/ggg-ggg", "").unwrap());
assert_eq!(DefaultCommands::HhhHhh, DefaultCommands::parse("/HHH-HHH", "").unwrap());
//assert_eq!(DefaultCommands::Foo, DefaultCommands::parse("/Bar",
// "").unwrap());
assert_eq!(DefaultCommands::Foo, DefaultCommands::parse("/Bar", "").unwrap());
// assert_eq!(
// "/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/
// HHH-HHH\n/Bar", DefaultCommands::descriptions().to_string()
// );
assert_eq!(
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/HHH-HHH",
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/HHH-HHH\n/Bar",
DefaultCommands::descriptions().to_string()
);
}