phonenumber_fixed/metadata/descriptor.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 to parse.
18#[derive(Clone, Debug)]
19pub struct Descriptor {
20 pub(crate) national_number: CachedRegex,
21
22 pub(crate) possible_length: Vec<u16>,
23 pub(crate) possible_local_length: Vec<u16>,
24
25 pub(crate) example: Option<String>,
26}
27
28impl Descriptor {
29 /// The national number is the pattern that a valid national significant
30 /// number would match. This specifies information such as its total length
31 /// and leading digits.
32 pub fn national_number(&self) -> &CachedRegex {
33 &self.national_number
34 }
35
36 /// These represent the lengths a phone number from this region can be. They
37 /// will be sorted from smallest to biggest. Note that these lengths are for
38 /// the full number, without country calling code or national prefix. For
39 /// example, for the Swiss number +41789270000, in local format 0789270000,
40 /// this would be 9.
41 ///
42 /// This could be used to highlight tokens in a text that may be a phone
43 /// number, or to quickly prune numbers that could not possibly be a phone
44 /// number for this locale.
45 pub fn possible_length(&self) -> &[u16] {
46 &self.possible_length
47 }
48
49 /// These represent the lengths that only local phone numbers (without an
50 /// area code) from this region can be. They will be sorted from smallest to
51 /// biggest. For example, since the American number 456-1234 may be locally
52 /// diallable, although not diallable from outside the area, 7 could be a
53 /// possible value. This could be used to highlight tokens in a text that
54 /// may be a phone number.
55 ///
56 /// To our knowledge, area codes are usually only relevant for some
57 /// fixed-line and mobile numbers, so this field should only be set for those
58 /// types of numbers (and the general description) - however there are
59 /// exceptions for NANPA countries.
60 ///
61 /// This data is used to calculate whether a number could be a possible
62 /// number for a particular type.
63 pub fn possible_local_length(&self) -> &[u16] {
64 &self.possible_local_length
65 }
66
67 /// An example national significant number for the specific type. It should
68 /// not contain any formatting information.
69 pub fn example(&self) -> Option<&str> {
70 self.example.as_ref().map(AsRef::as_ref)
71 }
72
73 /// Check if the descriptor matches the given national number.
74 pub fn is_match(&self, value: &str) -> bool {
75 if !self.possible_length.is_empty() &&
76 !self.possible_length.contains(&(value.len() as u16))
77 {
78 return false;
79 }
80
81 self.national_number.find(value).map(|m| m.start() == 0)
82 .unwrap_or(false)
83 }
84}