From d35c578585dd2ac77132658b55e0f18f34251e71 Mon Sep 17 00:00:00 2001 From: Joshua Megnauth Date: Tue, 27 Jun 2023 23:35:55 -0400 Subject: [PATCH] Clean up: Derive PartialEq instead of implementing I previously wrote PartialEq implementations for `Color` and structs that used it, but deriving it seems to work fine now. --- src/gui/styles/types/color_remote.rs | 31 ++------------ src/gui/styles/types/custom_style.rs | 61 +--------------------------- 2 files changed, 5 insertions(+), 87 deletions(-) diff --git a/src/gui/styles/types/color_remote.rs b/src/gui/styles/types/color_remote.rs index 8cff1be9..5380da68 100644 --- a/src/gui/styles/types/color_remote.rs +++ b/src/gui/styles/types/color_remote.rs @@ -104,16 +104,10 @@ where serializer.serialize_str(&hex_color) } -// Compare [iced::Color] as RGBA. -pub(super) fn color_partialeq(color: Color, other: Color) -> bool { - let color = color.into_rgba8(); - let other = other.into_rgba8(); - color.into_iter().eq(other.into_iter()) -} #[cfg(test)] mod tests { - use super::{color_partialeq, deserialize_color, serialize_color}; + use super::{deserialize_color, serialize_color}; use iced::Color; use serde::{Deserialize, Serialize}; use serde_test::{assert_de_tokens_error, assert_tokens, Token}; @@ -135,7 +129,7 @@ mod tests { a: 128.0 / 255.0, }; - #[derive(Debug, Deserialize, Serialize)] + #[derive(Debug, PartialEq, Deserialize, Serialize)] #[serde(transparent)] struct DelegateTest { #[serde( @@ -146,12 +140,6 @@ mod tests { color: Color, } - impl PartialEq for DelegateTest { - fn eq(&self, other: &Self) -> bool { - color_partialeq(self.color, other.color) - } - } - const CATPPUCCIN_PINK_DELEGATE: DelegateTest = DelegateTest { color: CATPPUCCIN_PINK, }; @@ -202,6 +190,7 @@ mod tests { ); } + // A hex string that is too long shouldn't deserialize #[test] fn test_len_too_large_color_de() { assert_de_tokens_error::( @@ -218,18 +207,4 @@ mod tests { "invalid value: string \"#ca🐈\", expected valid hexadecimal", ); } - - // Test color equality - #[test] - fn test_color_partialeq() { - let color = Color { - r: 1.0 / 3.0, - g: 2.0 / 3.0, - b: 3.0 / 3.0, - #[allow(clippy::excessive_precision)] - a: 1.618033988749, - }; - let other = color; - assert!(color_partialeq(color, other)) - } } diff --git a/src/gui/styles/types/custom_style.rs b/src/gui/styles/types/custom_style.rs index 79a483b8..30e9af52 100644 --- a/src/gui/styles/types/custom_style.rs +++ b/src/gui/styles/types/custom_style.rs @@ -35,10 +35,7 @@ use std::{ io::{BufReader, Read}, }; -use super::{ - color_remote::color_partialeq, - palette::{Palette, PaletteExtension}, -}; +use super::palette::{Palette, PaletteExtension}; use crate::Language; /// Custom color scheme data including the palette, name, and location of the toml. @@ -99,7 +96,7 @@ impl CustomStyle { // Clippy complains about deriving [Hash] with a manually written [PartialEq]. We manually implemented // Hash for [Palette] and [PaletteExtension], so deriving Hash is convenient and the error is spurious. #[allow(clippy::derived_hash_with_manual_eq)] -#[derive(Debug, Hash, Clone, Deserialize, Serialize)] +#[derive(Debug, Hash, Clone, PartialEq, Deserialize, Serialize)] pub struct CustomPalette { /// Base colors as used for the default sniffnet themes. #[serde(flatten)] @@ -109,60 +106,6 @@ pub struct CustomPalette { pub extension: PaletteExtension, } -impl PartialEq for CustomPalette { - fn eq(&self, other: &Self) -> bool { - let Palette { - primary, - secondary, - buttons, - incoming, - outgoing, - text_headers, - text_body, - round_borders, - round_containers, - } = self.base; - - let PaletteExtension { - starred, - badge_alpha, - color_mix_chart, - } = self.extension; - - // Other - let Palette { - primary: primary_other, - secondary: secondary_other, - buttons: buttons_other, - incoming: incoming_other, - outgoing: outgoing_other, - text_headers: text_headers_other, - text_body: text_body_other, - round_borders: round_borders_other, - round_containers: round_containers_other, - } = other.base; - - let PaletteExtension { - starred: starred_other, - badge_alpha: badge_alpha_other, - color_mix_chart: color_mix_chart_other, - } = other.extension; - - color_partialeq(primary, primary_other) - && color_partialeq(secondary, secondary_other) - && color_partialeq(buttons, buttons_other) - && color_partialeq(incoming, incoming_other) - && color_partialeq(outgoing, outgoing_other) - && color_partialeq(text_headers, text_headers_other) - && color_partialeq(text_body, text_body_other) - && color_partialeq(round_borders, round_borders_other) - && color_partialeq(round_containers, round_containers_other) - && color_partialeq(starred, starred_other) - && badge_alpha == badge_alpha_other - && color_mix_chart == color_mix_chart_other - } -} - /// Deserialize [CustomStyle] from a file path. /// /// This is implemented by first deserializing a file path which in turn contains the style as TOML.