mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-20 13:59:00 +01:00
fixed parse_command: now call parse_command_with_prefix("/", ...)
This commit is contained in:
parent
93a2376e92
commit
8cbe9d8aff
2 changed files with 6 additions and 15 deletions
|
@ -45,7 +45,7 @@ futures = "0.3.1"
|
||||||
pin-project = "0.4.6"
|
pin-project = "0.4.6"
|
||||||
serde_with_macros = "1.0.1"
|
serde_with_macros = "1.0.1"
|
||||||
|
|
||||||
teloxide-macros = { path = "teloxide-macros" }
|
teloxide-macros = "0.1.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
smart-default = "0.6.0"
|
smart-default = "0.6.0"
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
//!
|
//!
|
||||||
//! let (command, args) =
|
//! let (command, args) =
|
||||||
//! parse_command("/ban@MyBotName 3 hours", "MyBotName").unwrap();
|
//! parse_command("/ban@MyBotName 3 hours", "MyBotName").unwrap();
|
||||||
//! assert_eq!(command, "/ban@MyBotName");
|
//! assert_eq!(command, "ban");
|
||||||
//! assert_eq!(args, vec!["3", "hours"]);
|
//! assert_eq!(args, vec!["3", "hours"]);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
@ -117,23 +117,14 @@ pub trait BotCommand: Sized {
|
||||||
///
|
///
|
||||||
/// let text = "/mute@my_admin_bot 5 hours";
|
/// let text = "/mute@my_admin_bot 5 hours";
|
||||||
/// let (command, args) = parse_command(text, "my_admin_bot").unwrap();
|
/// let (command, args) = parse_command(text, "my_admin_bot").unwrap();
|
||||||
/// assert_eq!(command, "/mute");
|
/// assert_eq!(command, "mute");
|
||||||
/// assert_eq!(args, vec!["5", "hours"]);
|
/// assert_eq!(args, vec!["5", "hours"]);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse_command<N>(text: &str, bot_name: N) -> Option<(&str, Vec<&str>)>
|
pub fn parse_command<N>(text: &str, bot_name: N) -> Option<(&str, Vec<&str>)>
|
||||||
where
|
where
|
||||||
N: AsRef<str>,
|
N: AsRef<str>,
|
||||||
{
|
{
|
||||||
let mut words = text.split_whitespace();
|
parse_command_with_prefix("/", text, bot_name)
|
||||||
let mut splited = words.next()?.split('@');
|
|
||||||
let command = splited.next()?;
|
|
||||||
let bot = splited.next();
|
|
||||||
match bot {
|
|
||||||
Some(name) if name == bot_name.as_ref() => {}
|
|
||||||
None => {}
|
|
||||||
_ => return None,
|
|
||||||
}
|
|
||||||
Some((command, words.collect()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a string into a command with args (custom prefix).
|
/// Parses a string into a command with args (custom prefix).
|
||||||
|
@ -179,7 +170,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_command_with_args_() {
|
fn parse_command_with_args_() {
|
||||||
let data = "/command arg1 arg2";
|
let data = "/command arg1 arg2";
|
||||||
let expected = Some(("/command", vec!["arg1", "arg2"]));
|
let expected = Some(("command", vec!["arg1", "arg2"]));
|
||||||
let actual = parse_command(data, "");
|
let actual = parse_command(data, "");
|
||||||
assert_eq!(actual, expected)
|
assert_eq!(actual, expected)
|
||||||
}
|
}
|
||||||
|
@ -187,7 +178,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_command_with_args_without_args() {
|
fn parse_command_with_args_without_args() {
|
||||||
let data = "/command";
|
let data = "/command";
|
||||||
let expected = Some(("/command", vec![]));
|
let expected = Some(("command", vec![]));
|
||||||
let actual = parse_command(data, "");
|
let actual = parse_command(data, "");
|
||||||
assert_eq!(actual, expected)
|
assert_eq!(actual, expected)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue