From 0807ab49ec0a9673b9fa024654f8258028aeb330 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sun, 7 Jul 2024 16:26:44 +0200 Subject: [PATCH 1/3] Manual implementation of PartialEq for Code --- src/country.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/country.rs b/src/country.rs index 3c6c471..c45aaaa 100644 --- a/src/country.rs +++ b/src/country.rs @@ -18,7 +18,7 @@ use serde_derive::{Deserialize, Serialize}; use std::str; use strum::{AsRefStr, EnumString}; -#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Hash, Debug)] +#[derive(Copy, Clone, Serialize, Deserialize, Hash, Debug)] pub struct Code { /// The country code value. pub(crate) value: u16, @@ -27,6 +27,18 @@ pub struct Code { pub(crate) source: Source, } +impl PartialEq for Code { + /// Compare two country codes. + /// + /// This implementation is necessary because the `source` field is not + /// relevant for equality. + fn eq(&self, other: &Self) -> bool { + self.value == other.value + } +} + +impl Eq for Code {} + /// The source from which the country code is derived. This is not set in the /// general parsing method, but in the method that parses and keeps raw_input. #[derive(Eq, PartialEq, Copy, Clone, Serialize, Deserialize, Hash, Debug)] From 273a65b03fb8d5b12e780156fa6bedae26152fb7 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sun, 7 Jul 2024 16:29:16 +0200 Subject: [PATCH 2/3] Update test cases for equality testing --- src/phone_number.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/phone_number.rs b/src/phone_number.rs index 1139520..a8999d2 100644 --- a/src/phone_number.rs +++ b/src/phone_number.rs @@ -316,8 +316,14 @@ mod test { }; let formatted = number.format().mode(mode).to_string(); + + if mode == Mode::National && country_hint.is_none() { + // If we are in national mode, we need to have a country hint + return Ok(()); + } + let parsed = parser::parse(country_hint, &formatted).with_context(|| { - format!("parsing {number} after formatting in {mode:?} mode as {formatted}") + format!("parsing {number} with country hint {country_hint:?} after formatting in {mode:?} mode as {formatted}") })?; // impl Eq for PhoneNumber does not consider differently parsed phone numbers to be equal. @@ -336,4 +342,12 @@ mod test { ) { assert_eq!(r#type, number.number_type(&DATABASE)); } + + #[test] + fn equality() { + let a = crate::parse(None, "+32474091150").unwrap(); + let b = crate::parse(Some(BE), "0474091150").unwrap(); + + assert_eq!(a, b); + } } From f29250fe572227953f08566dab0b510e41330211 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sun, 7 Jul 2024 16:49:49 +0200 Subject: [PATCH 3/3] Manual implementation of Hash for Code --- src/country.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/country.rs b/src/country.rs index c45aaaa..0b33cc4 100644 --- a/src/country.rs +++ b/src/country.rs @@ -15,10 +15,10 @@ //! Country related types. use serde_derive::{Deserialize, Serialize}; -use std::str; +use std::{hash::Hash, str}; use strum::{AsRefStr, EnumString}; -#[derive(Copy, Clone, Serialize, Deserialize, Hash, Debug)] +#[derive(Copy, Clone, Serialize, Deserialize, Debug)] pub struct Code { /// The country code value. pub(crate) value: u16, @@ -39,6 +39,16 @@ impl PartialEq for Code { impl Eq for Code {} +impl Hash for Code { + /// Hash the country code. + /// + /// This implementation is necessary because the `source` field is not + /// relevant for hashing, and this should be consistent with Eq/PartialEq. + fn hash(&self, state: &mut H) { + self.value.hash(state) + } +} + /// The source from which the country code is derived. This is not set in the /// general parsing method, but in the method that parses and keeps raw_input. #[derive(Eq, PartialEq, Copy, Clone, Serialize, Deserialize, Hash, Debug)]