Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NonMax to signal builder range constraints #2703

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions examples/e05_command_framework/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! git = "https://github.com/serenity-rs/serenity.git"
//! features = ["framework", "standard_framework"]
//! ```
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::env;
use std::fmt::Write;
Expand Down Expand Up @@ -545,22 +546,30 @@ async fn am_i_admin(ctx: &Context, msg: &Message, _args: Args) -> CommandResult
#[command]
async fn slow_mode(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let say_content = if let Ok(slow_mode_rate_seconds) = args.single::<u16>() {
let builder = EditChannel::new().rate_limit_per_user(slow_mode_rate_seconds);
if let Err(why) = msg.channel_id.edit(&ctx.http, builder).await {
println!("Error setting channel's slow mode rate: {why:?}");

format!("Failed to set slow mode to `{slow_mode_rate_seconds}` seconds.")
if slow_mode_rate_seconds <= 21600 {
let slow_mode_rate_seconds = serenity::nonmax::NonMaxU16::new(slow_mode_rate_seconds)
.expect("Just checked less than less than max");

let builder = EditChannel::new().rate_limit_per_user(slow_mode_rate_seconds);
if let Err(why) = msg.channel_id.edit(&ctx.http, builder).await {
println!("Error setting channel's slow mode rate: {why:?}");

format!("Failed to set slow mode to `{slow_mode_rate_seconds}` seconds.").into()
} else {
format!("Successfully set slow mode rate to `{slow_mode_rate_seconds}` seconds.")
.into()
}
} else {
format!("Successfully set slow mode rate to `{slow_mode_rate_seconds}` seconds.")
Cow::Borrowed("That is too many seconds to add slow-mode for!")
}
} else if let Some(channel) = msg.channel_id.to_channel_cached(&ctx.cache) {
if let Some(slow_mode_rate) = channel.rate_limit_per_user {
format!("Current slow mode rate is `{slow_mode_rate}` seconds.")
format!("Current slow mode rate is `{slow_mode_rate}` seconds.").into()
} else {
"There is no current slow mode rate for this channel.".to_string()
Cow::Borrowed("There is no current slow mode rate for this channel.")
}
} else {
"Failed to find channel in cache.".to_string()
Cow::Borrowed("Failed to find channel in cache.")
};

msg.channel_id.say(&ctx.http, say_content).await?;
Expand Down
6 changes: 4 additions & 2 deletions src/builder/create_channel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Cow;

use nonmax::NonMaxU16;

#[cfg(feature = "http")]
use super::Builder;
#[cfg(feature = "http")]
Expand Down Expand Up @@ -27,7 +29,7 @@ pub struct CreateChannel<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
user_limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
rate_limit_per_user: Option<u16>,
rate_limit_per_user: Option<NonMaxU16>,
#[serde(skip_serializing_if = "Option::is_none")]
position: Option<u16>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
Expand Down Expand Up @@ -150,7 +152,7 @@ impl<'a> CreateChannel<'a> {
/// [`MANAGE_MESSAGES`]: crate::model::permissions::Permissions::MANAGE_MESSAGES
/// [`MANAGE_CHANNELS`]: crate::model::permissions::Permissions::MANAGE_CHANNELS
#[doc(alias = "slowmode")]
pub fn rate_limit_per_user(mut self, seconds: u16) -> Self {
pub fn rate_limit_per_user(mut self, seconds: NonMaxU16) -> Self {
self.rate_limit_per_user = Some(seconds);
self
}
Expand Down
6 changes: 4 additions & 2 deletions src/builder/create_forum_post.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Cow;

use nonmax::NonMaxU16;

#[cfg(feature = "http")]
use super::Builder;
use super::CreateMessage;
Expand All @@ -17,7 +19,7 @@ pub struct CreateForumPost<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
auto_archive_duration: Option<AutoArchiveDuration>,
#[serde(skip_serializing_if = "Option::is_none")]
rate_limit_per_user: Option<u16>,
rate_limit_per_user: Option<NonMaxU16>,
message: CreateMessage<'a>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
applied_tags: Cow<'a, [ForumTagId]>,
Expand Down Expand Up @@ -71,7 +73,7 @@ impl<'a> CreateForumPost<'a> {
/// [`MANAGE_MESSAGES`]: crate::model::permissions::Permissions::MANAGE_MESSAGES
/// [`MANAGE_CHANNELS`]: crate::model::permissions::Permissions::MANAGE_CHANNELS
#[doc(alias = "slowmode")]
pub fn rate_limit_per_user(mut self, seconds: u16) -> Self {
pub fn rate_limit_per_user(mut self, seconds: NonMaxU16) -> Self {
self.rate_limit_per_user = Some(seconds);
self
}
Expand Down
6 changes: 4 additions & 2 deletions src/builder/create_thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Cow;

use nonmax::NonMaxU16;

#[cfg(feature = "http")]
use super::Builder;
#[cfg(feature = "http")]
Expand All @@ -23,7 +25,7 @@ pub struct CreateThread<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
invitable: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
rate_limit_per_user: Option<u16>,
rate_limit_per_user: Option<NonMaxU16>,

#[serde(skip)]
audit_log_reason: Option<&'a str>,
Expand Down Expand Up @@ -66,7 +68,7 @@ impl<'a> CreateThread<'a> {
/// [`MANAGE_MESSAGES`]: crate::model::permissions::Permissions::MANAGE_MESSAGES
/// [`MANAGE_CHANNELS`]: crate::model::permissions::Permissions::MANAGE_CHANNELS
#[doc(alias = "slowmode")]
pub fn rate_limit_per_user(mut self, seconds: u16) -> Self {
pub fn rate_limit_per_user(mut self, seconds: NonMaxU16) -> Self {
self.rate_limit_per_user = Some(seconds);
self
}
Expand Down
10 changes: 6 additions & 4 deletions src/builder/edit_channel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Cow;

use nonmax::NonMaxU16;

#[cfg(feature = "http")]
use super::Builder;
use super::CreateForumTag;
Expand Down Expand Up @@ -47,7 +49,7 @@ pub struct EditChannel<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
nsfw: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
rate_limit_per_user: Option<u16>,
rate_limit_per_user: Option<NonMaxU16>,
#[serde(skip_serializing_if = "Option::is_none")]
bitrate: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -69,7 +71,7 @@ pub struct EditChannel<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
default_reaction_emoji: Option<Option<ForumEmoji>>,
#[serde(skip_serializing_if = "Option::is_none")]
default_thread_rate_limit_per_user: Option<u16>,
default_thread_rate_limit_per_user: Option<NonMaxU16>,
#[serde(skip_serializing_if = "Option::is_none")]
default_sort_order: Option<SortOrder>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -185,7 +187,7 @@ impl<'a> EditChannel<'a> {
/// [`MANAGE_MESSAGES`]: Permissions::MANAGE_MESSAGES
/// [`MANAGE_CHANNELS`]: Permissions::MANAGE_CHANNELS
#[doc(alias = "slowmode")]
pub fn rate_limit_per_user(mut self, seconds: u16) -> Self {
pub fn rate_limit_per_user(mut self, seconds: NonMaxU16) -> Self {
self.rate_limit_per_user = Some(seconds);
self
}
Expand Down Expand Up @@ -272,7 +274,7 @@ impl<'a> EditChannel<'a> {
/// copied to the thread at creation time and does not live update.
pub fn default_thread_rate_limit_per_user(
mut self,
default_thread_rate_limit_per_user: u16,
default_thread_rate_limit_per_user: NonMaxU16,
) -> Self {
self.default_thread_rate_limit_per_user = Some(default_thread_rate_limit_per_user);
self
Expand Down
6 changes: 4 additions & 2 deletions src/builder/edit_thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::Cow;

use nonmax::NonMaxU16;

#[cfg(feature = "http")]
use super::Builder;
#[cfg(feature = "http")]
Expand All @@ -23,7 +25,7 @@ pub struct EditThread<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
invitable: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
rate_limit_per_user: Option<u16>,
rate_limit_per_user: Option<NonMaxU16>,
#[serde(skip_serializing_if = "Option::is_none")]
flags: Option<ChannelFlags>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -79,7 +81,7 @@ impl<'a> EditThread<'a> {
/// Amount of seconds a user has to wait before sending another message (0-21600); bots, as well
/// as users with the permission manage_messages, manage_thread, or manage_channel, are
/// unaffected
pub fn rate_limit_per_user(mut self, rate_limit_per_user: u16) -> Self {
pub fn rate_limit_per_user(mut self, rate_limit_per_user: NonMaxU16) -> Self {
self.rate_limit_per_user = Some(rate_limit_per_user);
self
}
Expand Down
6 changes: 4 additions & 2 deletions src/builder/edit_webhook.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;
use super::CreateAttachment;
Expand All @@ -12,7 +14,7 @@ use crate::model::prelude::*;
#[must_use]
pub struct EditWebhook<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
name: Option<Cow<'a, str>>,
#[serde(skip_serializing_if = "Option::is_none")]
avatar: Option<Option<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -31,7 +33,7 @@ impl<'a> EditWebhook<'a> {
/// Set the webhook's name.
///
/// This must be between 1-80 characters.
pub fn name(mut self, name: impl Into<String>) -> Self {
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
self.name = Some(name.into());
self
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,6 @@ pub mod all {
*,
};
}

// Re-exports of crates used internally which are already publically exposed.
pub use nonmax;