Skip to content

Commit

Permalink
Clean up: Derive PartialEq instead of implementing
Browse files Browse the repository at this point in the history
I previously wrote PartialEq implementations for `Color` and structs
that used it, but deriving it seems to work fine now.
  • Loading branch information
joshuamegnauth54 committed Jun 28, 2023
1 parent 5ad2626 commit d35c578
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 87 deletions.
31 changes: 3 additions & 28 deletions src/gui/styles/types/color_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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(
Expand All @@ -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,
};
Expand Down Expand Up @@ -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::<DelegateTest>(
Expand All @@ -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))
}
}
61 changes: 2 additions & 59 deletions src/gui/styles/types/custom_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)]
Expand All @@ -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.
Expand Down

0 comments on commit d35c578

Please sign in to comment.