phonenumber_fixed/national_number.rs
1// Copyright (C) 2017 1aim GmbH
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt;
16
17/// The national number part of a phone number.
18#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Hash, Debug)]
19pub struct NationalNumber {
20 pub(crate) value: u64,
21
22 /// In some countries, the national (significant) number starts with one or
23 /// more "0"s without this being a national prefix or trunk code of some
24 /// kind. For example, the leading zero in the national (significant) number
25 /// of an Italian phone number indicates the number is a fixed-line number.
26 /// There have been plans to migrate fixed-line numbers to start with the
27 /// digit two since December 2000, but it has not happened yet. See
28 /// http://en.wikipedia.org/wiki/%2B39 for more details.
29 ///
30 /// These fields can be safely ignored (there is no need to set them) for
31 /// most countries. Some limited number of countries behave like Italy - for
32 /// these cases, if the leading zero(s) of a number would be retained even
33 /// when dialling internationally, set this flag to true, and also set the
34 /// number of leading zeros.
35 ///
36 /// Clients who use the parsing or conversion functionality of the i18n phone
37 /// number libraries will have these fields set if necessary automatically.
38 pub(crate) zeros: u8,
39}
40
41impl NationalNumber {
42 /// The number without any leading zeroes.
43 pub fn value(&self) -> u64 {
44 self.value
45 }
46
47 /// The number of leading zeroes.
48 pub fn zeros(&self) -> u8 {
49 self.zeros
50 }
51}
52
53impl Into<u64> for NationalNumber {
54 fn into(self) -> u64 {
55 self.value
56 }
57}
58
59impl fmt::Display for NationalNumber {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 for _ in 0 .. self.zeros {
62 write!(f, "0")?;
63 }
64
65 write!(f, "{}", self.value)
66 }
67}