From b08cfa0b46e78425622f9071ac3c580c7c260f3c Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Thu, 19 Jan 2023 13:35:02 -0500 Subject: [PATCH 1/3] feat(http, validate)!: channel user limits Validate channel `user_limit` fields on `UpdateChannel` and `CreateGuildChannel`, introducing a new `user_limit` function in `twilight_validate::channel`. Part of #1477. --- .../src/request/channel/update_channel.rs | 13 +++++-- twilight-validate/src/channel.rs | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/twilight-http/src/request/channel/update_channel.rs b/twilight-http/src/request/channel/update_channel.rs index 2705685ee95..131eb44c40a 100644 --- a/twilight-http/src/request/channel/update_channel.rs +++ b/twilight-http/src/request/channel/update_channel.rs @@ -18,7 +18,7 @@ use twilight_model::{ use twilight_validate::{ channel::{ bitrate as validate_bitrate, forum_topic as validate_forum_topic, name as validate_name, - topic as validate_topic, ChannelValidationError, + topic as validate_topic, user_limit as validate_user_limit, ChannelValidationError, }, request::{audit_reason as validate_audit_reason, ValidationError}, }; @@ -334,11 +334,18 @@ impl<'a> UpdateChannel<'a> { /// Set to 0 for no limit. Limit can otherwise be between 1 and 99 /// inclusive. See [Discord Docs/Modify Channel]. /// + /// # Errors + /// + /// Returns an error of type [`UserLimitInvalid`] if the bitrate is invalid. + /// /// [Discord Docs/Modify Channel]: https://discord.com/developers/docs/resources/channel#modify-channel-json-params-guild-channel - pub const fn user_limit(mut self, user_limit: u16) -> Self { + /// [`UserLimitInvalid`]: twilight_validate::channel::ChannelValidationErrorType::UserLimitInvalid + pub fn user_limit(mut self, user_limit: u16) -> Result { + validate_user_limit(user_limit)?; + self.fields.user_limit = Some(user_limit); - self + Ok(self) } /// Set the [`VideoQualityMode`] for the voice channel. diff --git a/twilight-validate/src/channel.rs b/twilight-validate/src/channel.rs index f9e1a279a05..7fee8b6cec2 100644 --- a/twilight-validate/src/channel.rs +++ b/twilight-validate/src/channel.rs @@ -24,6 +24,9 @@ pub const CHANNEL_RATE_LIMIT_PER_USER_MAX: u16 = 21_600; /// Maximum length of a channel's topic. pub const CHANNEL_TOPIC_LENGTH_MAX: usize = 1024; +/// Maximum user limit of an audio channel. +pub const CHANNEL_USER_LIMIT_MAX: u16 = 99; + /// Returned when the channel can not be updated as configured. #[derive(Debug)] pub struct ChannelValidationError { @@ -79,6 +82,11 @@ impl Display for ChannelValidationError { f.write_str(" is not a thread") } + ChannelValidationErrorType::UserLimitInvalid => { + f.write_str("user limit is greater than ")?; + + Display::fmt(&CHANNEL_USER_LIMIT_MAX, f) + } } } } @@ -108,6 +116,8 @@ pub enum ChannelValidationErrorType { /// Provided type. kind: ChannelType, }, + /// User limit is greater than 99. + UserLimitInvalid, } /// Ensure a channel's bitrate is collect. @@ -236,6 +246,25 @@ pub fn topic(value: impl AsRef) -> Result<(), ChannelValidationError> { } } +/// Ensure a channel's user limit is correct. +/// +/// Must be at least 99. +/// +/// # Errors +/// +/// Returns an error of type [`UserLimitInvalid`] if the UserLimit is invalid. +/// +/// [`UserLimitInvalid`]: ChannelValidationErrorType::BitrateInvalid +pub const fn user_limit(value: u16) -> Result<(), ChannelValidationError> { + if value <= CHANNEL_USER_LIMIT_MAX { + Ok(()) + } else { + Err(ChannelValidationError { + kind: ChannelValidationErrorType::UserLimitInvalid, + }) + } +} + #[cfg(test)] mod tests { use super::*; @@ -281,4 +310,14 @@ mod tests { assert!(topic("a".repeat(1_025)).is_err()); } + + #[test] + fn user_limit() { + assert!(super::user_limit(0).is_ok()); + assert!(super::user_limit(99).is_ok()); + assert!(matches!( + super::user_limit(100).unwrap_err().kind(), + ChannelValidationErrorType::UserLimitInvalid + )); + } } From e0bf2566014894dd7fc6edc16feacd53d389e19f Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Thu, 19 Jan 2023 13:55:14 -0500 Subject: [PATCH 2/3] clippy --- twilight-validate/src/channel.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twilight-validate/src/channel.rs b/twilight-validate/src/channel.rs index 7fee8b6cec2..5815244fcb8 100644 --- a/twilight-validate/src/channel.rs +++ b/twilight-validate/src/channel.rs @@ -252,7 +252,7 @@ pub fn topic(value: impl AsRef) -> Result<(), ChannelValidationError> { /// /// # Errors /// -/// Returns an error of type [`UserLimitInvalid`] if the UserLimit is invalid. +/// Returns an error of type [`UserLimitInvalid`] if the user limit is invalid. /// /// [`UserLimitInvalid`]: ChannelValidationErrorType::BitrateInvalid pub const fn user_limit(value: u16) -> Result<(), ChannelValidationError> { From 0d1fa4ea647c1e2caa9f4b4d49e75b5d0b61e10d Mon Sep 17 00:00:00 2001 From: Zeyla Date: Fri, 20 Jan 2023 11:11:58 -0500 Subject: [PATCH 3/3] Update twilight-validate/src/channel.rs Co-authored-by: Erk --- twilight-validate/src/channel.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twilight-validate/src/channel.rs b/twilight-validate/src/channel.rs index 5815244fcb8..06cfaedf51e 100644 --- a/twilight-validate/src/channel.rs +++ b/twilight-validate/src/channel.rs @@ -248,7 +248,7 @@ pub fn topic(value: impl AsRef) -> Result<(), ChannelValidationError> { /// Ensure a channel's user limit is correct. /// -/// Must be at least 99. +/// Must be at most 99. /// /// # Errors ///