Skip to content
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
13 changes: 6 additions & 7 deletions src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ impl<'a> CreateEmbed<'a> {
}

#[cfg(feature = "http")]
pub(super) fn check_length(&self) -> Result<()> {
pub(super) fn get_length(&self) -> usize {
let mut length = 0;
if let Some(ref author) = self.author {
if let Some(author) = &self.author {
length += author.name.chars().count();
}

if let Some(ref description) = self.description {
if let Some(description) = &self.description {
length += description.chars().count();
}

Expand All @@ -218,16 +218,15 @@ impl<'a> CreateEmbed<'a> {
length += field.value.chars().count();
}

if let Some(ref footer) = self.footer {
if let Some(footer) = &self.footer {
length += footer.text.chars().count();
}

if let Some(ref title) = self.title {
if let Some(title) = &self.title {
length += title.chars().count();
}

super::check_overflow(length, crate::constants::EMBED_MAX_LENGTH)
.map_err(|overflow| Error::Model(ModelError::EmbedTooLarge(overflow)))
length
}
}

Expand Down
23 changes: 5 additions & 18 deletions src/builder/create_interaction_response.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use super::{check_overflow, Builder};
use super::Builder;
use super::{
CreateActionRow,
CreateAllowedMentions,
Expand All @@ -10,8 +10,6 @@ use super::{
EditAttachments,
};
#[cfg(feature = "http")]
use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
use crate::internal::prelude::*;
use crate::json::{self, json};
Expand Down Expand Up @@ -101,26 +99,15 @@ impl serde::Serialize for CreateInteractionResponse<'_> {

impl CreateInteractionResponse<'_> {
#[cfg(feature = "http")]
fn check_length(&self) -> Result<()> {
fn check_length(&self) -> Result<(), ModelError> {
if let CreateInteractionResponse::Message(data)
| CreateInteractionResponse::Defer(data)
| CreateInteractionResponse::UpdateMessage(data) = self
{
if let Some(content) = &data.content {
check_overflow(content.chars().count(), constants::MESSAGE_CODE_LIMIT)
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}

if let Some(embeds) = &data.embeds {
check_overflow(embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;

for embed in embeds.iter() {
embed.check_length()?;
}
}
super::check_lengths(data.content.as_deref(), data.embeds.as_deref(), 0)
} else {
Ok(())
}
Ok(())
}
}

Expand Down
19 changes: 3 additions & 16 deletions src/builder/create_interaction_response_followup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use super::{check_overflow, Builder};
use super::Builder;
use super::{
CreateActionRow,
CreateAllowedMentions,
Expand All @@ -10,8 +10,6 @@ use super::{
EditAttachments,
};
#[cfg(feature = "http")]
use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
Expand Down Expand Up @@ -44,19 +42,8 @@ impl<'a> CreateInteractionResponseFollowup<'a> {
}

#[cfg(feature = "http")]
fn check_length(&self) -> Result<()> {
if let Some(content) = &self.content {
check_overflow(content.chars().count(), constants::MESSAGE_CODE_LIMIT)
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}

check_overflow(self.embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in self.embeds.iter() {
embed.check_length()?;
}

Ok(())
fn check_length(&self) -> Result<(), ModelError> {
super::check_lengths(self.content.as_deref(), Some(&self.embeds), 0)
}

/// Set the content of the message.
Expand Down
24 changes: 4 additions & 20 deletions src/builder/create_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use super::{check_overflow, Builder};
use super::Builder;
use super::{
CreateActionRow,
CreateAllowedMentions,
Expand All @@ -10,8 +10,6 @@ use super::{
EditAttachments,
};
#[cfg(feature = "http")]
use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
Expand Down Expand Up @@ -82,22 +80,8 @@ impl<'a> CreateMessage<'a> {
}

#[cfg(feature = "http")]
fn check_length(&self) -> Result<()> {
if let Some(content) = &self.content {
check_overflow(content.chars().count(), constants::MESSAGE_CODE_LIMIT)
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}

check_overflow(self.embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in self.embeds.iter() {
embed.check_length()?;
}

check_overflow(self.sticker_ids.len(), constants::STICKER_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::StickerAmount))?;

Ok(())
fn check_length(&self) -> Result<(), ModelError> {
super::check_lengths(self.content.as_deref(), Some(&self.embeds), self.sticker_ids.len())
}

/// Set the content of the message.
Expand Down Expand Up @@ -292,7 +276,7 @@ impl Builder for CreateMessage<'_> {
///
/// # Errors
///
/// Returns a [`ModelError::MessageTooLong`] if the message contents are over the above limits.
/// Returns a [`ModelError::TooLarge`] if the message contents are over the above limits.
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
Expand Down
11 changes: 4 additions & 7 deletions src/builder/create_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ impl<'a> Builder for CreateWebhook<'a> {
/// If the `cache` is enabled, returns [`ModelError::InvalidChannelType`] if the corresponding
/// channel is not of type [`Text`] or [`News`].
///
/// If the provided name is less than 2 characters, returns [`ModelError::NameTooShort`]. If it
/// is more than 100 characters, returns [`ModelError::NameTooLong`].
/// If the provided name is less than 2 characters, returns [`ModelError::TooSmall`]. If it
/// is more than 100 characters, returns [`ModelError::TooLarge`].
///
/// Returns a [`Error::Http`] if the current user lacks permission, or if invalid data is
/// given.
Expand All @@ -92,11 +92,8 @@ impl<'a> Builder for CreateWebhook<'a> {
}
}

if self.name.len() < 2 {
return Err(Error::Model(ModelError::NameTooShort));
} else if self.name.len() > 100 {
return Err(Error::Model(ModelError::NameTooLong));
}
crate::model::error::Minimum::WebhookName.check_underflow(self.name.chars().count())?;
crate::model::error::Maximum::WebhookName.check_overflow(self.name.chars().count())?;

cache_http.http().create_webhook(ctx, &self, self.audit_log_reason).await
}
Expand Down
23 changes: 4 additions & 19 deletions src/builder/edit_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use super::{check_overflow, Builder};
use super::Builder;
use super::{
CreateActionRow,
CreateAllowedMentions,
Expand All @@ -10,8 +10,6 @@ use super::{
EditAttachments,
};
#[cfg(feature = "http")]
use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
Expand Down Expand Up @@ -61,21 +59,8 @@ impl<'a> EditMessage<'a> {
}

#[cfg(feature = "http")]
fn check_length(&self) -> Result<()> {
if let Some(content) = &self.content {
check_overflow(content.chars().count(), constants::MESSAGE_CODE_LIMIT)
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}

if let Some(embeds) = &self.embeds {
check_overflow(embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in embeds.iter() {
embed.check_length()?;
}
}

Ok(())
fn check_length(&self) -> Result<(), ModelError> {
super::check_lengths(self.content.as_deref(), self.embeds.as_deref(), 0)
}

/// Set the content of the message.
Expand Down Expand Up @@ -243,7 +228,7 @@ impl Builder for EditMessage<'_> {
///
/// # Errors
///
/// Returns a [`ModelError::MessageTooLong`] if the message contents are over the above limits.
/// Returns a [`ModelError::TooLarge`] if the message contents are over the above limits.
///
/// Returns [`Error::Http`] if the user lacks permission, as well as if invalid data is given.
///
Expand Down
21 changes: 3 additions & 18 deletions src/builder/edit_webhook_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use super::{check_overflow, Builder};
use super::Builder;
use super::{
CreateActionRow,
CreateAllowedMentions,
Expand All @@ -10,8 +10,6 @@ use super::{
EditAttachments,
};
#[cfg(feature = "http")]
use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
Expand Down Expand Up @@ -45,21 +43,8 @@ impl<'a> EditWebhookMessage<'a> {
}

#[cfg(feature = "http")]
pub(crate) fn check_length(&self) -> Result<()> {
if let Some(content) = &self.content {
check_overflow(content.chars().count(), constants::MESSAGE_CODE_LIMIT)
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}

if let Some(embeds) = &self.embeds {
check_overflow(embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in embeds.iter() {
embed.check_length()?;
}
}

Ok(())
pub(crate) fn check_length(&self) -> Result<(), ModelError> {
super::check_lengths(self.content.as_deref(), self.embeds.as_deref(), 0)
}

/// Set the content of the message.
Expand Down
19 changes: 3 additions & 16 deletions src/builder/execute_webhook.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

#[cfg(feature = "http")]
use super::{check_overflow, Builder};
use super::Builder;
use super::{
CreateActionRow,
CreateAllowedMentions,
Expand All @@ -10,8 +10,6 @@ use super::{
EditAttachments,
};
#[cfg(feature = "http")]
use crate::constants;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
Expand Down Expand Up @@ -90,19 +88,8 @@ impl<'a> ExecuteWebhook<'a> {
}

#[cfg(feature = "http")]
fn check_length(&self) -> Result<()> {
if let Some(content) = &self.content {
check_overflow(content.chars().count(), constants::MESSAGE_CODE_LIMIT)
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}

check_overflow(self.embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in self.embeds.iter() {
embed.check_length()?;
}

Ok(())
fn check_length(&self) -> Result<(), ModelError> {
super::check_lengths(self.content.as_deref(), Some(&self.embeds), 0)
}

/// Override the default avatar of the webhook with an image URL.
Expand Down
26 changes: 21 additions & 5 deletions src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
#[cfg(feature = "http")]
use crate::model::ModelError;

/// Common trait for all HTTP request builders in this module.
#[cfg(feature = "http")]
Expand All @@ -28,12 +30,26 @@ pub trait Builder {
}

#[cfg(feature = "http")]
pub(crate) fn check_overflow(len: usize, max: usize) -> StdResult<(), usize> {
if len > max {
Err(len - max)
} else {
Ok(())
pub(crate) fn check_lengths(
content: Option<&str>,
embeds: Option<&[CreateEmbed<'_>]>,
stickers: usize,
) -> StdResult<(), ModelError> {
use crate::model::error::Maximum;

if let Some(content) = content {
Maximum::MessageLength.check_overflow(content.chars().count())?;
}

if let Some(embeds) = embeds {
Maximum::EmbedCount.check_overflow(embeds.len())?;

for embed in embeds {
Maximum::EmbedLength.check_overflow(embed.get_length())?;
}
}

Maximum::StickerCount.check_overflow(stickers)
}

mod add_member;
Expand Down
7 changes: 6 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! A set of constants used by the library.

use nonmax::NonMaxU16;

/// The maximum length of the textual size of an embed.
pub const EMBED_MAX_LENGTH: usize = 6000;

Expand All @@ -19,7 +21,10 @@ pub const LARGE_THRESHOLD: u8 = 250;
pub const MESSAGE_CODE_LIMIT: usize = 2000;

/// The maximum number of members the bot can fetch at once
pub const MEMBER_FETCH_LIMIT: u64 = 1000;
pub const MEMBER_FETCH_LIMIT: NonMaxU16 = match NonMaxU16::new(1000) {
Some(m) => m,
None => unreachable!(),
};

/// The [UserAgent] sent along with every request.
///
Expand Down
Loading