From 161859e103e9cdff76e2dcfbc236cc7dd73b4fb3 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sun, 7 Jul 2024 16:59:56 +0200 Subject: [PATCH 1/2] Return unparseable country code as error instead of silent failure Overrides the behaviour of https://github.com/whisperfish/rust-phonenumber/pull/54 Co-authored-by: Regis David Souza Mesquita Co-authored-by: Raphael Costa --- src/phone_number.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/phone_number.rs b/src/phone_number.rs index 1139520..8907324 100644 --- a/src/phone_number.rs +++ b/src/phone_number.rs @@ -236,8 +236,12 @@ impl<'a> Country<'a> { self.0.code.value() } - pub fn id(&self) -> Option { - self.0.metadata(&DATABASE).and_then(|m| m.id().parse().ok()) + pub fn id(&self) -> Result, crate::error::Parse> { + self.0 + .metadata(&DATABASE) + .map(|m| m.id().parse()) + .transpose() + .map_err(|_e| crate::error::Parse::InvalidCountryCode) } } From db2b29ff992ce311d43dc4196190dfe7393d9d7b Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sun, 7 Jul 2024 17:11:22 +0200 Subject: [PATCH 2/2] Make test compliant with new country_id method --- src/phone_number.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/phone_number.rs b/src/phone_number.rs index 8907324..418cf99 100644 --- a/src/phone_number.rs +++ b/src/phone_number.rs @@ -299,7 +299,10 @@ mod test { #[case] country: Option, #[case] _type: Type, ) -> anyhow::Result<()> { - assert_eq!(country, number.country().id()); + // Flatten the error into an Option. + // XXX: We can't currently distinguish an error case from an unavailable case, given the + // current phone number list in rstest above. + assert_eq!(country, number.country().id().ok().flatten()); Ok(()) }