From ee45fca6ab35b47f494304c970738c95a8f5d6b2 Mon Sep 17 00:00:00 2001 From: dusk Date: Sat, 11 Jan 2025 20:25:41 +0900 Subject: [PATCH] refactor(commands): make tree type properly hygienic --- crates/commands/src/lib.rs | 15 +++++++-------- crates/commands/src/tree.rs | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/commands/src/lib.rs b/crates/commands/src/lib.rs index f7739cd2..24228f9b 100644 --- a/crates/commands/src/lib.rs +++ b/crates/commands/src/lib.rs @@ -63,12 +63,9 @@ pub fn parse_command(input: String) -> CommandResult { let mut flags: HashMap> = HashMap::new(); loop { - println!("possible: {:?}", local_tree.branches.keys()); - let next = next_token( - local_tree.branches.keys().cloned().collect(), - input.clone(), - current_pos, - ); + let possible_tokens = local_tree.possible_tokens().cloned().collect::>(); + println!("possible: {:?}", possible_tokens); + let next = next_token(possible_tokens.clone(), input.clone(), current_pos); println!("next: {:?}", next); match next { Ok((found_token, arg, new_pos)) => { @@ -87,14 +84,14 @@ pub fn parse_command(input: String) -> CommandResult { args.push(arg.raw.to_string()); } - if let Some(next_tree) = local_tree.branches.get(&found_token) { + if let Some(next_tree) = local_tree.get_branch(&found_token) { local_tree = next_tree.clone(); } else { panic!("found token could not match tree, at {input}"); } } Err(None) => { - if let Some(command_ref) = local_tree.current_command_key { + if let Some(command_ref) = local_tree.callback() { println!("{command_ref} {params:?}"); return CommandResult::Ok { command: ParsedCommand { @@ -106,6 +103,8 @@ pub fn parse_command(input: String) -> CommandResult { }; } + println!("{possible_tokens:?}"); + // todo: check if last token is a common incorrect unquote (multi-member names etc) // todo: check if this is a system name in pk;s command return CommandResult::Err { diff --git a/crates/commands/src/tree.rs b/crates/commands/src/tree.rs index 27ee0791..911c21f0 100644 --- a/crates/commands/src/tree.rs +++ b/crates/commands/src/tree.rs @@ -5,8 +5,8 @@ use crate::{commands::Command, Token}; #[derive(Debug, Clone)] pub struct TreeBranch { - pub current_command_key: Option, - pub branches: OrderMap, + current_command_key: Option, + branches: OrderMap, } impl TreeBranch { @@ -36,4 +36,16 @@ impl TreeBranch { }, ); } + + pub fn callback(&self) -> Option { + self.current_command_key.clone() + } + + pub fn possible_tokens(&self) -> impl Iterator { + self.branches.keys() + } + + pub fn get_branch(&self, token: &Token) -> Option<&TreeBranch> { + self.branches.get(token) + } }