From d8748b1efcda51c2dba768b45560ba421ab4cded Mon Sep 17 00:00:00 2001 From: dusk Date: Sun, 23 Nov 2025 10:28:46 +0000 Subject: [PATCH] remove OpaqueStringRemainder and use Remainder like intended --- crates/command_definitions/src/admin.rs | 6 ++-- crates/command_definitions/src/group.rs | 10 +++--- .../command_definitions/src/import_export.rs | 2 +- crates/command_definitions/src/member.rs | 14 ++++---- crates/command_definitions/src/message.rs | 7 ++-- crates/command_definitions/src/switch.rs | 2 +- crates/command_definitions/src/system.rs | 16 ++++----- crates/command_parser/src/parameter.rs | 33 ++++++++----------- crates/commands/src/lib.rs | 2 +- crates/commands/src/write_cs_glue.rs | 4 +-- 10 files changed, 46 insertions(+), 50 deletions(-) diff --git a/crates/command_definitions/src/admin.rs b/crates/command_definitions/src/admin.rs index 73a8e892..24608f58 100644 --- a/crates/command_definitions/src/admin.rs +++ b/crates/command_definitions/src/admin.rs @@ -14,7 +14,7 @@ pub fn cmds() -> impl Iterator { .help("Shows an abuse log entry"), command!(abuselog, ("flagdeny", ["fd"]), log_param, Optional(("value", Toggle)) => format!("admin_abuselog_flag_deny_{}", log_param.name())) .help("Sets the deny flag on an abuse log entry"), - command!(abuselog, ("description", ["desc"]), log_param, Optional(("desc", OpaqueStringRemainder)) => format!("admin_abuselog_description_{}", log_param.name())) + command!(abuselog, ("description", ["desc"]), log_param, Optional(Remainder(("desc", OpaqueString))) => format!("admin_abuselog_description_{}", log_param.name())) .flag(CLEAR) .flag(YES) .help("Sets the description of an abuse log entry"), @@ -27,7 +27,7 @@ pub fn cmds() -> impl Iterator { ].into_iter() }; let abuselog_cmds = [ - command!(abuselog, ("create", ["c", "new"]), ("account", UserRef), Optional(("description", OpaqueStringRemainder)) => "admin_abuselog_create") + command!(abuselog, ("create", ["c", "new"]), ("account", UserRef), Optional(Remainder(("description", OpaqueString))) => "admin_abuselog_create") .flag(("deny-boy-usage", ["deny"])) .help("Creates an abuse log entry") ] @@ -66,7 +66,7 @@ pub fn cmds() -> impl Iterator { .help("Recovers a system"), command!(admin, ("systemdelete", ["sd"]), SystemRef => "admin_system_delete") .help("Deletes a system"), - command!(admin, ("sendmessage", ["sendmsg"]), ("account", UserRef), ("content", OpaqueStringRemainder) => "admin_send_message") + command!(admin, ("sendmessage", ["sendmsg"]), ("account", UserRef), Remainder(("content", OpaqueString)) => "admin_send_message") .help("Sends a message to a user"), ] .into_iter() diff --git a/crates/command_definitions/src/group.rs b/crates/command_definitions/src/group.rs index 63d3f7a5..fbf611be 100644 --- a/crates/command_definitions/src/group.rs +++ b/crates/command_definitions/src/group.rs @@ -18,7 +18,7 @@ pub fn cmds() -> impl Iterator { let group_new = tokens!(group, ("new", ["n"])); let group_new_cmd = [ - command!(group_new, ("name", OpaqueStringRemainder) => "group_new") + command!(group_new, Remainder(("name", OpaqueString)) => "group_new") .help("Creates a new group"), ] .into_iter(); @@ -37,7 +37,7 @@ pub fn cmds() -> impl Iterator { command!(group_name, CLEAR => "group_clear_name") .flag(YES) .help("Clears the group's name"), - command!(group_name, ("name", OpaqueStringRemainder) => "group_rename") + command!(group_name, Remainder(("name", OpaqueString)) => "group_rename") .help("Renames the group"), ] .into_iter(); @@ -49,7 +49,7 @@ pub fn cmds() -> impl Iterator { command!(group_display_name, CLEAR => "group_clear_display_name") .flag(YES) .help("Clears the group's display name"), - command!(group_display_name, ("name", OpaqueStringRemainder) => "group_change_display_name") + command!(group_display_name, Remainder(("name", OpaqueString)) => "group_change_display_name") .help("Changes the group's display name"), ] .into_iter(); @@ -67,7 +67,7 @@ pub fn cmds() -> impl Iterator { command!(group_description, CLEAR => "group_clear_description") .flag(YES) .help("Clears the group's description"), - command!(group_description, ("description", OpaqueStringRemainder) => "group_change_description") + command!(group_description, Remainder(("description", OpaqueString)) => "group_change_description") .help("Changes the group's description"), ] .into_iter(); @@ -154,7 +154,7 @@ pub fn cmds() -> impl Iterator { let search = tokens!( ("search", ["find", "query"]), - ("query", OpaqueStringRemainder) + Remainder(("query", OpaqueString)) ); let group_list_members = tokens!(group_target, ("members", ["list", "ls"])); let group_list_members_cmd = [ diff --git a/crates/command_definitions/src/import_export.rs b/crates/command_definitions/src/import_export.rs index beed5e72..7e2e9404 100644 --- a/crates/command_definitions/src/import_export.rs +++ b/crates/command_definitions/src/import_export.rs @@ -2,7 +2,7 @@ use super::*; pub fn cmds() -> impl Iterator { [ - command!("import", Optional(("url", OpaqueStringRemainder)) => "import").flag(YES), + command!("import", Optional(Remainder(("url", OpaqueString))) => "import").flag(YES), command!("export" => "export"), ] .into_iter() diff --git a/crates/command_definitions/src/member.rs b/crates/command_definitions/src/member.rs index f35bb3d8..655384ff 100644 --- a/crates/command_definitions/src/member.rs +++ b/crates/command_definitions/src/member.rs @@ -48,7 +48,7 @@ pub fn cmds() -> impl Iterator { let member_name = tokens!(member_target, name); [ command!(member_name => "member_name_show").help("Shows a member's name"), - command!(member_name, ("name", OpaqueStringRemainder) => "member_name_update") + command!(member_name, Remainder(("name", OpaqueString)) => "member_name_update") .flag(YES) .help("Changes a member's name"), ] @@ -62,7 +62,7 @@ pub fn cmds() -> impl Iterator { command!(member_desc, CLEAR => "member_desc_clear") .flag(YES) .help("Clears a member's description"), - command!(member_desc, ("description", OpaqueStringRemainder) => "member_desc_update") + command!(member_desc, Remainder(("description", OpaqueString)) => "member_desc_update") .help("Changes a member's description"), ] .into_iter() @@ -87,7 +87,7 @@ pub fn cmds() -> impl Iterator { [ command!(member_pronouns => "member_pronouns_show") .help("Shows a member's pronouns"), - command!(member_pronouns, ("pronouns", OpaqueStringRemainder) => "member_pronouns_update") + command!(member_pronouns, Remainder(("pronouns", OpaqueString)) => "member_pronouns_update") .help("Changes a member's pronouns"), command!(member_pronouns, CLEAR => "member_pronouns_clear") .flag(YES) @@ -139,7 +139,7 @@ pub fn cmds() -> impl Iterator { [ command!(member_display_name => "member_displayname_show") .help("Shows a member's display name"), - command!(member_display_name, ("name", OpaqueStringRemainder) => "member_displayname_update") + command!(member_display_name, Remainder(("name", OpaqueString)) => "member_displayname_update") .help("Changes a member's display name"), command!(member_display_name, CLEAR => "member_displayname_clear") .flag(YES) @@ -152,7 +152,7 @@ pub fn cmds() -> impl Iterator { [ command!(member_server_name => "member_servername_show") .help("Shows a member's server name"), - command!(member_server_name, ("name", OpaqueStringRemainder) => "member_servername_update") + command!(member_server_name, Remainder(("name", OpaqueString)) => "member_servername_update") .help("Changes a member's server name"), command!(member_server_name, CLEAR => "member_servername_clear") .flag(YES) @@ -165,7 +165,7 @@ pub fn cmds() -> impl Iterator { [ command!(member_proxy => "member_proxy_show") .help("Shows a member's proxy tags"), - command!(member_proxy, ("tags", OpaqueString) => "member_proxy_set") + command!(member_proxy, Remainder(("tags", OpaqueString)) => "member_proxy_set") .help("Sets a member's proxy tags"), command!(member_proxy, ("add", ["a"]), ("tag", OpaqueString) => "member_proxy_add") .help("Adds proxy tag to a member"), @@ -298,7 +298,7 @@ pub fn cmds() -> impl Iterator { let member_list_group_cmds = [ command!(member_group => "member_list_groups"), command!(member_group, "list" => "member_list_groups"), - command!(member_group, ("search", ["find", "query"]), ("query", OpaqueStringRemainder) => "member_search_groups"), + command!(member_group, ("search", ["find", "query"]), Remainder(("query", OpaqueString)) => "member_search_groups"), ] .into_iter() .map(|cmd| cmd.flags(get_list_flags())); diff --git a/crates/command_definitions/src/message.rs b/crates/command_definitions/src/message.rs index f9bfec49..f341fa78 100644 --- a/crates/command_definitions/src/message.rs +++ b/crates/command_definitions/src/message.rs @@ -8,6 +8,7 @@ pub fn cmds() -> impl Iterator { let reproxy = ("reproxy", ["rp", "crimes", "crime"]); let edit = ("edit", ["e"]); + let new_content_param = Remainder(("new_content", OpaqueString)); let apply_edit = |cmd: Command| { cmd.flag(("append", ["a"])) .flag(("prepend", ["p"])) @@ -25,9 +26,9 @@ pub fn cmds() -> impl Iterator { .help("Shows information about a proxied message"), command!(message, author => "message_author").help("Shows the author of a proxied message"), command!(message, delete => "message_delete").help("Deletes a proxied message"), - apply_edit(command!(message, edit, ("new_content", OpaqueStringRemainder) => "message_edit_specified")), - apply_edit(command!(edit, Skip(MessageRef), ("new_content", OpaqueStringRemainder) => "message_edit_specified")), - apply_edit(command!(edit, ("new_content", OpaqueStringRemainder) => "message_edit")), + apply_edit(command!(message, edit, new_content_param => "message_edit_specified")), + apply_edit(command!(edit, Skip(MessageRef), new_content_param => "message_edit_specified")), + apply_edit(command!(edit, new_content_param => "message_edit")), command!(reproxy, ("member", MemberRef) => "message_reproxy") .help("Reproxies a message with a different member"), command!(reproxy, ("msg", MessageRef), ("member", MemberRef) => "message_reproxy_specified") diff --git a/crates/command_definitions/src/switch.rs b/crates/command_definitions/src/switch.rs index ab5167c3..ef9e15de 100644 --- a/crates/command_definitions/src/switch.rs +++ b/crates/command_definitions/src/switch.rs @@ -20,7 +20,7 @@ pub fn cmds() -> impl Iterator { command!(switch, ("commands", ["help"]) => "switch_commands"), command!(switch, out => "switch_out"), command!(switch, delete => "switch_delete").flag(("all", ["clear", "c"])), - command!(switch, r#move, OpaqueStringRemainder => "switch_move"), // TODO: datetime parsing + command!(switch, r#move, Remainder(OpaqueString) => "switch_move"), // TODO: datetime parsing command!(switch, edit, out => "switch_edit_out").flag(YES), command!(switch, edit, Optional(MemberRefs) => "switch_edit").flags(edit_flags), command!(switch, copy, Optional(MemberRefs) => "switch_copy").flags(edit_flags), diff --git a/crates/command_definitions/src/system.rs b/crates/command_definitions/src/system.rs index 2bacba40..6af66566 100644 --- a/crates/command_definitions/src/system.rs +++ b/crates/command_definitions/src/system.rs @@ -23,7 +23,7 @@ pub fn edit() -> impl Iterator { let system_new = tokens!(system, ("new", ["n"])); let system_new_cmd = [ command!(system_new => "system_new").help("Creates a new system"), - command!(system_new, ("name", OpaqueStringRemainder) => "system_new_name") + command!(system_new, Remainder(("name", OpaqueString)) => "system_new_name") .help("Creates a new system (using the provided name)"), ] .into_iter(); @@ -62,7 +62,7 @@ pub fn edit() -> impl Iterator { command!(system_name_self, CLEAR => "system_clear_name") .flag(YES) .help("Clears your system's name"), - command!(system_name_self, ("name", OpaqueStringRemainder) => "system_rename") + command!(system_name_self, Remainder(("name", OpaqueString)) => "system_rename") .help("Renames your system"), ] .into_iter(); @@ -80,7 +80,7 @@ pub fn edit() -> impl Iterator { command!(system_server_name_self, CLEAR => "system_clear_server_name") .flag(YES) .help("Clears your system's server name"), - command!(system_server_name_self, ("name", OpaqueStringRemainder) => "system_rename_server_name") + command!(system_server_name_self, Remainder(("name", OpaqueString)) => "system_rename_server_name") .help("Renames your system's server name"), ] .into_iter(); @@ -97,7 +97,7 @@ pub fn edit() -> impl Iterator { command!(system_description_self, CLEAR => "system_clear_description") .flag(YES) .help("Clears your system's description"), - command!(system_description_self, ("description", OpaqueStringRemainder) => "system_change_description") + command!(system_description_self, Remainder(("description", OpaqueString)) => "system_change_description") .help("Changes your system's description"), ] .into_iter(); @@ -128,7 +128,7 @@ pub fn edit() -> impl Iterator { command!(system_tag_self, CLEAR => "system_clear_tag") .flag(YES) .help("Clears your system's tag"), - command!(system_tag_self, ("tag", OpaqueStringRemainder) => "system_change_tag") + command!(system_tag_self, Remainder(("tag", OpaqueString)) => "system_change_tag") .help("Changes your system's tag"), ] .into_iter(); @@ -146,7 +146,7 @@ pub fn edit() -> impl Iterator { command!(system_server_tag_self, CLEAR => "system_clear_server_tag") .flag(YES) .help("Clears your system's server tag"), - command!(system_server_tag_self, ("tag", OpaqueStringRemainder) => "system_change_server_tag") + command!(system_server_tag_self, Remainder(("tag", OpaqueString)) => "system_change_server_tag") .help("Changes your system's server tag"), ] .into_iter(); @@ -163,7 +163,7 @@ pub fn edit() -> impl Iterator { command!(system_pronouns_self, CLEAR => "system_clear_pronouns") .flag(YES) .help("Clears your system's pronouns"), - command!(system_pronouns_self, ("pronouns", OpaqueStringRemainder) => "system_change_pronouns") + command!(system_pronouns_self, Remainder(("pronouns", OpaqueString)) => "system_change_pronouns") .help("Changes your system's pronouns"), ] .into_iter(); @@ -274,7 +274,7 @@ pub fn edit() -> impl Iterator { let system_list = tokens!("members", list); let search = tokens!( ("search", ["query", "find"]), - ("query", OpaqueStringRemainder), + Remainder(("query", OpaqueString)) ); let add_list_flags = |cmd: Command| cmd.flags(get_list_flags()); let system_list_cmd = [ diff --git a/crates/command_parser/src/parameter.rs b/crates/command_parser/src/parameter.rs index 5645ba7f..0d13d773 100644 --- a/crates/command_parser/src/parameter.rs +++ b/crates/command_parser/src/parameter.rs @@ -12,7 +12,6 @@ use crate::token::{Token, TokenMatchResult}; pub enum ParameterKind { OpaqueString, OpaqueInt, - OpaqueStringRemainder, MemberRef, MemberRefs, GroupRef, @@ -36,7 +35,6 @@ impl ParameterKind { match self { ParameterKind::OpaqueString => "string", ParameterKind::OpaqueInt => "number", - ParameterKind::OpaqueStringRemainder => "string", ParameterKind::MemberRef => "target", ParameterKind::MemberRefs => "targets", ParameterKind::GroupRef => "target", @@ -80,13 +78,6 @@ pub enum ParameterValue { Null, } -fn is_remainder(kind: ParameterKind) -> bool { - matches!( - kind, - ParameterKind::OpaqueStringRemainder | ParameterKind::MemberRefs | ParameterKind::GroupRefs - ) -} - #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Parameter { name: SmolStr, @@ -135,9 +126,7 @@ impl Parameter { pub fn match_value(&self, input: &str) -> Result { match self.kind { // TODO: actually parse image url - ParameterKind::OpaqueString | ParameterKind::OpaqueStringRemainder => { - Ok(ParameterValue::OpaqueString(input.into())) - } + ParameterKind::OpaqueString => Ok(ParameterValue::OpaqueString(input.into())), ParameterKind::OpaqueInt => input .parse::() .map(|num| ParameterValue::OpaqueInt(num)) @@ -254,13 +243,10 @@ impl Display for Parameter { ParameterKind::OpaqueInt => { write!(f, "[{}]", self.name) } - ParameterKind::OpaqueStringRemainder => { - write!(f, "[{}]...", self.name) - } ParameterKind::MemberRef => write!(f, ""), - ParameterKind::MemberRefs => write!(f, " ..."), + ParameterKind::MemberRefs => write!(f, " "), ParameterKind::GroupRef => write!(f, ""), - ParameterKind::GroupRefs => write!(f, " ..."), + ParameterKind::GroupRefs => write!(f, " "), ParameterKind::SystemRef => write!(f, ""), ParameterKind::UserRef => write!(f, ""), ParameterKind::MessageRef => write!(f, ""), @@ -273,10 +259,18 @@ impl Display for Parameter { ParameterKind::Toggle => write!(f, ""), ParameterKind::Avatar => write!(f, ""), ParameterKind::ProxySwitchAction => write!(f, ""), + }?; + if self.is_remainder() { + write!(f, "...")?; } + Ok(()) } } +fn is_remainder(kind: ParameterKind) -> bool { + matches!(kind, ParameterKind::MemberRefs | ParameterKind::GroupRefs) +} + impl From for Parameter { fn from(value: ParameterKind) -> Self { Parameter { @@ -301,6 +295,7 @@ impl From<(&str, ParameterKind)> for Parameter { } } +/// if no input is left to parse, this parameter matches to Null #[derive(Clone)] pub struct Optional>(pub P); @@ -311,6 +306,7 @@ impl> From> for Parameter { } } +/// tells the parser to use the remainder of the input as the input to this parameter #[derive(Clone)] pub struct Remainder>(pub P); @@ -321,8 +317,7 @@ impl> From> for Parameter { } } -// todo(dusk): this is kind of annoying to use, should probably introduce -// a way to match multiple parameters in a single parameter +/// skips the branch this parameter is in if it does not match #[derive(Clone)] pub struct Skip>(pub P); diff --git a/crates/commands/src/lib.rs b/crates/commands/src/lib.rs index ed67b282..08a094f2 100644 --- a/crates/commands/src/lib.rs +++ b/crates/commands/src/lib.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, fmt::Write, usize}; +use std::{collections::HashMap, fmt::Write}; use command_parser::{parameter::ParameterValue, token::TokenMatchResult, Tree}; diff --git a/crates/commands/src/write_cs_glue.rs b/crates/commands/src/write_cs_glue.rs index de1b5e74..d2269d03 100644 --- a/crates/commands/src/write_cs_glue.rs +++ b/crates/commands/src/write_cs_glue.rs @@ -262,7 +262,7 @@ fn command_callback_to_name(cb: &str) -> String { fn get_param_ty(kind: ParameterKind) -> &'static str { match kind { - ParameterKind::OpaqueString | ParameterKind::OpaqueStringRemainder => "string", + ParameterKind::OpaqueString => "string", ParameterKind::OpaqueInt => "int", ParameterKind::MemberRef => "PKMember", ParameterKind::MemberRefs => "List", @@ -285,7 +285,7 @@ fn get_param_ty(kind: ParameterKind) -> &'static str { fn get_param_param_ty(kind: ParameterKind) -> &'static str { match kind { - ParameterKind::OpaqueString | ParameterKind::OpaqueStringRemainder => "Opaque", + ParameterKind::OpaqueString => "Opaque", ParameterKind::OpaqueInt => "Number", ParameterKind::MemberRef => "Member", ParameterKind::MemberRefs => "Members",