refactor(command_parser): remove the Empty token, we don't need it

This commit is contained in:
dusk 2025-01-24 04:13:06 +09:00
parent 071db3d6d6
commit 92276a720e
No known key found for this signature in database
4 changed files with 7 additions and 21 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::Value { .. } => {
if was_parameter { if was_parameter {
break; break;
} }

View file

@ -6,10 +6,6 @@ use crate::parameter::{Parameter, ParameterKind, ParameterValue};
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Token { pub enum Token {
/// Token used to represent a finished command (i.e. no more parameters required)
// todo: this is likely not the right way to represent this
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 { Value {
name: SmolStr, name: SmolStr,
@ -69,8 +65,6 @@ impl Token {
None => { None => {
// short circuit on: // short circuit on:
return match self { return match self {
// empty token
Self::Empty => Some(Ok(None)),
// missing paramaters // missing paramaters
Self::Parameter(param) => Some(Err(TokenMatchError::MissingParameter { Self::Parameter(param) => Some(Err(TokenMatchError::MissingParameter {
name: param.name().into(), name: param.name().into(),
@ -85,7 +79,6 @@ impl Token {
// try actually matching stuff // try actually matching stuff
match self { match self {
Self::Empty => None,
Self::Value { name, aliases } => (aliases.iter().chain(std::iter::once(name))) Self::Value { name, aliases } => (aliases.iter().chain(std::iter::once(name)))
.any(|v| v.eq(input)) .any(|v| v.eq(input))
.then(|| TokenMatchValue::new_match(input)) .then(|| TokenMatchValue::new_match(input))
@ -104,7 +97,6 @@ impl Token {
impl Display for Token { 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 {
Self::Empty => write!(f, ""),
Self::Value { name, .. } => write!(f, "{name}"), Self::Value { name, .. } => write!(f, "{name}"),
Self::Parameter(param) => param.kind().format(f, param.name()), Self::Parameter(param) => param.kind().format(f, param.name()),
} }

View file

@ -28,14 +28,8 @@ impl TreeBranch {
.entry(token) .entry(token)
.or_insert_with(TreeBranch::default); .or_insert_with(TreeBranch::default);
} }
// when we're out of tokens, add an Empty branch with the callback and no sub-branches // when we're out of tokens add the command to the last branch
current_branch.branches.insert( current_branch.current_command = Some(command);
Token::Empty,
TreeBranch {
branches: OrderMap::new(),
current_command: Some(command),
},
);
} }
pub fn command(&self) -> Option<Command> { pub fn command(&self) -> Option<Command> {
@ -61,7 +55,7 @@ impl TreeBranch {
for branch in self.branches.values() { for branch in self.branches.values() {
if let Some(command) = branch.current_command.as_ref() { if let Some(command) = branch.current_command.as_ref() {
commands = box_iter(commands.chain(std::iter::once(command))); commands = box_iter(commands.chain(std::iter::once(command)));
// we dont need to look further if we found a command (only Empty tokens have commands) // we dont need to look further if we found a command
continue; continue;
} }
commands = box_iter(commands.chain(branch.possible_commands(max_depth - 1))); commands = box_iter(commands.chain(branch.possible_commands(max_depth - 1)));

View file

@ -1,6 +1,6 @@
#![feature(iter_intersperse)] #![feature(iter_intersperse)]
use command_parser::{token::Token, Tree}; use command_parser::Tree;
use commands::COMMAND_TREE; use commands::COMMAND_TREE;
fn main() { fn main() {
@ -32,8 +32,8 @@ fn print_tree(tree: &Tree, depth: usize) {
print!("-"); print!("-");
} }
print!("> {token:?}"); print!("> {token:?}");
if matches!(token, Token::Empty) { if let Some(command) = branch.command() {
println!(": {}", branch.command().unwrap().cb) println!(": {}", command.cb)
} else { } else {
print_tree(branch, depth + 1) print_tree(branch, depth + 1)
} }