Make command parsing helper function public
This commit is contained in:
parent
4479126500
commit
3aaef7ab11
2 changed files with 35 additions and 47 deletions
|
|
@ -15,6 +15,7 @@ Procedure when bumping the version number:
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- `bot::botrulez::Uptime` now implements `bot::command::Command`
|
- `bot::botrulez::Uptime` now implements `bot::command::Command`
|
||||||
|
- `bot::command::parse_prefix_initiated`
|
||||||
- `bot::commands::Commands::fallthrough`
|
- `bot::commands::Commands::fallthrough`
|
||||||
- `bot::commands::Commands::set_fallthrough`
|
- `bot::commands::Commands::set_fallthrough`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,11 @@ use crate::nick;
|
||||||
|
|
||||||
use super::{Command, Context};
|
use super::{Command, Context};
|
||||||
|
|
||||||
/// Parse leading whitespace followed by an `!`-initiated command.
|
/// Parse leading whitespace followed by an prefix-initiated command.
|
||||||
///
|
///
|
||||||
/// Returns the command name and the remaining text with one leading whitespace
|
/// Returns the command name and the remaining text with one leading whitespace
|
||||||
/// removed. The remaining text may be the empty string.
|
/// removed. The remaining text may be the empty string.
|
||||||
fn parse_command<'a>(text: &'a str, prefix: &str) -> Option<(&'a str, &'a str)> {
|
pub fn parse_prefix_initiated<'a>(text: &'a str, prefix: &str) -> Option<(&'a str, &'a str)> {
|
||||||
let text = text.trim_start();
|
let text = text.trim_start();
|
||||||
let text = text.strip_prefix(prefix)?;
|
let text = text.strip_prefix(prefix)?;
|
||||||
let (name, rest) = text.split_once(char::is_whitespace).unwrap_or((text, ""));
|
let (name, rest) = text.split_once(char::is_whitespace).unwrap_or((text, ""));
|
||||||
|
|
@ -19,20 +19,6 @@ fn parse_command<'a>(text: &'a str, prefix: &str) -> Option<(&'a str, &'a str)>
|
||||||
Some((name, rest))
|
Some((name, rest))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse leading whitespace followed by an `@`-initiated nick.
|
|
||||||
///
|
|
||||||
/// Returns the nick and the remaining text with one leading whitespace removed.
|
|
||||||
/// The remaining text may be the empty string.
|
|
||||||
fn parse_specific(text: &str) -> Option<(&str, &str)> {
|
|
||||||
let text = text.trim_start();
|
|
||||||
let text = text.strip_prefix('@')?;
|
|
||||||
let (name, rest) = text.split_once(char::is_whitespace).unwrap_or((text, ""));
|
|
||||||
if name.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
Some((name, rest))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Global<C> {
|
pub struct Global<C> {
|
||||||
prefix: String,
|
prefix: String,
|
||||||
name: String,
|
name: String,
|
||||||
|
|
@ -73,7 +59,7 @@ where
|
||||||
bot: &mut B,
|
bot: &mut B,
|
||||||
) -> Result<bool, E> {
|
) -> Result<bool, E> {
|
||||||
// TODO Replace with let-else
|
// TODO Replace with let-else
|
||||||
let (name, rest) = match parse_command(arg, &self.prefix) {
|
let (name, rest) = match parse_prefix_initiated(arg, &self.prefix) {
|
||||||
Some(parsed) => parsed,
|
Some(parsed) => parsed,
|
||||||
None => return Ok(false),
|
None => return Ok(false),
|
||||||
};
|
};
|
||||||
|
|
@ -126,7 +112,7 @@ where
|
||||||
bot: &mut B,
|
bot: &mut B,
|
||||||
) -> Result<bool, E> {
|
) -> Result<bool, E> {
|
||||||
// TODO Replace with let-else
|
// TODO Replace with let-else
|
||||||
let (name, rest) = match parse_command(arg, &self.prefix) {
|
let (name, rest) = match parse_prefix_initiated(arg, &self.prefix) {
|
||||||
Some(parsed) => parsed,
|
Some(parsed) => parsed,
|
||||||
None => return Ok(false),
|
None => return Ok(false),
|
||||||
};
|
};
|
||||||
|
|
@ -135,7 +121,7 @@ where
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if parse_specific(rest).is_some() {
|
if parse_prefix_initiated(rest, "@").is_some() {
|
||||||
// The command looks like a specific command. If we treated it like
|
// The command looks like a specific command. If we treated it like
|
||||||
// a general command match, we would interpret other bots' specific
|
// a general command match, we would interpret other bots' specific
|
||||||
// commands as general commands.
|
// commands as general commands.
|
||||||
|
|
@ -187,7 +173,7 @@ where
|
||||||
bot: &mut B,
|
bot: &mut B,
|
||||||
) -> Result<bool, E> {
|
) -> Result<bool, E> {
|
||||||
// TODO Replace with let-else
|
// TODO Replace with let-else
|
||||||
let (name, rest) = match parse_command(arg, &self.prefix) {
|
let (name, rest) = match parse_prefix_initiated(arg, &self.prefix) {
|
||||||
Some(parsed) => parsed,
|
Some(parsed) => parsed,
|
||||||
None => return Ok(false),
|
None => return Ok(false),
|
||||||
};
|
};
|
||||||
|
|
@ -197,7 +183,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Replace with let-else
|
// TODO Replace with let-else
|
||||||
let (nick, rest) = match parse_specific(rest) {
|
let (nick, rest) = match parse_prefix_initiated(rest, "@") {
|
||||||
Some(parsed) => parsed,
|
Some(parsed) => parsed,
|
||||||
None => return Ok(false),
|
None => return Ok(false),
|
||||||
};
|
};
|
||||||
|
|
@ -212,33 +198,34 @@ where
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::{parse_command, parse_specific};
|
use super::parse_prefix_initiated;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_command() {
|
fn test_parse_prefixed() {
|
||||||
assert_eq!(parse_command("!foo", "!"), Some(("foo", "")));
|
assert_eq!(parse_prefix_initiated("!foo", "!"), Some(("foo", "")));
|
||||||
assert_eq!(parse_command(" !foo", "!"), Some(("foo", "")));
|
assert_eq!(parse_prefix_initiated(" !foo", "!"), Some(("foo", "")));
|
||||||
assert_eq!(parse_command("!foo ", "!"), Some(("foo", " ")));
|
assert_eq!(
|
||||||
assert_eq!(parse_command(" !foo ", "!"), Some(("foo", " ")));
|
parse_prefix_initiated("!foo ", "!"),
|
||||||
assert_eq!(parse_command("!foo @bar", "!"), Some(("foo", "@bar")));
|
Some(("foo", " "))
|
||||||
assert_eq!(parse_command("!foo @bar", "!"), Some(("foo", " @bar")));
|
);
|
||||||
assert_eq!(parse_command("!foo @bar ", "!"), Some(("foo", "@bar ")));
|
assert_eq!(
|
||||||
assert_eq!(parse_command("! foo @bar", "!"), None);
|
parse_prefix_initiated(" !foo ", "!"),
|
||||||
assert_eq!(parse_command("!", "!"), None);
|
Some(("foo", " "))
|
||||||
assert_eq!(parse_command("?foo", "!"), None);
|
);
|
||||||
}
|
assert_eq!(
|
||||||
|
parse_prefix_initiated("!foo @bar", "!"),
|
||||||
#[test]
|
Some(("foo", "@bar"))
|
||||||
fn test_parse_specific() {
|
);
|
||||||
assert_eq!(parse_specific("@foo"), Some(("foo", "")));
|
assert_eq!(
|
||||||
assert_eq!(parse_specific(" @foo"), Some(("foo", "")));
|
parse_prefix_initiated("!foo @bar", "!"),
|
||||||
assert_eq!(parse_specific("@foo "), Some(("foo", " ")));
|
Some(("foo", " @bar"))
|
||||||
assert_eq!(parse_specific(" @foo "), Some(("foo", " ")));
|
);
|
||||||
assert_eq!(parse_specific("@foo !bar"), Some(("foo", "!bar")));
|
assert_eq!(
|
||||||
assert_eq!(parse_specific("@foo !bar"), Some(("foo", " !bar")));
|
parse_prefix_initiated("!foo @bar ", "!"),
|
||||||
assert_eq!(parse_specific("@foo !bar "), Some(("foo", "!bar ")));
|
Some(("foo", "@bar "))
|
||||||
assert_eq!(parse_specific("@ foo !bar"), None);
|
);
|
||||||
assert_eq!(parse_specific("@"), None);
|
assert_eq!(parse_prefix_initiated("! foo @bar", "!"), None);
|
||||||
assert_eq!(parse_specific("?foo"), None);
|
assert_eq!(parse_prefix_initiated("!", "!"), None);
|
||||||
|
assert_eq!(parse_prefix_initiated("?foo", "!"), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue