Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual implementation of PartialEq for Code #72

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/country.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<H: std::hash::Hasher>(&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)]
Expand Down
16 changes: 15 additions & 1 deletion src/phone_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
Loading