From ff121ecc5161c70dd52615e30c130488f31f7fe8 Mon Sep 17 00:00:00 2001 From: dusk Date: Sun, 5 Jan 2025 00:59:59 +0900 Subject: [PATCH] refactor(commands): ToToken trait for easier conversion into tokens --- crates/commands/src/commands.rs | 8 +++++-- crates/commands/src/commands/fun.rs | 2 +- crates/commands/src/commands/help.rs | 18 +++++++++++---- crates/commands/src/commands/member.rs | 8 +++---- crates/commands/src/commands/system.rs | 4 ++-- crates/commands/src/token.rs | 32 ++++++++++++++++++-------- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/crates/commands/src/commands.rs b/crates/commands/src/commands.rs index 789240ab..b904bb59 100644 --- a/crates/commands/src/commands.rs +++ b/crates/commands/src/commands.rs @@ -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 { (help::cmds()) .chain(system::cmds()) .chain(member::cmds()) + .chain(fun::cmds()) .collect() } diff --git a/crates/commands/src/commands/fun.rs b/crates/commands/src/commands/fun.rs index e14da01b..a3ba0d9e 100644 --- a/crates/commands/src/commands/fun.rs +++ b/crates/commands/src/commands/fun.rs @@ -2,7 +2,7 @@ use super::*; pub fn cmds() -> impl Iterator { [command!( - [Token::cmd("thunder")], + ["thunder"], "fun_thunder", "Shows the help command" )] diff --git a/crates/commands/src/commands/help.rs b/crates/commands/src/commands/help.rs index e91d13f3..269d87a9 100644 --- a/crates/commands/src/commands/help.rs +++ b/crates/commands/src/commands/help.rs @@ -1,10 +1,18 @@ use super::*; pub fn cmds() -> impl Iterator { - [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() } diff --git a/crates/commands/src/commands/member.rs b/crates/commands/src/commands/member.rs index d92e59cc..ae5c1e85 100644 --- a/crates/commands/src/commands/member.rs +++ b/crates/commands/src/commands/member.rs @@ -3,10 +3,10 @@ use super::*; pub fn cmds() -> impl Iterator { 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!( diff --git a/crates/commands/src/commands/system.rs b/crates/commands/src/commands/system.rs index 9d1bdf1f..bbc449ed 100644 --- a/crates/commands/src/commands/system.rs +++ b/crates/commands/src/commands/system.rs @@ -3,8 +3,8 @@ use super::*; pub fn cmds() -> impl Iterator { 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!( diff --git a/crates/commands/src/token.rs b/crates/commands/src/token.rs index 60df6dfc..3b59fe38 100644 --- a/crates/commands/src/token.rs +++ b/crates/commands/src/token.rs @@ -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) -> Self { - Self::Value(vec![value.into()]) - } - - pub fn cmd_with_alias(value: impl IntoIterator>) -> Self { - Self::Value(value.into_iter().map(Into::into).collect()) - } - pub fn try_match(&self, input: Option) -> 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()) + } +} \ No newline at end of file