1use crate::error::FieldIssue;
2
3#[derive(Debug, Clone, Copy)]
5pub struct DialPlan {
6 pub locale: &'static str,
7 pub country_code: &'static str,
8 pub national_min: usize,
9 pub national_max: usize,
10}
11
12pub const DIAL_PLANS: &[DialPlan] = &[
13 DialPlan {
14 locale: "US",
15 country_code: "1",
16 national_min: 10,
17 national_max: 10,
18 },
19 DialPlan {
20 locale: "CA",
21 country_code: "1",
22 national_min: 10,
23 national_max: 10,
24 },
25 DialPlan {
26 locale: "MX",
27 country_code: "52",
28 national_min: 10,
29 national_max: 10,
30 },
31 DialPlan {
32 locale: "GB",
33 country_code: "44",
34 national_min: 9,
35 national_max: 10,
36 },
37 DialPlan {
38 locale: "ES",
39 country_code: "34",
40 national_min: 9,
41 national_max: 9,
42 },
43 DialPlan {
44 locale: "BR",
45 country_code: "55",
46 national_min: 10,
47 national_max: 11,
48 },
49 DialPlan {
50 locale: "FR",
51 country_code: "33",
52 national_min: 9,
53 national_max: 9,
54 },
55 DialPlan {
56 locale: "DE",
57 country_code: "49",
58 national_min: 10,
59 national_max: 11,
60 },
61 DialPlan {
62 locale: "AU",
63 country_code: "61",
64 national_min: 9,
65 national_max: 9,
66 },
67 DialPlan {
68 locale: "IN",
69 country_code: "91",
70 national_min: 10,
71 national_max: 10,
72 },
73 DialPlan {
74 locale: "JP",
75 country_code: "81",
76 national_min: 9,
77 national_max: 10,
78 },
79];
80
81pub fn dial_plan(locale: &str) -> Option<&'static DialPlan> {
82 let locale = locale.trim().to_uppercase();
83 DIAL_PLANS.iter().find(|plan| plan.locale == locale)
84}
85
86pub fn check_phone_e164(field: &str, value: &str) -> Result<(), FieldIssue> {
88 let Some(digits) = value.strip_prefix('+') else {
89 return Err(FieldIssue::new(
90 field,
91 "INVALID_PHONE",
92 "phone must be E.164 with a leading +",
93 ));
94 };
95 if !digits.bytes().all(|b| b.is_ascii_digit()) {
96 return Err(FieldIssue::new(
97 field,
98 "INVALID_PHONE",
99 "phone may only contain + and digits",
100 ));
101 }
102 if !(7..=15).contains(&digits.len()) {
103 return Err(FieldIssue::new(
104 field,
105 "INVALID_PHONE",
106 "phone digit count must be between 7 and 15",
107 ));
108 }
109 Ok(())
110}
111
112pub fn check_phone_locale(field: &str, value: &str, locale: &str) -> Result<(), FieldIssue> {
114 check_phone_e164(field, value)?;
115 let Some(plan) = dial_plan(locale) else {
116 return Err(FieldIssue::new(
117 field,
118 "INVALID_PHONE_LOCALE",
119 format!("unknown phone locale {locale}"),
120 ));
121 };
122 let digits = &value[1..];
123 if !digits.starts_with(plan.country_code) {
124 return Err(FieldIssue::new(
125 field,
126 "INVALID_PHONE",
127 format!("phone must start with +{}", plan.country_code),
128 ));
129 }
130 let national = digits.len() - plan.country_code.len();
131 if national < plan.national_min || national > plan.national_max {
132 return Err(FieldIssue::new(
133 field,
134 "INVALID_PHONE",
135 format!(
136 "national number length must be {}..={}",
137 plan.national_min, plan.national_max
138 ),
139 ));
140 }
141 Ok(())
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn accepts_us_number() {
150 assert!(check_phone_locale("phone", "+14155552671", "US").is_ok());
151 }
152
153 #[test]
154 fn rejects_short_e164() {
155 assert!(check_phone_e164("phone", "+123").is_err());
156 }
157
158 #[test]
159 fn rejects_untrimmed_e164() {
160 assert!(check_phone_e164("phone", "+14155552671 ").is_err());
161 assert!(check_phone_e164("phone", " +14155552671").is_err());
162 }
163}