mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-12 08:40:11 +00:00
feat: implement proper ("static") parameters handling command parser -> bot
feat: handle few more commands bot side fix(commands): handle missing parameters and return error refactor(commands): use ordermap instead of relying on a sort function to sort tokens
This commit is contained in:
parent
1a781014bd
commit
eec9f64026
16 changed files with 358 additions and 502 deletions
|
|
@ -1,14 +1,15 @@
|
|||
use ordermap::OrderMap;
|
||||
use smol_str::SmolStr;
|
||||
|
||||
use crate::{commands::Command, Token};
|
||||
use std::{cmp::Ordering, collections::HashMap};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TreeBranch {
|
||||
pub current_command_key: Option<SmolStr>,
|
||||
/// branches.keys(), but sorted by specificity
|
||||
pub possible_tokens: Vec<Token>,
|
||||
pub branches: HashMap<Token, TreeBranch>,
|
||||
pub branches: OrderMap<Token, TreeBranch>,
|
||||
}
|
||||
|
||||
impl TreeBranch {
|
||||
|
|
@ -20,7 +21,7 @@ impl TreeBranch {
|
|||
current_branch = current_branch.branches.entry(token).or_insert(TreeBranch {
|
||||
current_command_key: None,
|
||||
possible_tokens: vec![],
|
||||
branches: HashMap::new(),
|
||||
branches: OrderMap::new(),
|
||||
})
|
||||
}
|
||||
// when we're out of tokens, add an Empty branch with the callback and no sub-branches
|
||||
|
|
@ -29,31 +30,8 @@ impl TreeBranch {
|
|||
TreeBranch {
|
||||
current_command_key: Some(command.cb),
|
||||
possible_tokens: vec![],
|
||||
branches: HashMap::new(),
|
||||
branches: OrderMap::new(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn sort_tokens(&mut self) {
|
||||
for branch in self.branches.values_mut() {
|
||||
branch.sort_tokens();
|
||||
}
|
||||
|
||||
// put Value tokens at the end
|
||||
// i forget exactly how this works
|
||||
// todo!: document this before PR mergs
|
||||
self.possible_tokens = self
|
||||
.branches
|
||||
.keys()
|
||||
.into_iter()
|
||||
.map(|v| v.clone())
|
||||
.collect();
|
||||
self.possible_tokens.sort_by(|v, _| {
|
||||
if matches!(v, Token::Value(_)) {
|
||||
Ordering::Greater
|
||||
} else {
|
||||
Ordering::Less
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue