From d5f38649211963292821801ee8e8e72134b19029 Mon Sep 17 00:00:00 2001 From: p0lunin Date: Sat, 25 Apr 2020 20:16:27 +0300 Subject: [PATCH] wip --- Cargo.toml | 2 +- src/dispatching/dispatcher_handler_rx_ext.rs | 9 ++--- src/utils/command.rs | 36 ++++++++++++++------ 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f4a5158..70fcf335 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ futures = "0.3.1" pin-project = "0.4.6" serde_with_macros = "1.0.1" -teloxide-macros = "0.2.1" +teloxide-macros = {git="https://github.com/teloxide/teloxide-macros", branch="dev"} [dev-dependencies] smart-default = "0.6.0" diff --git a/src/dispatching/dispatcher_handler_rx_ext.rs b/src/dispatching/dispatcher_handler_rx_ext.rs index e0aa607d..23a9c029 100644 --- a/src/dispatching/dispatcher_handler_rx_ext.rs +++ b/src/dispatching/dispatcher_handler_rx_ext.rs @@ -19,7 +19,7 @@ pub trait DispatcherHandlerRxExt { fn commands( self, bot_name: N, - ) -> BoxStream<'static, (DispatcherHandlerCx, C, Vec)> + ) -> BoxStream<'static, (DispatcherHandlerCx, C)> where Self: Stream>, C: BotCommand, @@ -44,7 +44,7 @@ where fn commands( self, bot_name: N, - ) -> BoxStream<'static, (DispatcherHandlerCx, C, Vec)> + ) -> BoxStream<'static, (DispatcherHandlerCx, C)> where Self: Stream>, C: BotCommand, @@ -56,13 +56,10 @@ where let bot_name = bot_name.clone(); async move { - C::parse(&text, &bot_name).map(|(command, args)| { + C::parse(&text, &bot_name).map(|command| { ( cx, command, - args.into_iter() - .map(ToOwned::to_owned) - .collect::>(), ) }) } diff --git a/src/utils/command.rs b/src/utils/command.rs index b69d4b9b..24618001 100644 --- a/src/utils/command.rs +++ b/src/utils/command.rs @@ -57,6 +57,7 @@ //! [examples/admin_bot]: https://github.com/teloxide/teloxide/blob/master/examples/miltiple_handlers_bot/ pub use teloxide_macros::BotCommand; +use std::str::FromStr; /// An enumeration of bot's commands. /// @@ -100,13 +101,28 @@ pub use teloxide_macros::BotCommand; /// /// All variant attributes overlap the `enum` attributes. pub trait BotCommand: Sized { - fn try_from(s: &str) -> Option; fn descriptions() -> String; - fn parse(s: &str, bot_name: N) -> Option<(Self, Vec<&str>)> + fn parse(s: &str, bot_name: N) -> Option where N: Into; } +pub trait CommandArgument { + fn parse(args: &mut String) -> Option where Self: Sized; +} + +impl CommandArgument for T { + fn parse(args: &mut String) -> Option { + match T::from_str(&args) { + Ok(res) => { + args.clear(); + Some(res) + } + Err(_) => None + } + } +} + /// Parses a string into a command with args. /// /// It calls [`parse_command_with_prefix`] with the default prefix `/`. @@ -191,12 +207,12 @@ mod tests { #[command(rename = "lowercase")] #[derive(BotCommand, Debug, PartialEq)] enum DefaultCommands { - Start, + Start(String), Help, } let data = "/start arg1 arg2"; - let expected = Some((DefaultCommands::Start, vec!["arg1", "arg2"])); + let expected = Some(DefaultCommands::Start("arg1 arg2".to_string())); let actual = DefaultCommands::parse(data, ""); assert_eq!(actual, expected) } @@ -207,12 +223,12 @@ mod tests { #[derive(BotCommand, Debug, PartialEq)] enum DefaultCommands { #[command(prefix = "!")] - Start, + Start(String), Help, } let data = "!start arg1 arg2"; - let expected = Some((DefaultCommands::Start, vec!["arg1", "arg2"])); + let expected = Some(DefaultCommands::Start("arg1 arg2".to_string())); let actual = DefaultCommands::parse(data, ""); assert_eq!(actual, expected) } @@ -229,7 +245,7 @@ mod tests { assert_eq!( DefaultCommands::Start, - DefaultCommands::parse("!start", "").unwrap().0 + DefaultCommands::parse("!start", "").unwrap() ); assert_eq!(DefaultCommands::descriptions(), "!start - desc\n/help\n"); } @@ -250,11 +266,11 @@ mod tests { assert_eq!( DefaultCommands::Start, - DefaultCommands::parse("/start", "MyNameBot").unwrap().0 + DefaultCommands::parse("/start", "MyNameBot").unwrap() ); assert_eq!( DefaultCommands::Help, - DefaultCommands::parse("!help", "MyNameBot").unwrap().0 + DefaultCommands::parse("!help", "MyNameBot").unwrap() ); assert_eq!( DefaultCommands::descriptions(), @@ -274,7 +290,7 @@ mod tests { assert_eq!( DefaultCommands::Start, - DefaultCommands::parse("/start@MyNameBot", "MyNameBot").unwrap().0 + DefaultCommands::parse("/start@MyNameBot", "MyNameBot").unwrap() ); } }