phonenumber_fixed/metadata/
format.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 regex_cache::CachedRegex;
16
17/// Description of a phone number format.
18#[derive(Clone, Debug)]
19pub struct Format {
20	pub(crate) pattern: CachedRegex,
21	pub(crate) format: String,
22
23	pub(crate) leading_digits: Vec<CachedRegex>,
24	pub(crate) national_prefix: Option<String>,
25	pub(crate) national_prefix_optional: bool,
26	pub(crate) domestic_carrier: Option<String>,
27}
28
29impl Format {
30	/// A regex that is used to match the national (significant) number. For
31	/// example, the pattern "(20)(\d{4})(\d{4})" will match number "2070313000",
32	/// which is the national (significant) number for Google London.
33	///
34	/// Note the presence of the parentheses, which are capturing groups what
35	/// specifies the grouping of numbers.
36	pub fn pattern(&self) -> &CachedRegex {
37		&self.pattern
38	}
39
40	/// Specifies how the national (significant) number matched by pattern should
41	/// be formatted.
42	///
43	/// Using the same example as above, format could contain "$1 $2 $3", meaning
44	/// that the number should be formatted as "20 7031 3000".
45	///
46	/// Each $x are replaced by the numbers captured by group x in the regex
47	/// specified by pattern.
48	pub fn format(&self) -> &str {
49		&self.format
50	}
51
52	/// A regex that is used to match a certain number of digits at the beginning
53	/// of the national (significant) number. When the match is successful, the
54	/// accompanying pattern and format should be used to format this number. For
55	/// example, if leading_digits="[1-3]|44", then all the national numbers
56	/// starting with 1, 2, 3 or 44 should be formatted using the
57	/// accompanying pattern and format.
58	///
59	/// The first leadingDigitsPattern matches up to the first three digits of the
60	/// national (significant) number; the next one matches the first four digits,
61	/// then the first five and so on, until the leadingDigitsPattern can uniquely
62	/// identify one pattern and format to be used to format the number.
63	///
64	/// In the case when only one formatting pattern exists, no
65	/// leading_digits_pattern is needed.
66	pub fn leading_digits(&self) -> &[CachedRegex] {
67		&self.leading_digits
68	}
69
70	/// Specifies how the national prefix ($NP) together with the first group
71	/// ($FG) in the national significant number should be formatted in the
72	/// NATIONAL format when a national prefix exists for a certain country.
73	///
74	/// For example, when this field contains "($NP$FG)", a number from Beijing,
75	/// China (whose $NP = 0), which would by default be formatted without
76	/// national prefix as 10 1234 5678 in NATIONAL format, will instead be
77	/// formatted as (010) 1234 5678; to format it as (0)10 1234 5678, the field
78	/// would contain "($NP)$FG". Note $FG should always be present in this field,
79	/// but $NP can be omitted. For example, having "$FG" could indicate the
80	/// number should be formatted in NATIONAL format without the national prefix.
81	///
82	/// This is commonly used to override the rule specified for the territory in
83	/// the XML file.
84	///
85	/// When this field is missing, a number will be formatted without national
86	/// prefix in NATIONAL format. This field does not affect how a number is
87	/// formatted in other formats, such as INTERNATIONAL.
88	pub fn national_prefix(&self) -> Option<&str> {
89		self.national_prefix.as_ref().map(AsRef::as_ref)
90	}
91
92	/// Whether the national prefix is optional when formatting.
93	pub fn is_national_prefix_optional(&self) -> bool {
94		self.national_prefix_optional
95	}
96
97	/// Specifies how any carrier code ($CC) together with the first group ($FG)
98	/// in the national significant number should be formatted when
99	/// formatWithCarrierCode is called, if carrier codes are used for a certain
100	/// country.
101	pub fn domestic_carrier(&self) -> Option<&str> {
102		self.domestic_carrier.as_ref().map(AsRef::as_ref)
103	}
104}