From 397462cc88bb2611d12b19d4952a3fd3c00611cc Mon Sep 17 00:00:00 2001 From: Gnome! Date: Tue, 30 Jan 2024 12:26:14 +0000 Subject: [PATCH] Take prefix as Cow<'static, str> (#242) --- src/builtins/help.rs | 8 +++++--- src/builtins/pretty_help.rs | 14 +++++++------- src/dispatch/prefix.rs | 4 ++-- src/structs/prefix.rs | 9 ++++++--- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/builtins/help.rs b/src/builtins/help.rs index 8d86ae1cc401..c5780b4288d3 100644 --- a/src/builtins/help.rs +++ b/src/builtins/help.rs @@ -1,7 +1,7 @@ //! Contains the built-in help command and surrounding infrastructure use crate::{serenity_prelude as serenity, CreateReply}; -use std::fmt::Write as _; +use std::{borrow::Cow, fmt::Write as _}; /// Optional configuration for how the help message from [`help()`] looks pub struct HelpConfiguration<'a> { @@ -85,7 +85,9 @@ impl TwoColumnList { } /// Get the prefix from options -pub(super) async fn get_prefix_from_options(ctx: crate::Context<'_, U, E>) -> Option { +pub(super) async fn get_prefix_from_options( + ctx: crate::Context<'_, U, E>, +) -> Option> { let options = &ctx.framework().options().prefix_options; match &options.prefix { Some(fixed_prefix) => Some(fixed_prefix.clone()), @@ -155,7 +157,7 @@ async fn help_single_command( // None can happen if the prefix is dynamic, and the callback // fails due to help being invoked with slash or context menu // commands. Not sure there's a better way to handle this. - None => String::from(""), + None => Cow::Borrowed(""), }; invocations.push(format!("`{}{}`", prefix, command.name)); if subprefix.is_none() { diff --git a/src/builtins/pretty_help.rs b/src/builtins/pretty_help.rs index 1a578ef66dea..c9cda41bbb63 100644 --- a/src/builtins/pretty_help.rs +++ b/src/builtins/pretty_help.rs @@ -1,7 +1,7 @@ //! Contains a built-in help command and surrounding infrastructure that uses embeds. use crate::{serenity_prelude as serenity, CreateReply}; -use std::fmt::Write as _; +use std::{borrow::Cow, fmt::Write as _}; /// Optional configuration for how the help message from [`pretty_help()`] looks pub struct PrettyHelpConfiguration<'a> { @@ -81,7 +81,7 @@ async fn pretty_help_all_commands( for cmd in cmds { let name = cmd.context_menu_name.as_deref().unwrap_or(&cmd.name); - let prefix = format_cmd_prefix(cmd, &options_prefix); + let prefix = format_cmd_prefix(cmd, options_prefix.as_deref()); if let Some(description) = cmd.description.as_deref() { writeln!(buffer, "{}{}`: *{}*", prefix, name, description).ok(); @@ -92,7 +92,7 @@ async fn pretty_help_all_commands( if config.show_subcommands { for sbcmd in &cmd.subcommands { let name = sbcmd.context_menu_name.as_deref().unwrap_or(&sbcmd.name); - let prefix = format_cmd_prefix(sbcmd, &options_prefix); + let prefix = format_cmd_prefix(sbcmd, options_prefix.as_deref()); if let Some(description) = sbcmd.description.as_deref() { writeln!(buffer, "> {}{}`: *{}*", prefix, name, description).ok(); @@ -127,11 +127,11 @@ async fn pretty_help_all_commands( } /// Figures out which prefix a command should have -fn format_cmd_prefix(cmd: &crate::Command, options_prefix: &Option) -> String { +fn format_cmd_prefix(cmd: &crate::Command, options_prefix: Option<&str>) -> String { if cmd.slash_action.is_some() { "`/".into() } else if cmd.prefix_action.is_some() { - format!("`{}", options_prefix.as_deref().unwrap_or_default()) + format!("`{}", options_prefix.unwrap_or_default()) } else if cmd.context_menu_action.is_some() { match cmd.context_menu_action { Some(crate::ContextMenuCommandAction::Message(_)) => "Message menu: `".into(), @@ -187,7 +187,7 @@ async fn pretty_help_single_command( .await // This can happen if the prefix is dynamic, and the callback fails // due to help being invoked with slash or context menu commands. - .unwrap_or_else(|| String::from("")); + .unwrap_or(Cow::Borrowed("")); invocations.push(format!("`{}{}`", prefix, command.name)); subprefix = subprefix.or(Some(format!("> `{}{}`", prefix, command.name))); } @@ -247,7 +247,7 @@ async fn pretty_help_single_command( .subcommands .iter() .map(|sbcmd| { - let prefix = format_cmd_prefix(sbcmd, &subprefix); // i have no idea about this really + let prefix = format_cmd_prefix(sbcmd, subprefix.as_deref()); // i have no idea about this really let name = sbcmd.context_menu_name.as_deref().unwrap_or(&sbcmd.name); if let Some(description) = sbcmd.description.as_deref() { format!("> {}{}`: *{} *", prefix, name, description) diff --git a/src/dispatch/prefix.rs b/src/dispatch/prefix.rs index a73ef0cb84e1..1cbc6d8ab78f 100644 --- a/src/dispatch/prefix.rs +++ b/src/dispatch/prefix.rs @@ -21,7 +21,7 @@ async fn strip_prefix<'a, U, E>( match dynamic_prefix(partial_ctx).await { Ok(prefix) => { if let Some(prefix) = prefix { - if msg.content.starts_with(&prefix) { + if msg.content.starts_with(prefix.as_ref()) { return Some(msg.content.split_at(prefix.len())); } } @@ -37,7 +37,7 @@ async fn strip_prefix<'a, U, E>( } } - if let Some(prefix) = &framework.options.prefix_options.prefix { + if let Some(prefix) = framework.options.prefix_options.prefix.as_deref() { if let Some(content) = msg.content.strip_prefix(prefix) { return Some((prefix, content)); } diff --git a/src/structs/prefix.rs b/src/structs/prefix.rs index afcf37b35bde..8a754b6250fe 100644 --- a/src/structs/prefix.rs +++ b/src/structs/prefix.rs @@ -1,5 +1,7 @@ //! Holds prefix-command definition structs. +use std::borrow::Cow; + use crate::{serenity_prelude as serenity, BoxFuture}; /// The event that triggered a prefix command execution @@ -82,7 +84,7 @@ pub enum Prefix { pub struct PrefixFrameworkOptions { /// The main bot prefix. Can be set to None if the bot supports only /// [dynamic prefixes](Self::dynamic_prefix). - pub prefix: Option, + pub prefix: Option>, /// List of additional bot prefixes // TODO: maybe it would be nicer to have separate fields for literal and regex prefixes // That way, you don't need to wrap every single literal prefix in a long path which looks ugly @@ -93,8 +95,9 @@ pub struct PrefixFrameworkOptions { /// /// For more advanced dynamic prefixes, see [`Self::stripped_dynamic_prefix`] #[derivative(Debug = "ignore")] - pub dynamic_prefix: - Option) -> BoxFuture<'_, Result, E>>>, + pub dynamic_prefix: Option< + fn(crate::PartialContext<'_, U, E>) -> BoxFuture<'_, Result>, E>>, + >, /// Callback invoked on every message to strip the prefix off an incoming message. /// /// Override this field for advanced dynamic prefixes which change depending on guild or user.