Skip to content

Commit

Permalink
Add PhoneNumber::number_type method (#57)
Browse files Browse the repository at this point in the history
* Add PhoneNumber::number_type method
* Add tests for PhoneNumber::number_type

Co-authored-by: Ruben De Smet <[email protected]>
  • Loading branch information
tom25519 and rubdos authored Jun 7, 2023
1 parent 23640df commit 03a4b04
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/phone_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ impl PhoneNumber {
pub fn is_valid_with(&self, database: &Database) -> bool {
validator::is_valid_with(database, self)
}

/// Determine the [`Type`] of the phone number.
pub fn number_type(&self, database: &Database) -> Type {
match self.metadata(database) {
Some(metadata) => validator::number_type(metadata, &self.national.value.to_string()),
None => Type::Unknown,
}
}
}

impl<'a> Country<'a> {
Expand All @@ -244,7 +252,9 @@ impl<'a> Deref for Country<'a> {
#[cfg(test)]
mod test {
use crate::country;
use crate::metadata::DATABASE;
use crate::parser;
use crate::Type;

#[test]
fn country_id() {
Expand Down Expand Up @@ -289,4 +299,28 @@ mod test {
.unwrap()
);
}

#[test]
fn number_type() {
assert_eq!(
Type::FixedLine,
parser::parse(None, "+441212345678")
.unwrap()
.number_type(&DATABASE)
);

assert_eq!(
Type::Mobile,
parser::parse(None, "+34612345678")
.unwrap()
.number_type(&DATABASE)
);

assert_eq!(
Type::PremiumRate,
parser::parse(None, "+611900123456")
.unwrap()
.number_type(&DATABASE)
);
}
}

0 comments on commit 03a4b04

Please sign in to comment.