diff --git a/src/phone_number.rs b/src/phone_number.rs index 1139520..418cf99 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) } } @@ -295,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(()) }