Skip to content

Commit

Permalink
Take prefix as Cow<'static, str> (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed May 3, 2024
1 parent 85c47e7 commit 397462c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/builtins/help.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -85,7 +85,9 @@ impl TwoColumnList {
}

/// Get the prefix from options
pub(super) async fn get_prefix_from_options<U, E>(ctx: crate::Context<'_, U, E>) -> Option<String> {
pub(super) async fn get_prefix_from_options<U, E>(
ctx: crate::Context<'_, U, E>,
) -> Option<Cow<'static, str>> {
let options = &ctx.framework().options().prefix_options;
match &options.prefix {
Some(fixed_prefix) => Some(fixed_prefix.clone()),
Expand Down Expand Up @@ -155,7 +157,7 @@ async fn help_single_command<U, E>(
// 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("<prefix>"),
None => Cow::Borrowed("<prefix>"),
};
invocations.push(format!("`{}{}`", prefix, command.name));
if subprefix.is_none() {
Expand Down
14 changes: 7 additions & 7 deletions src/builtins/pretty_help.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -81,7 +81,7 @@ async fn pretty_help_all_commands<U, E>(

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();
Expand All @@ -92,7 +92,7 @@ async fn pretty_help_all_commands<U, E>(
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();
Expand Down Expand Up @@ -127,11 +127,11 @@ async fn pretty_help_all_commands<U, E>(
}

/// Figures out which prefix a command should have
fn format_cmd_prefix<U, E>(cmd: &crate::Command<U, E>, options_prefix: &Option<String>) -> String {
fn format_cmd_prefix<U, E>(cmd: &crate::Command<U, E>, 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(),
Expand Down Expand Up @@ -187,7 +187,7 @@ async fn pretty_help_single_command<U, E>(
.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("<prefix>"));
.unwrap_or(Cow::Borrowed("<prefix>"));
invocations.push(format!("`{}{}`", prefix, command.name));
subprefix = subprefix.or(Some(format!("> `{}{}`", prefix, command.name)));
}
Expand Down Expand Up @@ -247,7 +247,7 @@ async fn pretty_help_single_command<U, E>(
.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)
Expand Down
4 changes: 2 additions & 2 deletions src/dispatch/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Expand All @@ -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));
}
Expand Down
9 changes: 6 additions & 3 deletions src/structs/prefix.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -82,7 +84,7 @@ pub enum Prefix {
pub struct PrefixFrameworkOptions<U, E> {
/// The main bot prefix. Can be set to None if the bot supports only
/// [dynamic prefixes](Self::dynamic_prefix).
pub prefix: Option<String>,
pub prefix: Option<Cow<'static, str>>,
/// 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
Expand All @@ -93,8 +95,9 @@ pub struct PrefixFrameworkOptions<U, E> {
///
/// For more advanced dynamic prefixes, see [`Self::stripped_dynamic_prefix`]
#[derivative(Debug = "ignore")]
pub dynamic_prefix:
Option<fn(crate::PartialContext<'_, U, E>) -> BoxFuture<'_, Result<Option<String>, E>>>,
pub dynamic_prefix: Option<
fn(crate::PartialContext<'_, U, E>) -> BoxFuture<'_, Result<Option<Cow<'static, str>>, 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.
Expand Down

0 comments on commit 397462c

Please sign in to comment.