mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-14 09:40:10 +00:00
refactor(commands): make tree type properly hygienic
This commit is contained in:
parent
3120e62dda
commit
ee45fca6ab
2 changed files with 21 additions and 10 deletions
|
|
@ -63,12 +63,9 @@ pub fn parse_command(input: String) -> CommandResult {
|
||||||
let mut flags: HashMap<String, Option<String>> = HashMap::new();
|
let mut flags: HashMap<String, Option<String>> = HashMap::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
println!("possible: {:?}", local_tree.branches.keys());
|
let possible_tokens = local_tree.possible_tokens().cloned().collect::<Vec<_>>();
|
||||||
let next = next_token(
|
println!("possible: {:?}", possible_tokens);
|
||||||
local_tree.branches.keys().cloned().collect(),
|
let next = next_token(possible_tokens.clone(), input.clone(), current_pos);
|
||||||
input.clone(),
|
|
||||||
current_pos,
|
|
||||||
);
|
|
||||||
println!("next: {:?}", next);
|
println!("next: {:?}", next);
|
||||||
match next {
|
match next {
|
||||||
Ok((found_token, arg, new_pos)) => {
|
Ok((found_token, arg, new_pos)) => {
|
||||||
|
|
@ -87,14 +84,14 @@ pub fn parse_command(input: String) -> CommandResult {
|
||||||
args.push(arg.raw.to_string());
|
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();
|
local_tree = next_tree.clone();
|
||||||
} else {
|
} else {
|
||||||
panic!("found token could not match tree, at {input}");
|
panic!("found token could not match tree, at {input}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(None) => {
|
Err(None) => {
|
||||||
if let Some(command_ref) = local_tree.current_command_key {
|
if let Some(command_ref) = local_tree.callback() {
|
||||||
println!("{command_ref} {params:?}");
|
println!("{command_ref} {params:?}");
|
||||||
return CommandResult::Ok {
|
return CommandResult::Ok {
|
||||||
command: ParsedCommand {
|
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 last token is a common incorrect unquote (multi-member names etc)
|
||||||
// todo: check if this is a system name in pk;s command
|
// todo: check if this is a system name in pk;s command
|
||||||
return CommandResult::Err {
|
return CommandResult::Err {
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@ use crate::{commands::Command, Token};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TreeBranch {
|
pub struct TreeBranch {
|
||||||
pub current_command_key: Option<SmolStr>,
|
current_command_key: Option<SmolStr>,
|
||||||
pub branches: OrderMap<Token, TreeBranch>,
|
branches: OrderMap<Token, TreeBranch>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeBranch {
|
impl TreeBranch {
|
||||||
|
|
@ -36,4 +36,16 @@ impl TreeBranch {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn callback(&self) -> Option<SmolStr> {
|
||||||
|
self.current_command_key.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn possible_tokens(&self) -> impl Iterator<Item = &Token> {
|
||||||
|
self.branches.keys()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_branch(&self, token: &Token) -> Option<&TreeBranch> {
|
||||||
|
self.branches.get(token)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue