refactor(commands): ToToken trait for easier conversion into tokens

This commit is contained in:
dusk 2025-01-05 00:59:59 +09:00
parent e70d69e45c
commit ff121ecc51
No known key found for this signature in database
6 changed files with 49 additions and 23 deletions

View file

@ -20,7 +20,10 @@ pub mod system;
use smol_str::SmolStr;
use crate::{command, token::Token};
use crate::{
command,
token::{ToToken, Token},
};
#[derive(Clone, Debug)]
pub struct Command {
@ -47,7 +50,7 @@ impl Command {
#[macro_export]
macro_rules! command {
([$($v:expr),+], $cb:expr, $help:expr) => {
$crate::commands::Command::new([$($v.clone()),*], $help, $cb)
$crate::commands::Command::new([$($v.to_token()),*], $help, $cb)
};
}
@ -55,5 +58,6 @@ pub fn all() -> Vec<Command> {
(help::cmds())
.chain(system::cmds())
.chain(member::cmds())
.chain(fun::cmds())
.collect()
}

View file

@ -2,7 +2,7 @@ use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
[command!(
[Token::cmd("thunder")],
["thunder"],
"fun_thunder",
"Shows the help command"
)]

View file

@ -1,10 +1,18 @@
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
[command!(
[Token::cmd("help")],
"help",
"Shows the help command"
)]
let help = ["help", "h"];
[
command!(
[help],
"help",
"Shows the help command"
),
command!(
[help, "commands"],
"help_commands",
"Commands"
),
]
.into_iter()
}

View file

@ -3,10 +3,10 @@ use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
use Token::*;
let member = Token::cmd_with_alias(["member", "m"]);
let description = Token::cmd_with_alias(["description", "desc"]);
let privacy = Token::cmd_with_alias(["privacy", "priv"]);
let new = Token::cmd_with_alias(["new", "n"]);
let member = ["member", "m"];
let description = ["description", "desc"];
let privacy = ["privacy", "priv"];
let new = ["new", "n"];
[
command!(

View file

@ -3,8 +3,8 @@ use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
use Token::*;
let system = Token::cmd_with_alias(["system", "s"]);
let new = Token::cmd_with_alias(["new", "n"]);
let system = ["system", "s"];
let new = ["new", "n"];
[
command!(

View file

@ -1,4 +1,4 @@
use smol_str::SmolStr;
use smol_str::{SmolStr, ToSmolStr};
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum Token {
@ -41,14 +41,6 @@ lazy_static::lazy_static!(
);
impl Token {
pub fn cmd(value: impl Into<SmolStr>) -> Self {
Self::Value(vec![value.into()])
}
pub fn cmd_with_alias(value: impl IntoIterator<Item = impl Into<SmolStr>>) -> Self {
Self::Value(value.into_iter().map(Into::into).collect())
}
pub fn try_match(&self, input: Option<SmolStr>) -> TokenMatchResult {
// short circuit on empty things
if matches!(self, Self::Empty) && input.is_none() {
@ -94,3 +86,25 @@ impl Token {
return TokenMatchResult::NoMatch;
}
}
pub trait ToToken {
fn to_token(&self) -> Token;
}
impl ToToken for Token {
fn to_token(&self) -> Token {
self.clone()
}
}
impl ToToken for &str {
fn to_token(&self) -> Token {
Token::Value(vec![self.to_smolstr()])
}
}
impl ToToken for [&str] {
fn to_token(&self) -> Token {
Token::Value(self.into_iter().map(|s| s.to_smolstr()).collect())
}
}