From c6db96115eb148555ae8caf5b6800969c18f9f74 Mon Sep 17 00:00:00 2001 From: dusk Date: Mon, 20 Jan 2025 22:50:45 +0900 Subject: [PATCH] refactor(commands): remove help text from command macro and use method to set it --- crates/commands/src/commands.rs | 20 +++++------ crates/commands/src/commands/config.rs | 26 ++++++-------- crates/commands/src/commands/fun.rs | 4 +-- crates/commands/src/commands/help.rs | 6 ++-- crates/commands/src/commands/member.rs | 47 ++++++++------------------ crates/commands/src/commands/system.rs | 15 +++----- 6 files changed, 44 insertions(+), 74 deletions(-) diff --git a/crates/commands/src/commands.rs b/crates/commands/src/commands.rs index c331a3fe..8626a52a 100644 --- a/crates/commands/src/commands.rs +++ b/crates/commands/src/commands.rs @@ -36,11 +36,7 @@ pub struct Command { } impl Command { - pub fn new( - tokens: impl IntoIterator, - help: impl Into, - cb: impl Into, - ) -> Self { + pub fn new(tokens: impl IntoIterator, cb: impl Into) -> Self { let tokens = tokens.into_iter().collect::>(); assert!(tokens.len() > 0); let mut parse_flags_before = tokens.len(); @@ -60,7 +56,7 @@ impl Command { } Self { flags: Vec::new(), - help: help.into(), + help: SmolStr::new_static(""), cb: cb.into(), show_in_suggestions: true, parse_flags_before, @@ -68,6 +64,11 @@ impl Command { } } + pub fn help(mut self, v: impl Into) -> Self { + self.help = v.into(); + self + } + pub fn show_in_suggestions(mut self, v: bool) -> Self { self.show_in_suggestions = v; self @@ -111,16 +112,15 @@ impl Display for Command { // (and something like &dyn Trait would require everything to be referenced which doesnt look nice anyway) #[macro_export] macro_rules! command { - ([$($v:expr),+], $cb:expr, $help:expr) => { - $crate::commands::Command::new([$(Token::from($v)),*], $help, $cb) + ([$($v:expr),+], $cb:expr$(,)*) => { + $crate::commands::Command::new([$(Token::from($v)),*], $cb) }; } -pub fn all() -> Vec { +pub fn all() -> impl Iterator { (help::cmds()) .chain(system::cmds()) .chain(member::cmds()) .chain(config::cmds()) .chain(fun::cmds()) - .collect() } diff --git a/crates/commands/src/commands/config.rs b/crates/commands/src/commands/config.rs index 47c46cbc..b2f5b80d 100644 --- a/crates/commands/src/commands/config.rs +++ b/crates/commands/src/commands/config.rs @@ -5,21 +5,15 @@ pub fn cmds() -> impl Iterator { let autoproxy = ["autoproxy", "ap"]; [ - command!( - [cfg, autoproxy, ["account", "ac"]], - "cfg_ap_account_show", - "Shows autoproxy status for the account" - ), + command!([cfg, autoproxy, ["account", "ac"]], "cfg_ap_account_show") + .help("Shows autoproxy status for the account"), command!( [cfg, autoproxy, ["account", "ac"], Toggle], - "cfg_ap_account_update", - "Toggles autoproxy for the account" - ), - command!( - [cfg, autoproxy, ["timeout", "tm"]], - "cfg_ap_timeout_show", - "Shows the autoproxy timeout" - ), + "cfg_ap_account_update" + ) + .help("Toggles autoproxy for the account"), + command!([cfg, autoproxy, ["timeout", "tm"]], "cfg_ap_timeout_show") + .help("Shows the autoproxy timeout"), command!( [ cfg, @@ -27,9 +21,9 @@ pub fn cmds() -> impl Iterator { ["timeout", "tm"], any!(Disable, Reset, ("timeout", OpaqueString::SINGLE)) // todo: we should parse duration / time values ], - "cfg_ap_timeout_update", - "Sets the autoproxy timeout" - ), + "cfg_ap_timeout_update" + ) + .help("Sets the autoproxy timeout"), ] .into_iter() } diff --git a/crates/commands/src/commands/fun.rs b/crates/commands/src/commands/fun.rs index a8bddf45..720eadc0 100644 --- a/crates/commands/src/commands/fun.rs +++ b/crates/commands/src/commands/fun.rs @@ -2,8 +2,8 @@ use super::*; pub fn cmds() -> impl Iterator { [ - command!(["thunder"], "fun_thunder", "fun thunder"), - command!(["meow"], "fun_meow", "fun meow"), + command!(["thunder"], "fun_thunder"), + command!(["meow"], "fun_meow"), ] .into_iter() } diff --git a/crates/commands/src/commands/help.rs b/crates/commands/src/commands/help.rs index f663ee68..411b3565 100644 --- a/crates/commands/src/commands/help.rs +++ b/crates/commands/src/commands/help.rs @@ -3,9 +3,9 @@ use super::*; pub fn cmds() -> impl Iterator { let help = ["help", "h"]; [ - command!([help], "help", "Shows the help command"), - command!([help, "commands"], "help_commands", "help commands"), - command!([help, "proxy"], "help_proxy", "help proxy"), + command!([help], "help").help("Shows the help command"), + command!([help, "commands"], "help_commands").help("help commands"), + command!([help, "proxy"], "help_proxy").help("help proxy"), ] .into_iter() } diff --git a/crates/commands/src/commands/member.rs b/crates/commands/src/commands/member.rs index 6f33e615..5346bd53 100644 --- a/crates/commands/src/commands/member.rs +++ b/crates/commands/src/commands/member.rs @@ -7,22 +7,13 @@ pub fn cmds() -> impl Iterator { let new = ["new", "n"]; [ - command!( - [member, new, ("name", OpaqueString::SINGLE)], - "member_new", - "Creates a new system member" - ), - command!( - [member, MemberRef], - "member_show", - "Shows information about a member" - ) - .value_flag("pt", Disable), - command!( - [member, MemberRef, description], - "member_desc_show", - "Shows a member's description" - ), + command!([member, new, ("name", OpaqueString::SINGLE)], "member_new") + .help("Creates a new system member"), + command!([member, MemberRef], "member_show") + .help("Shows information about a member") + .value_flag("pt", Disable), + command!([member, MemberRef, description], "member_desc_show") + .help("Shows a member's description"), command!( [ member, @@ -30,14 +21,11 @@ pub fn cmds() -> impl Iterator { description, ("description", OpaqueString::REMAINDER) ], - "member_desc_update", - "Changes a member's description" - ), - command!( - [member, MemberRef, privacy], - "member_privacy_show", - "Displays a member's current privacy settings" - ), + "member_desc_update" + ) + .help("Changes a member's description"), + command!([member, MemberRef, privacy], "member_privacy_show") + .help("Displays a member's current privacy settings"), command!( [ member, @@ -46,15 +34,10 @@ pub fn cmds() -> impl Iterator { MemberPrivacyTarget, ("new_privacy_level", PrivacyLevel) ], - "member_privacy_update", - "Changes a member's privacy settings" - ), - command!( - [member, MemberRef, "soulscream"], - "member_soulscream", - "todo" + "member_privacy_update" ) - .show_in_suggestions(false), + .help("Changes a member's privacy settings"), + command!([member, MemberRef, "soulscream"], "member_soulscream").show_in_suggestions(false), ] .into_iter() } diff --git a/crates/commands/src/commands/system.rs b/crates/commands/src/commands/system.rs index 47eb6591..fd2148a3 100644 --- a/crates/commands/src/commands/system.rs +++ b/crates/commands/src/commands/system.rs @@ -5,17 +5,10 @@ pub fn cmds() -> impl Iterator { let new = ["new", "n"]; [ - command!( - [system], - "system_show", - "Shows information about your system" - ), - command!([system, new], "system_new", "Creates a new system"), - command!( - [system, new, ("name", OpaqueString::SINGLE)], - "system_new", - "Creates a new system" - ), + command!([system], "system_show").help("Shows information about your system"), + command!([system, new], "system_new").help("Creates a new system"), + command!([system, new, ("name", OpaqueString::SINGLE)], "system_new") + .help("Creates a new system"), ] .into_iter() }