diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eacc015..3f83a4fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 02aaf40e..5295cccd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/README.md b/README.md index f03246ad..ab74b6d7 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/examples/admin.rs b/examples/admin.rs index 0a810d31..4be9a543 100644 --- a/examples/admin.rs +++ b/examples/admin.rs @@ -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" )] diff --git a/examples/buttons.rs b/examples/buttons.rs index 84b12ae1..ffb9846b 100644 --- a/examples/buttons.rs +++ b/examples/buttons.rs @@ -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, diff --git a/examples/command.rs b/examples/command.rs index 901d3866..00f44315 100644 --- a/examples/command.rs +++ b/examples/command.rs @@ -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, diff --git a/examples/db_remember.rs b/examples/db_remember.rs index c102cbe2..de09db0d 100644 --- a/examples/db_remember.rs +++ b/examples/db_remember.rs @@ -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, diff --git a/examples/dispatching_features.rs b/examples/dispatching_features.rs index e8ed5e16..983f0002 100644 --- a/examples/dispatching_features.rs +++ b/examples/dispatching_features.rs @@ -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 }, diff --git a/examples/purchase.rs b/examples/purchase.rs index 89ec5654..daf27cdd 100644 --- a/examples/purchase.rs +++ b/examples/purchase.rs @@ -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, diff --git a/src/dispatching.rs b/src/dispatching.rs index c19b2b93..f22c99c1 100644 --- a/src/dispatching.rs +++ b/src/dispatching.rs @@ -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, diff --git a/src/dispatching/handler_description.rs b/src/dispatching/handler_description.rs index 3ed2f621..92bbda38 100644 --- a/src/dispatching/handler_description.rs +++ b/src/dispatching/handler_description.rs @@ -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, } diff --git a/src/dispatching/handler_ext.rs b/src/dispatching/handler_ext.rs index f73dfb0a..932d5079 100644 --- a/src/dispatching/handler_ext.rs +++ b/src/dispatching/handler_ext.rs @@ -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()) })) } diff --git a/src/utils/command.rs b/src/utils/command.rs index 49dec198..57b9d5ff 100644 --- a/src/utils/command.rs +++ b/src/utils/command.rs @@ -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(s: &str, bot_username: N) -> Result - where - N: Into; + fn parse(s: &str, bot_username: &str) -> Result; /// Returns descriptions of the commands suitable to be shown to the user /// (for example when `/help` command is used). diff --git a/tests/command.rs b/tests/command.rs index d3322ea4..a42fca6b 100644 --- a/tests/command.rs +++ b/tests/command.rs @@ -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() ); }