mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
refactor(command_parser): remove the Empty token, we don't need it
This commit is contained in:
parent
071db3d6d6
commit
92276a720e
4 changed files with 7 additions and 21 deletions
|
|
@ -30,7 +30,7 @@ impl Command {
|
|||
parse_flags_before = idx;
|
||||
was_parameter = true;
|
||||
}
|
||||
Token::Empty | Token::Value { .. } => {
|
||||
Token::Value { .. } => {
|
||||
if was_parameter {
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Command> {
|
||||
|
|
@ -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)));
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue