45 lines
1.4 KiB
Rust
45 lines
1.4 KiB
Rust
/// 解析斜杠命令
|
|
/// 返回 (command_name, args) 或 None
|
|
pub fn parse_slash_command(content: &str) -> Option<(&str, &str)> {
|
|
let trimmed = content.trim();
|
|
if !trimmed.starts_with('/') {
|
|
return None;
|
|
}
|
|
let rest = &trimmed[1..];
|
|
if let Some((name, args)) = rest.split_once(' ') {
|
|
Some((name, args.trim()))
|
|
} else {
|
|
Some((rest, ""))
|
|
}
|
|
}
|
|
|
|
/// 检查内容是否匹配指定命令
|
|
pub fn command_matches(content: &str, aliases: &[&str]) -> bool {
|
|
let trimmed = content.trim();
|
|
aliases.iter().any(|&alias| trimmed == alias || trimmed.starts_with(&format!("{} ", alias)))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_parse_slash_command() {
|
|
assert_eq!(parse_slash_command("/reset"), Some(("reset", "")));
|
|
assert_eq!(parse_slash_command("/reset arg"), Some(("reset", "arg")));
|
|
assert_eq!(parse_slash_command("/new hello world"), Some(("new", "hello world")));
|
|
assert_eq!(parse_slash_command("hello"), None);
|
|
assert_eq!(parse_slash_command("/"), Some(("", "")));
|
|
}
|
|
|
|
#[test]
|
|
fn test_command_matches() {
|
|
let aliases = &["/reset", "/new"];
|
|
assert!(command_matches("/reset", aliases));
|
|
assert!(command_matches("/new", aliases));
|
|
assert!(command_matches("/reset arg", aliases));
|
|
assert!(!command_matches("/help", aliases));
|
|
assert!(!command_matches("reset", aliases));
|
|
}
|
|
}
|