changed signature to Command::parse(text, bot_name) but it failed

This commit is contained in:
p0lunin 2020-02-22 18:58:29 +02:00
parent 15d5a02154
commit a3e5331312
3 changed files with 16 additions and 8 deletions

View file

@ -18,6 +18,7 @@ pub trait DispatcherHandlerRxExt {
/// arbitrary messages.
fn commands<C>(
self,
bot_name: &'static str,
) -> BoxStream<'static, (DispatcherHandlerCx<Message>, C, Vec<String>)>
where
Self: Stream<Item = DispatcherHandlerCx<Message>>,
@ -41,13 +42,14 @@ where
fn commands<C>(
self,
bot_name: &'static str,
) -> BoxStream<'static, (DispatcherHandlerCx<Message>, C, Vec<String>)>
where
Self: Stream<Item = DispatcherHandlerCx<Message>>,
C: BotCommand,
{
Box::pin(self.text_messages().filter_map(|(cx, text)| async move {
C::parse(&text).map(|(command, args)| {
C::parse(&text, bot_name).map(|(command, args)| {
(
cx,
command,

View file

@ -92,7 +92,7 @@ pub use teloxide_macros::BotCommand;
pub trait BotCommand: Sized {
fn try_from(s: &str) -> Option<Self>;
fn descriptions() -> String;
fn parse(s: &str) -> Option<(Self, Vec<&str>)>;
fn parse<'a, 'b>(s: &'a str, bot_name: &'b str) -> Option<(Self, Vec<&'a str>)>;
}
/// Parses a string into a command with args.
@ -170,7 +170,7 @@ mod tests {
let data = "/start arg1 arg2";
let expected = Some((DefaultCommands::Start, vec!["arg1", "arg2"]));
let actual = DefaultCommands::parse(data);
let actual = DefaultCommands::parse(data, "");
assert_eq!(actual, expected)
}
@ -186,7 +186,7 @@ mod tests {
let data = "!start arg1 arg2";
let expected = Some((DefaultCommands::Start, vec!["arg1", "arg2"]));
let actual = DefaultCommands::parse(data);
let actual = DefaultCommands::parse(data, "");
assert_eq!(actual, expected)
}
@ -202,7 +202,7 @@ mod tests {
assert_eq!(
DefaultCommands::Start,
DefaultCommands::parse("!start").unwrap().0
DefaultCommands::parse("!start", "").unwrap().0
);
assert_eq!(
DefaultCommands::descriptions(),

View file

@ -107,10 +107,16 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream {
fn descriptions() -> String {
std::concat!(#global_description #(#variant_str2, " - ", #variant_description, '\n'),*).to_string()
}
fn parse(s: &str) -> Option<(Self, Vec<&str>)> {
fn parse<'a, 'b>(s: &'a str, bot_name: &'b str) -> Option<(Self, Vec<&'a str>)> {
let mut words = s.split_whitespace();
let word_command = words.next()?.split('@').next()?;
let command = Self::try_from(word_command)?;
let splited = words.next()?.split('@');
let command_raw = splited.next()?;
let bot = splited.next();
match bot {
Some(name) if name == bot_name || None => {}
_ => return None,
}
let command = Self::try_from(command_raw)?;
Some((command, words.collect()))
}
}