refactor(command_parser): separate Token::Value field into name and aliases

This commit is contained in:
dusk 2025-01-22 02:12:17 +09:00
parent ff6dc12cae
commit 35f7bdbaf5
No known key found for this signature in database
2 changed files with 19 additions and 14 deletions

View file

@ -30,7 +30,7 @@ impl Command {
parse_flags_before = idx; parse_flags_before = idx;
was_parameter = true; was_parameter = true;
} }
Token::Empty | Token::Value(_) => { Token::Empty | Token::Value { .. } => {
if was_parameter { if was_parameter {
break; break;
} }

View file

@ -1,7 +1,4 @@
use std::{ use std::fmt::{Debug, Display};
fmt::{Debug, Display},
ops::Not,
};
use smol_str::SmolStr; use smol_str::SmolStr;
@ -14,7 +11,10 @@ pub enum Token {
Empty, Empty,
/// A bot-defined command / subcommand (usually) (eg. "member" in `pk;member MyName`) /// A bot-defined command / subcommand (usually) (eg. "member" in `pk;member MyName`)
Value(Vec<SmolStr>), Value {
name: SmolStr,
aliases: Vec<SmolStr>,
},
/// A parameter that must be provided a value /// A parameter that must be provided a value
Parameter(Parameter), Parameter(Parameter),
@ -76,7 +76,7 @@ impl Token {
name: param.name().into(), name: param.name().into(),
})), })),
// everything else doesnt match if no input anyway // everything else doesnt match if no input anyway
Self::Value(_) => None, Self::Value { .. } => None,
// don't add a _ match here! // don't add a _ match here!
}; };
} }
@ -86,8 +86,7 @@ impl Token {
// try actually matching stuff // try actually matching stuff
match self { match self {
Self::Empty => None, Self::Empty => None,
Self::Value(values) => values Self::Value { name, aliases } => (aliases.iter().chain(std::iter::once(name)))
.iter()
.any(|v| v.eq(input)) .any(|v| v.eq(input))
.then(|| TokenMatchValue::new_match(input)) .then(|| TokenMatchValue::new_match(input))
.unwrap_or(None), .unwrap_or(None),
@ -106,22 +105,28 @@ impl Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Token::Empty => write!(f, ""), Token::Empty => write!(f, ""),
Token::Value(vec) if vec.is_empty().not() => write!(f, "{}", vec.first().unwrap()), Token::Value { name, .. } => write!(f, "{name}"),
Token::Value(_) => Ok(()), // if value token has no values (lol), don't print anything
Token::Parameter(param) => param.kind().format(f, param.name()), Token::Parameter(param) => param.kind().format(f, param.name()),
} }
} }
} }
impl From<&str> for Token { impl From<&str> for Token {
fn from(value: &str) -> Self { fn from(name: &str) -> Self {
Token::Value(vec![SmolStr::new(value)]) Token::Value {
name: SmolStr::new(name),
aliases: Vec::new(),
}
} }
} }
impl<const L: usize> From<[&str; L]> for Token { impl<const L: usize> From<[&str; L]> for Token {
fn from(value: [&str; L]) -> Self { fn from(value: [&str; L]) -> Self {
Token::Value(value.into_iter().map(SmolStr::from).collect::<Vec<_>>()) assert!(value.len() > 0, "can't create a Token::Value from nothing");
Token::Value {
name: value[0].into(),
aliases: value.into_iter().skip(1).map(SmolStr::new).collect::<Vec<_>>(),
}
} }
} }