Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- toolchain: "nightly"
coverage: true
tests: true
- toolchain: "1.76.0"
- toolchain: "1.79.0"
coverage: false
tests: false
steps:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "phonenumber"
version = "0.3.10+9.0.33"
edition = "2021"
rust-version = "1.76.0"
rust-version = "1.79.0"

authors = ["Gabriel Féron <g@leirbag.net>", "Ruben De Smet <ruben.de.smet@rubdos.be>", "meh. <meh@1aim.com>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

Rust version of [libphonenumber](https://github.com/googlei18n/libphonenumber).
We currently require 1.76.0 as minimum supported Rust version (MSRV).
We currently require 1.79.0 as minimum supported Rust version (MSRV).

## Usage

Expand Down
49 changes: 35 additions & 14 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,27 @@ pub fn parse_with<S: AsRef<str>>(

// If no country was supplied, try to determine the metadata from the number.
// We need to determine the country to be able to classify the national prefix if present.
let meta = if let Some(country) = &country {
database.by_id(country.as_ref())
} else {
let code = country::Code {
value: number.prefix.clone().map(|p| p.parse()).unwrap_or(Ok(0))?,
source: number.country,
};

use either::{Left, Right};
let without_zeros = number.national.trim_start_matches('0');
match validator::source_for(database, code.value(), without_zeros) {
Some(Left(region)) => database.by_id(region.as_ref()),
Some(Right(code)) => database.by_code(&code).and_then(|m| m.into_iter().next()),
None => None,
// Only trust the supplied reference country when the country code did not
// come from the number itself. If the number carried its own country code
// (a leading `+`, IDD, or an extracted code), the metadata must come from
// that code, otherwise we strip the wrong national prefix.
use either::{Left, Right};
let meta = match &country {
Some(country) if number.country == country::Source::Default => {
database.by_id(country.as_ref())
}
_ => {
let code = country::Code {
value: number.prefix.clone().map(|p| p.parse()).unwrap_or(Ok(0))?,
source: number.country,
};

let without_zeros = number.national.trim_start_matches('0');
match validator::source_for(database, code.value(), without_zeros) {
Some(Left(region)) => database.by_id(region.as_ref()),
Some(Right(code)) => database.by_code(&code).and_then(|m| m.into_iter().next()),
None => None,
}
}
};

Expand Down Expand Up @@ -265,6 +272,20 @@ mod test {
assert!(res.is_err());
}

#[test]
fn issue_30() {
// A fully-qualified international number must round-trip regardless of
// the reference country: the country code came from the number, so the
// reference country's national prefix must not be stripped.
let parsed = parser::parse(Some(country::US), "+33142764978").unwrap();
assert_eq!(parsed.code().value(), 33);
assert_eq!(parsed.national().value(), 142764978);
assert_eq!(
parser::parse(Some(country::US), "+33142764978").unwrap(),
parser::parse(Some(country::FR), "+33142764978").unwrap()
);
}

#[test]
fn advisory_1() {
let res = parser::parse(None, ".;phone-context=");
Expand Down
Loading