derive PartialEq and Eq for Atom and Atom related types

This commit is contained in:
John Turner
2025-11-23 05:18:30 +00:00
parent 5be1e5c37a
commit 86e2b4559a

View File

@@ -34,11 +34,10 @@ pub struct Category(#[get(method = "get", kind = "deref")] String);
#[derive(Clone, Debug, PartialEq, Eq, Hash, Get)] #[derive(Clone, Debug, PartialEq, Eq, Hash, Get)]
pub struct Name(#[get(method = "get", kind = "deref")] String); pub struct Name(#[get(method = "get", kind = "deref")] String);
#[derive(Clone, Debug, Hash, Get)] #[derive(Clone, Debug, PartialEq, Eq, Hash, Get)]
pub struct VersionNumber(#[get(method = "get", kind = "deref")] String); pub struct VersionNumber(#[get(method = "get", kind = "deref")] String);
#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Get)]
#[derive(Debug, Clone, Hash, Get)]
struct VersionNumbers(#[get(method = "get", kind = "deref")] Vec<VersionNumber>); struct VersionNumbers(#[get(method = "get", kind = "deref")] Vec<VersionNumber>);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -50,22 +49,19 @@ pub enum VersionSuffixKind {
P, P,
} }
#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Clone, Debug, Hash, PartialEq, Eq, Get)]
#[derive(Clone, Debug, Hash, Get)]
pub struct VersionSuffix { pub struct VersionSuffix {
kind: VersionSuffixKind, kind: VersionSuffixKind,
number: Option<VersionNumber>, number: Option<VersionNumber>,
} }
#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Get)]
#[derive(Debug, Clone, Hash, Get)]
pub struct VersionSuffixes(#[get(method = "get", kind = "deref")] Vec<VersionSuffix>); pub struct VersionSuffixes(#[get(method = "get", kind = "deref")] Vec<VersionSuffix>);
#[derive(Debug, Clone, Get, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Get, PartialEq, Eq, Hash)]
pub struct BuildId(#[get(method = "get", kind = "deref")] String); pub struct BuildId(#[get(method = "get", kind = "deref")] String);
#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Clone, Debug, PartialEq, Eq, Hash, Get)]
#[derive(Clone, Debug, Hash, Get)]
pub struct Version { pub struct Version {
numbers: VersionNumbers, numbers: VersionNumbers,
letter: Option<char>, letter: Option<char>,
@@ -231,21 +227,6 @@ impl Ord for BuildId {
} }
} }
impl PartialEq for VersionSuffix {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
&& match (&self.number, &other.number) {
(Some(a), Some(b)) => a.0 == b.0,
(Some(a), None) if a.get().chars().all(|c| c == '0') => true,
(None, Some(b)) if b.get().chars().all(|c| c == '0') => true,
(Some(_), None) | (None, Some(_)) => false,
(None, None) => true,
}
}
}
impl Eq for VersionSuffix {}
impl PartialOrd for VersionSuffix { impl PartialOrd for VersionSuffix {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
@@ -269,23 +250,6 @@ impl Ord for VersionSuffix {
} }
} }
impl PartialEq for VersionSuffixes {
fn eq(&self, other: &Self) -> bool {
let mut a = self.get().iter();
let mut b = other.get().iter();
loop {
match (a.next(), b.next()) {
(Some(a), Some(b)) if a == b => (),
(Some(_) | None, Some(_)) | (Some(_), None) => break false,
(None, None) => break true,
}
}
}
}
impl Eq for VersionSuffixes {}
impl PartialOrd for VersionSuffixes { impl PartialOrd for VersionSuffixes {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
@@ -316,36 +280,6 @@ impl Ord for VersionSuffixes {
} }
} }
impl PartialEq for VersionNumbers {
fn eq(&self, other: &Self) -> bool {
self.get()
.first()
.unwrap()
.cmp_as_ints(other.get().first().unwrap())
== Ordering::Equal
&& {
let mut a = self.get().iter().skip(1);
let mut b = other.get().iter().skip(1);
loop {
match (a.next(), b.next()) {
(Some(a), Some(b)) => {
let Ordering::Equal = a.cmp_as_str(b) else {
return false;
};
}
(Some(a), None) if a.get().chars().all(|c| c == '0') => (),
(None, Some(b)) if b.get().chars().all(|c| c == '0') => (),
(None, None) => break true,
_ => break false,
}
}
}
}
}
impl Eq for VersionNumbers {}
impl PartialOrd for VersionNumbers { impl PartialOrd for VersionNumbers {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
@@ -384,28 +318,6 @@ impl Ord for VersionNumbers {
} }
} }
impl PartialEq for Version {
fn eq(&self, other: &Self) -> bool {
self.numbers == other.numbers
&& self.suffixes == other.suffixes
&& self.letter == other.letter
&& match (&self.rev, &other.rev) {
(Some(a), Some(b)) => matches!(a.cmp_as_ints(b), Ordering::Equal),
(Some(a), None) if a.get().chars().all(|c| c == '0') => true,
(None, Some(b)) if b.get().chars().all(|c| c == '0') => true,
(Some(_), None) | (None, Some(_)) => false,
(None, None) => true,
}
&& match (&self.build_id, &other.build_id) {
(Some(a), Some(b)) => a == b,
(Some(_), None) | (None, Some(_)) => false,
(None, None) => true,
}
}
}
impl Eq for Version {}
impl PartialOrd for Version { impl PartialOrd for Version {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
@@ -774,38 +686,6 @@ mod test {
assert_eq!(atom.to_string().as_str(), s); assert_eq!(atom.to_string().as_str(), s);
} }
#[test]
fn test_version_suffix_eq() {
let a = VersionSuffix::parser()
.parse_finished(InputIter::new("alpha0"))
.unwrap();
let b = VersionSuffix::parser()
.parse_finished(InputIter::new("alpha"))
.unwrap();
assert_eq_display!(a, b);
}
#[test]
fn test_version_eq() {
let versions = [
("1", "1"),
("1", "1.0.0"),
("1.0", "1.0.0"),
("1.0.0_alpha0", "1.0.0_alpha"),
("1.0.0", "1.0.0-r0"),
];
for (a, b) in versions.map(|(a, b)| {
(
Version::parser().parse_finished(InputIter::new(a)).unwrap(),
Version::parser().parse_finished(InputIter::new(b)).unwrap(),
)
}) {
assert_eq_display!(a, b);
}
}
#[test] #[test]
fn test_version_cmp() { fn test_version_cmp() {
let versions = [ let versions = [