Skip to content

Commit

Permalink
Remove PartialEq implementations from all builders (serenity-rs#2734)
Browse files Browse the repository at this point in the history
Because builder fields are all private, it doesn't make sense to compare
them directly. From a user's perspective, they have no idea what the
builder might contain. This also lets us change their internals easier,
without worrying about equality.
  • Loading branch information
mkrasnitski authored and GnomedDev committed Mar 13, 2024
1 parent f83ae5c commit 8500e5b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/builder/create_allowed_mentions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl ParseAction {
/// ```
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#allowed-mentions-object).
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
pub struct CreateAllowedMentions<'a> {
parse: ArrayVec<ParseValue, 3>,
Expand Down
8 changes: 4 additions & 4 deletions src/builder/create_attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::model::id::AttachmentId;
/// [Discord docs](https://discord.com/developers/docs/resources/channel#attachment-object-attachment-structure).
///
/// [`send_files`]: crate::model::id::ChannelId::send_files
#[derive(Clone, Debug, Serialize, PartialEq)]
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
#[must_use]
pub struct CreateAttachment<'a> {
Expand Down Expand Up @@ -112,12 +112,12 @@ impl<'a> CreateAttachment<'a> {
}
}

#[derive(Debug, Clone, serde::Serialize, PartialEq)]
#[derive(Debug, Clone, serde::Serialize)]
struct ExistingAttachment {
id: AttachmentId,
}

#[derive(Debug, Clone, serde::Serialize, PartialEq)]
#[derive(Debug, Clone, serde::Serialize)]
#[serde(untagged)]
enum NewOrExisting<'a> {
New(CreateAttachment<'a>),
Expand Down Expand Up @@ -179,7 +179,7 @@ enum NewOrExisting<'a> {
///
/// Internally, this type is used not just for message editing endpoints, but also for message
/// creation endpoints.
#[derive(Default, Debug, Clone, serde::Serialize, PartialEq)]
#[derive(Default, Debug, Clone, serde::Serialize)]
#[serde(transparent)]
#[must_use]
pub struct EditAttachments<'a> {
Expand Down
12 changes: 6 additions & 6 deletions src/builder/create_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::model::prelude::*;
/// A builder for creating a components action row in a message.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#component-object).
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
#[must_use]
pub enum CreateActionRow<'a> {
Buttons(Vec<CreateButton<'a>>),
Expand All @@ -34,7 +34,7 @@ impl<'a> serde::Serialize for CreateActionRow<'a> {
}

/// A builder for creating a button component in a message
#[derive(Clone, Debug, Serialize, PartialEq)]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateButton<'a> {
style: ButtonStyle,
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Serialize for CreateSelectMenuDefault {
}

/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure).
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
pub enum CreateSelectMenuKind<'a> {
String {
options: Cow<'a, [CreateSelectMenuOption<'a>]>,
Expand Down Expand Up @@ -226,7 +226,7 @@ impl<'a> Serialize for CreateSelectMenuKind<'a> {
/// A builder for creating a select menu component in a message
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure).
#[derive(Clone, Debug, Serialize, PartialEq)]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateSelectMenu<'a> {
custom_id: Cow<'a, str>,
Expand Down Expand Up @@ -292,7 +292,7 @@ impl<'a> CreateSelectMenu<'a> {
/// A builder for creating an option of a select menu component in a message
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure)
#[derive(Clone, Debug, Serialize, PartialEq)]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateSelectMenuOption<'a> {
label: Cow<'a, str>,
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<'a> CreateSelectMenuOption<'a> {
/// A builder for creating an input text component in a modal
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure).
#[derive(Clone, Debug, Serialize, PartialEq)]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateInputText<'a> {
#[serde(rename = "type")]
Expand Down
10 changes: 5 additions & 5 deletions src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::model::prelude::*;
/// A builder to create an embed in a message
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object)
#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateEmbed<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<'a> From<Embed> for CreateEmbed<'a> {
}

/// A builder to create the author data of an emebd. See [`CreateEmbed::author`]
#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateEmbedAuthor<'a> {
name: Cow<'a, str>,
Expand Down Expand Up @@ -306,7 +306,7 @@ impl<'a> From<EmbedAuthor> for CreateEmbedAuthor<'a> {
}

/// A builder to create the footer data for an embed. See [`CreateEmbed::footer`]
#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateEmbedFooter<'a> {
text: Cow<'a, str>,
Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'a> From<EmbedFooter> for CreateEmbedFooter<'a> {
}
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, Serialize)]
struct CreateEmbedField<'a> {
name: Cow<'a, str>,
value: Cow<'a, str>,
Expand All @@ -371,7 +371,7 @@ impl<'a> From<EmbedField> for CreateEmbedField<'a> {
}
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, Serialize)]
struct CreateEmbedImage<'a> {
url: Cow<'a, str>,
}
Expand Down
17 changes: 13 additions & 4 deletions src/builder/edit_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::model::prelude::*;
/// ```
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#edit-message)
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
pub struct EditMessage<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -204,6 +204,17 @@ impl<'a> EditMessage<'a> {
}
}

impl EditMessage<'_> {
fn is_only_suppress_embeds(&self) -> bool {
self.flags == Some(MessageFlags::SUPPRESS_EMBEDS)
&& self.content.is_none()
&& self.embeds.is_none()
&& self.allowed_mentions.is_none()
&& self.components.is_none()
&& self.attachments.is_none()
}
}

#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for EditMessage<'_> {
Expand Down Expand Up @@ -243,9 +254,7 @@ impl Builder for EditMessage<'_> {
#[cfg(feature = "cache")]
if let Some(user_id) = ctx.2 {
if let Some(cache) = cache_http.cache() {
let reference_builder = EditMessage::new().suppress_embeds(true);

if user_id != cache.current_user().id && self != reference_builder {
if user_id != cache.current_user().id && !self.is_only_suppress_embeds() {
return Err(Error::Model(ModelError::InvalidUser));
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/model/channel/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::model::{Colour, Timestamp};
///
/// [slack's attachments]: https://api.slack.com/docs/message-attachments
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Embed {
/// The title of the embed.
Expand Down Expand Up @@ -76,7 +76,7 @@ pub struct Embed {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct EmbedAuthor {
/// The name of the author.
Expand All @@ -98,7 +98,7 @@ pub struct EmbedAuthor {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct EmbedField {
/// The name of the field.
Expand Down Expand Up @@ -144,7 +144,7 @@ impl EmbedField {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct EmbedFooter {
/// The associated text with the footer.
Expand All @@ -163,7 +163,7 @@ pub struct EmbedFooter {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct EmbedImage {
/// Source URL of the image.
Expand All @@ -182,7 +182,7 @@ pub struct EmbedImage {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-provider-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct EmbedProvider {
/// The name of the provider.
Expand All @@ -195,7 +195,7 @@ pub struct EmbedProvider {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct EmbedThumbnail {
/// The source URL of the thumbnail.
Expand All @@ -214,7 +214,7 @@ pub struct EmbedThumbnail {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-video-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct EmbedVideo {
/// The source URL of the video.
Expand Down

0 comments on commit 8500e5b

Please sign in to comment.