diff --git a/src/national_number.rs b/src/national_number.rs index bc2dc7c..301a1b0 100644 --- a/src/national_number.rs +++ b/src/national_number.rs @@ -22,13 +22,16 @@ pub struct NationalNumber { } impl NationalNumber { - pub fn new(value: u64, zeros: u8) -> Self { + pub fn new(value: u64, zeros: u8) -> Result { // E.164 specifies a maximum of 15 decimals, which corresponds to slightly over 48.9 bits. // 56 bits ought to cut it here. - assert!(value < (1 << 56), "number too long"); - Self { - value: ((zeros as u64) << 56) | value, + if value >= (1 << 56) { + return Err(crate::error::Parse::TooLong); } + + Ok(Self { + value: ((zeros as u64) << 56) | value, + }) } /// The number without any leading zeroes. diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 0e3110f..c5914a9 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -87,7 +87,7 @@ pub fn parse_with>( national: NationalNumber::new( number.national.parse()?, number.national.chars().take_while(|&c| c == '0').count() as u8, - ), + )?, extension: number.extension.map(|s| Extension(s.into_owned())), carrier: number.carrier.map(|s| Carrier(s.into_owned())), @@ -109,7 +109,7 @@ mod test { source: country::Source::Default, }, - national: NationalNumber::new(33316005, 0), + national: NationalNumber::new(33316005, 0).unwrap(), extension: None, carrier: None, @@ -197,7 +197,7 @@ mod test { source: country::Source::Number, }, - national: NationalNumber::new(64123456, 0), + national: NationalNumber::new(64123456, 0).unwrap(), extension: None, carrier: None, @@ -215,7 +215,7 @@ mod test { source: country::Source::Default, }, - national: NationalNumber::new(30123456, 0), + national: NationalNumber::new(30123456, 0).unwrap(), extension: None, carrier: None, @@ -230,7 +230,7 @@ mod test { source: country::Source::Plus, }, - national: NationalNumber::new(2345, 0,), + national: NationalNumber::new(2345, 0,).unwrap(), extension: None, carrier: None, @@ -245,7 +245,7 @@ mod test { source: country::Source::Default, }, - national: NationalNumber::new(12, 0,), + national: NationalNumber::new(12, 0,).unwrap(), extension: None, carrier: None, @@ -260,7 +260,7 @@ mod test { source: country::Source::Default, }, - national: NationalNumber::new(3121286979, 0), + national: NationalNumber::new(3121286979, 0).unwrap(), extension: None, carrier: Some("12".into()),