diff --git a/src/country.rs b/src/country.rs index 3c6c471..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, Eq, PartialEq, Serialize, Deserialize, Hash, Debug)] +#[derive(Copy, Clone, Serialize, Deserialize, Debug)] pub struct Code { /// The country code value. pub(crate) value: u16, @@ -27,6 +27,28 @@ 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 {} + +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)] 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); + } }