2025-01-05 13:00:06 +09:00
|
|
|
use ordermap::OrderMap;
|
2025-01-04 07:43:55 +09:00
|
|
|
use smol_str::SmolStr;
|
|
|
|
|
|
2025-01-04 07:35:04 +09:00
|
|
|
use crate::{commands::Command, Token};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct TreeBranch {
|
2025-01-04 07:43:55 +09:00
|
|
|
pub current_command_key: Option<SmolStr>,
|
2025-01-05 13:00:06 +09:00
|
|
|
pub branches: OrderMap<Token, TreeBranch>,
|
2025-01-04 07:35:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TreeBranch {
|
2025-01-11 19:51:45 +09:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
current_command_key: None,
|
|
|
|
|
branches: OrderMap::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 07:35:04 +09:00
|
|
|
pub fn register_command(&mut self, command: Command) {
|
|
|
|
|
let mut current_branch = self;
|
|
|
|
|
// iterate over tokens in command
|
|
|
|
|
for token in command.tokens {
|
|
|
|
|
// recursively get or create a sub-branch for each token
|
|
|
|
|
current_branch = current_branch.branches.entry(token).or_insert(TreeBranch {
|
|
|
|
|
current_command_key: None,
|
2025-01-05 13:00:06 +09:00
|
|
|
branches: OrderMap::new(),
|
2025-01-04 07:35:04 +09:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// when we're out of tokens, add an Empty branch with the callback and no sub-branches
|
|
|
|
|
current_branch.branches.insert(
|
|
|
|
|
Token::Empty,
|
|
|
|
|
TreeBranch {
|
|
|
|
|
current_command_key: Some(command.cb),
|
2025-01-05 13:00:06 +09:00
|
|
|
branches: OrderMap::new(),
|
2025-01-04 07:35:04 +09:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|