Skip to content

Commit

Permalink
Rewrite builders to take Cows
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jan 5, 2024
1 parent 4e4e274 commit 4c73b15
Show file tree
Hide file tree
Showing 67 changed files with 1,160 additions and 883 deletions.
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "e14_slash_commands"
version = "0.1.0"
authors = ["my name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "collector"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn run(options: &[ResolvedOption]) -> String {
}
}

pub fn register() -> CreateCommand {
pub fn register() -> CreateCommand<'static> {
CreateCommand::new("attachmentinput")
.description("Test command for attachment input")
.add_option(
Expand Down
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/src/commands/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn run(options: &[ResolvedOption]) -> String {
}
}

pub fn register() -> CreateCommand {
pub fn register() -> CreateCommand<'static> {
CreateCommand::new("id").description("Get a user id").add_option(
CreateCommandOption::new(CommandOptionType::User, "id", "The user to lookup")
.required(true),
Expand Down
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/src/commands/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ pub async fn run(ctx: &Context, interaction: &CommandInteraction) -> Result<(),
Ok(())
}

pub fn register() -> CreateCommand {
pub fn register() -> CreateCommand<'static> {
CreateCommand::new("modal").description("Asks some details about you")
}
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/src/commands/numberinput.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serenity::builder::{CreateCommand, CreateCommandOption};
use serenity::model::application::CommandOptionType;

pub fn register() -> CreateCommand {
pub fn register() -> CreateCommand<'static> {
CreateCommand::new("numberinput")
.description("Test command for number input")
.add_option(
Expand Down
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/src/commands/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub fn run(_options: &[ResolvedOption]) -> String {
"Hey, I'm alive!".to_string()
}

pub fn register() -> CreateCommand {
pub fn register() -> CreateCommand<'static> {
CreateCommand::new("ping").description("A ping command")
}
30 changes: 20 additions & 10 deletions examples/e14_slash_commands/src/commands/welcome.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use std::borrow::Cow;
use std::collections::HashMap;

use serenity::builder::{CreateCommand, CreateCommandOption};
use serenity::model::application::CommandOptionType;

pub fn register() -> CreateCommand {
fn new_map<'a>(key: &'a str, value: &'a str) -> HashMap<Cow<'a, str>, Cow<'a, str>> {
let mut map = HashMap::with_capacity(1);
map.insert(Cow::Borrowed(key), Cow::Borrowed(value));
map
}

pub fn register() -> CreateCommand<'static> {
CreateCommand::new("welcome")
.description("Welcome a user")
.name_localized("de", "begrüßen")
Expand All @@ -20,27 +29,28 @@ pub fn register() -> CreateCommand {
.add_string_choice_localized(
"Welcome to our cool server! Ask me if you need help",
"pizza",
[(
new_map(
"de",
"Willkommen auf unserem coolen Server! Frag mich, falls du Hilfe brauchst",
)],
),
)
.add_string_choice_localized(
"Hey, do you want a coffee?",
"coffee",
new_map("de", "Hey, willst du einen Kaffee?"),
)
.add_string_choice_localized("Hey, do you want a coffee?", "coffee", [(
"de",
"Hey, willst du einen Kaffee?",
)])
.add_string_choice_localized(
"Welcome to the club, you're now a good person. Well, I hope.",
"club",
[(
new_map(
"de",
"Willkommen im Club, du bist jetzt ein guter Mensch. Naja, hoffentlich.",
)],
),
)
.add_string_choice_localized(
"I hope that you brought a controller to play together!",
"game",
[("de", "Ich hoffe du hast einen Controller zum Spielen mitgebracht!")],
new_map("de", "Ich hoffe du hast einen Controller zum Spielen mitgebracht!"),
),
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serenity::builder::CreateCommand;

pub fn register() -> CreateCommand {
pub fn register() -> CreateCommand<'static> {
CreateCommand::new("wonderful_command").description("An amazing command")
}
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl EventHandler for Handler {
);

let commands = guild_id
.set_commands(&ctx.http, vec![
.set_commands(&ctx.http, &[
commands::ping::register(),
commands::id::register(),
commands::welcome::register(),
Expand Down
5 changes: 3 additions & 2 deletions examples/e17_message_components/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::env;
use std::time::Duration;

Expand Down Expand Up @@ -40,13 +41,13 @@ impl EventHandler for Handler {
&ctx,
CreateMessage::new().content("Please select your favorite animal").select_menu(
CreateSelectMenu::new("animal_select", CreateSelectMenuKind::String {
options: vec![
options: Cow::Borrowed(&[
CreateSelectMenuOption::new("🐈 meow", "Cat"),
CreateSelectMenuOption::new("🐕 woof", "Dog"),
CreateSelectMenuOption::new("🐎 neigh", "Horse"),
CreateSelectMenuOption::new("🦙 hoooooooonk", "Alpaca"),
CreateSelectMenuOption::new("🦀 crab rave", "Ferris"),
],
]),
})
.custom_id("animal_select")
.placeholder("No animal selected"),
Expand Down
2 changes: 1 addition & 1 deletion examples/e19_interactions_endpoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serenity::model::application::*;

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

fn handle_command(interaction: CommandInteraction) -> CreateInteractionResponse {
fn handle_command(interaction: CommandInteraction) -> CreateInteractionResponse<'static> {
CreateInteractionResponse::Message(CreateInteractionResponseMessage::new().content(format!(
"Hello from interactions webhook HTTP server! <@{}>",
interaction.user.id
Expand Down
13 changes: 8 additions & 5 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use serenity::builder::*;
use serenity::model::prelude::*;
use serenity::prelude::*;
Expand Down Expand Up @@ -98,10 +100,10 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
CreateButton::new_link("https://google.com").emoji('🔍').label("Search"),
)
.select_menu(CreateSelectMenu::new("3", CreateSelectMenuKind::String {
options: vec![
options: Cow::Borrowed(&[
CreateSelectMenuOption::new("foo", "foo"),
CreateSelectMenuOption::new("bar", "bar"),
],
]),
})),
)
.await?;
Expand Down Expand Up @@ -162,7 +164,8 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
channel_id
.edit_thread(
&ctx,
EditThread::new().applied_tags(forum.available_tags.iter().map(|t| t.id)),
EditThread::new()
.applied_tags(forum.available_tags.iter().map(|t| t.id).collect::<Vec<_>>()),
)
.await?;
} else if msg.content == "embedrace" {
Expand Down Expand Up @@ -324,10 +327,10 @@ async fn interaction(
CreateInteractionResponse::Message(
CreateInteractionResponseMessage::new()
.select_menu(CreateSelectMenu::new("0", CreateSelectMenuKind::String {
options: vec![
options: Cow::Borrowed(&[
CreateSelectMenuOption::new("foo", "foo"),
CreateSelectMenuOption::new("bar", "bar"),
],
]),
}))
.select_menu(CreateSelectMenu::new(
"1",
Expand Down
30 changes: 16 additions & 14 deletions src/builder/add_member.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use super::Builder;
#[cfg(feature = "http")]
Expand All @@ -11,25 +13,25 @@ use crate::model::prelude::*;
/// [Discord docs](https://discord.com/developers/docs/resources/guild#add-guild-member).
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct AddMember {
access_token: String,
pub struct AddMember<'a> {
access_token: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
nick: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
roles: Vec<RoleId>,
nick: Option<Cow<'a, str>>,
#[serde(skip_serializing_if = "<[RoleId]>::is_empty")]
roles: Cow<'a, [RoleId]>,
#[serde(skip_serializing_if = "Option::is_none")]
mute: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
deaf: Option<bool>,
}

impl AddMember {
impl<'a> AddMember<'a> {
/// Constructs a new builder with the given access token, leaving all other fields empty.
pub fn new(access_token: String) -> Self {
pub fn new(access_token: impl Into<Cow<'a, str>>) -> Self {
Self {
access_token,
access_token: access_token.into(),
roles: Cow::default(),
nick: None,
roles: Vec::new(),
mute: None,
deaf: None,
}
Expand All @@ -38,7 +40,7 @@ impl AddMember {
/// Sets the OAuth2 access token for this request, replacing the current one.
///
/// Requires the access token to have the `guilds.join` scope granted.
pub fn access_token(mut self, access_token: impl Into<String>) -> Self {
pub fn access_token(mut self, access_token: impl Into<Cow<'a, str>>) -> Self {
self.access_token = access_token.into();
self
}
Expand All @@ -48,7 +50,7 @@ impl AddMember {
/// Requires the [Manage Nicknames] permission.
///
/// [Manage Nicknames]: crate::model::permissions::Permissions::MANAGE_NICKNAMES
pub fn nickname(mut self, nickname: impl Into<String>) -> Self {
pub fn nickname(mut self, nickname: impl Into<Cow<'a, str>>) -> Self {
self.nick = Some(nickname.into());
self
}
Expand All @@ -58,8 +60,8 @@ impl AddMember {
/// Requires the [Manage Roles] permission.
///
/// [Manage Roles]: crate::model::permissions::Permissions::MANAGE_ROLES
pub fn roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleId>>) -> Self {
self.roles = roles.into_iter().map(Into::into).collect();
pub fn roles(mut self, roles: impl Into<Cow<'a, [RoleId]>>) -> Self {
self.roles = roles.into();
self
}

Expand All @@ -86,7 +88,7 @@ impl AddMember {

#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for AddMember {
impl Builder for AddMember<'_> {
type Context<'ctx> = (GuildId, UserId);
type Built = Option<Member>;

Expand Down
28 changes: 19 additions & 9 deletions src/builder/bot_auth_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::borrow::Cow;
use std::fmt::Write;

use arrayvec::ArrayVec;
use url::Url;

Expand All @@ -8,18 +11,28 @@ use crate::internal::prelude::*;
use crate::model::application::Scope;
use crate::model::prelude::*;

fn join_to_string(sep: char, iter: impl Iterator<Item = impl std::fmt::Display>) -> String {
let mut buf = String::new();
for item in iter {
write!(buf, "{item}{sep}").unwrap();
}

buf.truncate(buf.len() - 1);
buf
}

/// A builder for constructing an invite link with custom OAuth2 scopes.
#[derive(Debug, Clone, Default)]
#[must_use]
pub struct CreateBotAuthParameters {
pub struct CreateBotAuthParameters<'a> {
client_id: Option<ApplicationId>,
scopes: Vec<Scope>,
scopes: Cow<'a, [Scope]>,
permissions: Permissions,
guild_id: Option<GuildId>,
disable_guild_select: bool,
}

impl CreateBotAuthParameters {
impl<'a> CreateBotAuthParameters<'a> {
/// Equivalent to [`Self::default`].
pub fn new() -> Self {
Self::default()
Expand All @@ -36,10 +49,7 @@ impl CreateBotAuthParameters {
}

if !self.scopes.is_empty() {
valid_data.push((
"scope",
self.scopes.iter().map(ToString::to_string).collect::<Vec<_>>().join(" "),
));
valid_data.push(("scope", join_to_string(',', self.scopes.iter())));
}

if bits != 0 {
Expand Down Expand Up @@ -85,8 +95,8 @@ impl CreateBotAuthParameters {
/// **Note**: This needs to include the [`Bot`] scope.
///
/// [`Bot`]: Scope::Bot
pub fn scopes(mut self, scopes: &[Scope]) -> Self {
self.scopes = scopes.to_vec();
pub fn scopes(mut self, scopes: impl Into<Cow<'a, [Scope]>>) -> Self {
self.scopes = scopes.into();
self
}

Expand Down
Loading

0 comments on commit 4c73b15

Please sign in to comment.