Skip to content

Commit

Permalink
Provide alternative implementations for PartialEq for some types when…
Browse files Browse the repository at this point in the history
… sqlx feature is enabled
  • Loading branch information
bitfl0wer committed Jul 20, 2024
1 parent 491cd25 commit 98b129a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 29 deletions.
25 changes: 24 additions & 1 deletion src/types/entities/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::types::utils::Snowflake;
use crate::types::Shared;
use crate::types::{Team, User};

#[allow(unused_imports)]
use super::{arc_rwlock_ptr_eq, option_arc_rwlock_ptr_eq};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -87,13 +88,35 @@ impl PartialEq for Application {
&& self.discovery_eligibility_flags == other.discovery_eligibility_flags
&& self.tags == other.tags
&& self.cover_image == other.cover_image
&& option_arc_rwlock_ptr_eq(&self.install_params, &other.install_params)
&& compare_install_params(&self.install_params, &other.install_params)
&& self.terms_of_service_url == other.terms_of_service_url
&& self.privacy_policy_url == other.privacy_policy_url
&& self.team == other.team
}
}

#[cfg(not(tarpaulin_include))]
#[cfg(feature = "sqlx")]
fn compare_install_params(
a: &Option<sqlx::types::Json<InstallParams>>,
b: &Option<sqlx::types::Json<InstallParams>>,
) -> bool {
match (a, b) {
(Some(a), Some(b)) => a.encode_to_string() == b.encode_to_string(),
(None, None) => true,
_ => false,
}
}

#[cfg(not(tarpaulin_include))]
#[cfg(not(feature = "sqlx"))]
fn compare_install_params(
a: &Option<Shared<InstallParams>>,
b: &Option<Shared<InstallParams>>,
) -> bool {
option_arc_rwlock_ptr_eq(a, b)
}

impl Default for Application {
fn default() -> Self {
Self {
Expand Down
69 changes: 43 additions & 26 deletions src/types/entities/audit_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::sync::Arc;
#[allow(unused_imports)]
use super::option_vec_arc_rwlock_ptr_eq;

use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
Expand Down Expand Up @@ -30,39 +31,55 @@ pub struct AuditLogEntry {
pub options: Option<AuditEntryInfo>,
pub reason: Option<String>,
}

#[cfg(not(tarpaulin_include))]
impl PartialEq for AuditLogEntry {
fn eq(&self, other: &Self) -> bool {
let everything_else = self.target_id == other.target_id
self.target_id == other.target_id
&& self.user_id == other.user_id
&& self.id == other.id
&& self.action_type == other.action_type
&& self.options == other.options
&& self.reason == other.reason;
// Compare the Vec<Shared<AuditLogChange>> separately, since Shared<T> doesn't implement PartialEq
match (&self.changes, &other.changes) {
// If both are Some, compare the contents
(Some(self_changes), Some(other_changes)) => {
let mut changes_equal = true;
// Iterate over both Vecs and compare the Arc pointers. If any are different, the
// Vecs are not equal
for (self_change, other_change) in self_changes.iter().zip(other_changes.iter()) {
if !Arc::ptr_eq(self_change, other_change) {
changes_equal = false;
break;
}
}
everything_else && changes_equal
}
// If both are None, they're equal
(None, None) => everything_else,
// If one is Some and the other is None, they're not equal
_ => false,
}
&& compare_options(&self.options, &other.options)
&& self.reason == other.reason
&& compare_changes(&self.changes, &other.changes)
}
}

#[cfg(not(tarpaulin_include))]
#[cfg(feature = "sqlx")]
fn compare_options(
a: &Option<sqlx::types::Json<AuditEntryInfo>>,
b: &Option<sqlx::types::Json<AuditEntryInfo>>,
) -> bool {
match (a, b) {
(Some(a), Some(b)) => a.encode_to_string() == b.encode_to_string(),
(None, None) => true,
_ => false,
}
}

#[cfg(not(tarpaulin_include))]
#[cfg(not(feature = "sqlx"))]
fn compare_options(a: &Option<AuditEntryInfo>, b: &Option<AuditEntryInfo>) -> bool {
a == b
}

#[cfg(not(tarpaulin_include))]
#[cfg(feature = "sqlx")]
fn compare_changes(
a: &sqlx::types::Json<Option<Vec<Shared<AuditLogChange>>>>,
b: &sqlx::types::Json<Option<Vec<Shared<AuditLogChange>>>>,
) -> bool {
a.encode_to_string() == b.encode_to_string()
}

#[cfg(not(tarpaulin_include))]
#[cfg(not(feature = "sqlx"))]
fn compare_changes(
a: &Option<Vec<Shared<AuditLogChange>>>,
b: &Option<Vec<Shared<AuditLogChange>>>,
) -> bool {
option_vec_arc_rwlock_ptr_eq(a, b)
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
/// See <https://discord.com/developers/docs/resources/audit-log#audit-log-change-object>
pub struct AuditLogChange {
Expand Down
26 changes: 25 additions & 1 deletion src/types/entities/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use sqlx::types::Json;
use std::fmt::{Debug, Formatter};
use std::str::FromStr;

Expand Down Expand Up @@ -98,6 +99,7 @@ pub struct Channel {
}

#[cfg(not(tarpaulin_include))]
#[allow(clippy::nonminimal_bool)]
impl PartialEq for Channel {
fn eq(&self, other: &Self) -> bool {
self.application_id == other.application_id
Expand Down Expand Up @@ -128,7 +130,7 @@ impl PartialEq for Channel {
&& self.nsfw == other.nsfw
&& self.owner_id == other.owner_id
&& self.parent_id == other.parent_id
&& option_vec_arc_rwlock_ptr_eq(
&& compare_permission_overwrites(
&self.permission_overwrites,
&other.permission_overwrites,
)
Expand All @@ -145,6 +147,28 @@ impl PartialEq for Channel {
}
}

#[cfg(not(tarpaulin_include))]
#[cfg(feature = "sqlx")]
fn compare_permission_overwrites(
a: &Option<Json<Vec<PermissionOverwrite>>>,
b: &Option<Json<Vec<PermissionOverwrite>>>,
) -> bool {
match (a, b) {
(Some(a), Some(b)) => a.encode_to_string() == b.encode_to_string(),
(None, None) => true,
_ => false,
}
}

#[cfg(not(tarpaulin_include))]
#[cfg(not(feature = "sqlx"))]
fn compare_permission_overrides(
a: &Option<Vec<Shared<PermissionOverwrite>>>,
b: &Option<Vec<Shared<PermissionOverwrite>>>,
) -> bool {
option_vec_arc_rwlock_ptr_eq(a, b)
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// A tag that can be applied to a thread in a [ChannelType::GuildForum] or [ChannelType::GuildMedia] channel.
///
Expand Down
3 changes: 2 additions & 1 deletion src/types/entities/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct Emoji {
}

#[cfg(not(tarpaulin_include))]
#[allow(clippy::nonminimal_bool)]
impl PartialEq for Emoji {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
Expand All @@ -62,7 +63,7 @@ impl PartialEq for Emoji {
impl From<PartialEmoji> for Emoji {
fn from(value: PartialEmoji) -> Self {
Self {
id: value.id.unwrap_or_default(), // TODO: this should be handled differently
id: value.id.unwrap_or_default(), // TODO: Make this method an impl to TryFrom<> instead
name: Some(value.name),
roles: None,
user: None,
Expand Down

0 comments on commit 98b129a

Please sign in to comment.