From 92276a720e92a51ea657b96df24fc4a411a5d497 Mon Sep 17 00:00:00 2001 From: dusk Date: Fri, 24 Jan 2025 04:13:06 +0900 Subject: [PATCH] refactor(command_parser): remove the Empty token, we don't need it --- crates/command_parser/src/command.rs | 2 +- crates/command_parser/src/token.rs | 8 -------- crates/command_parser/src/tree.rs | 12 +++--------- crates/commands/src/main.rs | 6 +++--- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/crates/command_parser/src/command.rs b/crates/command_parser/src/command.rs index b7fd65ce..6810a82d 100644 --- a/crates/command_parser/src/command.rs +++ b/crates/command_parser/src/command.rs @@ -30,7 +30,7 @@ impl Command { parse_flags_before = idx; was_parameter = true; } - Token::Empty | Token::Value { .. } => { + Token::Value { .. } => { if was_parameter { break; } diff --git a/crates/command_parser/src/token.rs b/crates/command_parser/src/token.rs index 79300279..b737a8cd 100644 --- a/crates/command_parser/src/token.rs +++ b/crates/command_parser/src/token.rs @@ -6,10 +6,6 @@ use crate::parameter::{Parameter, ParameterKind, ParameterValue}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] 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`) Value { name: SmolStr, @@ -69,8 +65,6 @@ impl Token { None => { // short circuit on: return match self { - // empty token - Self::Empty => Some(Ok(None)), // missing paramaters Self::Parameter(param) => Some(Err(TokenMatchError::MissingParameter { name: param.name().into(), @@ -85,7 +79,6 @@ impl Token { // try actually matching stuff match self { - Self::Empty => None, Self::Value { name, aliases } => (aliases.iter().chain(std::iter::once(name))) .any(|v| v.eq(input)) .then(|| TokenMatchValue::new_match(input)) @@ -104,7 +97,6 @@ impl Token { impl Display for Token { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Empty => write!(f, ""), Self::Value { name, .. } => write!(f, "{name}"), Self::Parameter(param) => param.kind().format(f, param.name()), } diff --git a/crates/command_parser/src/tree.rs b/crates/command_parser/src/tree.rs index 53057a69..9b1466a3 100644 --- a/crates/command_parser/src/tree.rs +++ b/crates/command_parser/src/tree.rs @@ -28,14 +28,8 @@ impl TreeBranch { .entry(token) .or_insert_with(TreeBranch::default); } - // when we're out of tokens, add an Empty branch with the callback and no sub-branches - current_branch.branches.insert( - Token::Empty, - TreeBranch { - branches: OrderMap::new(), - current_command: Some(command), - }, - ); + // when we're out of tokens add the command to the last branch + current_branch.current_command = Some(command); } pub fn command(&self) -> Option { @@ -61,7 +55,7 @@ impl TreeBranch { for branch in self.branches.values() { if let Some(command) = branch.current_command.as_ref() { 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; } commands = box_iter(commands.chain(branch.possible_commands(max_depth - 1))); diff --git a/crates/commands/src/main.rs b/crates/commands/src/main.rs index 2c863447..376d2469 100644 --- a/crates/commands/src/main.rs +++ b/crates/commands/src/main.rs @@ -1,6 +1,6 @@ #![feature(iter_intersperse)] -use command_parser::{token::Token, Tree}; +use command_parser::Tree; use commands::COMMAND_TREE; fn main() { @@ -32,8 +32,8 @@ fn print_tree(tree: &Tree, depth: usize) { print!("-"); } print!("> {token:?}"); - if matches!(token, Token::Empty) { - println!(": {}", branch.command().unwrap().cb) + if let Some(command) = branch.command() { + println!(": {}", command.cb) } else { print_tree(branch, depth + 1) }