refactor(commands): also use smolstr for commands themselves

This commit is contained in:
dusk 2025-01-04 07:43:55 +09:00
parent af523a4c23
commit b9867e7ea3
No known key found for this signature in database
3 changed files with 12 additions and 8 deletions

View file

@ -18,26 +18,28 @@ pub mod server_config;
pub mod switch; pub mod switch;
pub mod system; pub mod system;
use smol_str::SmolStr;
use crate::{command, token::Token}; use crate::{command, token::Token};
#[derive(Clone)] #[derive(Clone)]
pub struct Command { pub struct Command {
// TODO: fix hygiene // TODO: fix hygiene
pub tokens: Vec<Token>, pub tokens: Vec<Token>,
pub help: String, pub help: SmolStr,
pub cb: String, pub cb: SmolStr,
} }
impl Command { impl Command {
pub fn new( pub fn new(
tokens: impl IntoIterator<Item = Token>, tokens: impl IntoIterator<Item = Token>,
help: impl ToString, help: impl Into<SmolStr>,
cb: impl ToString, cb: impl Into<SmolStr>,
) -> Self { ) -> Self {
Self { Self {
tokens: tokens.into_iter().collect(), tokens: tokens.into_iter().collect(),
help: help.to_string(), help: help.into(),
cb: cb.to_string(), cb: cb.into(),
} }
} }
} }

View file

@ -84,7 +84,7 @@ fn parse_command(input: String) -> CommandResult {
if let Some(command_ref) = local_tree.current_command_key { if let Some(command_ref) = local_tree.current_command_key {
return CommandResult::Ok { return CommandResult::Ok {
command: ParsedCommand { command: ParsedCommand {
command_ref: command_ref.to_owned(), command_ref: command_ref.into(),
args, args,
flags, flags,
}, },

View file

@ -1,9 +1,11 @@
use smol_str::SmolStr;
use crate::{commands::Command, Token}; use crate::{commands::Command, Token};
use std::{cmp::Ordering, collections::HashMap}; use std::{cmp::Ordering, collections::HashMap};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TreeBranch { pub struct TreeBranch {
pub current_command_key: Option<String>, pub current_command_key: Option<SmolStr>,
/// branches.keys(), but sorted by specificity /// branches.keys(), but sorted by specificity
pub possible_tokens: Vec<Token>, pub possible_tokens: Vec<Token>,
pub branches: HashMap<Token, TreeBranch>, pub branches: HashMap<Token, TreeBranch>,