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 value = value.trim();
89 if !value.starts_with('+') {
90 return Err(FieldIssue::new(
91 field,
92 "INVALID_PHONE",
93 "phone must be E.164 with a leading +",
94 ));
95 }
96 let digits: String = value.chars().skip(1).filter(|ch| ch.is_ascii_digit()).collect();
97 if digits.len() != value.len() - 1 {
98 return Err(FieldIssue::new(
99 field,
100 "INVALID_PHONE",
101 "phone may only contain + and digits",
102 ));
103 }
104 if !(7..=15).contains(&digits.len()) {
105 return Err(FieldIssue::new(
106 field,
107 "INVALID_PHONE",
108 "phone digit count must be between 7 and 15",
109 ));
110 }
111 Ok(())
112}
113
114pub fn check_phone_locale(field: &str, value: &str, locale: &str) -> Result<(), FieldIssue> {
116 check_phone_e164(field, value)?;
117 let Some(plan) = dial_plan(locale) else {
118 return Err(FieldIssue::new(
119 field,
120 "INVALID_PHONE_LOCALE",
121 format!("unknown phone locale {locale}"),
122 ));
123 };
124 let digits: String = value.chars().skip(1).filter(|ch| ch.is_ascii_digit()).collect();
125 if !digits.starts_with(plan.country_code) {
126 return Err(FieldIssue::new(
127 field,
128 "INVALID_PHONE",
129 format!("phone must start with +{}", plan.country_code),
130 ));
131 }
132 let national = digits.len() - plan.country_code.len();
133 if national < plan.national_min || national > plan.national_max {
134 return Err(FieldIssue::new(
135 field,
136 "INVALID_PHONE",
137 format!(
138 "national number length must be {}..={}",
139 plan.national_min, plan.national_max
140 ),
141 ));
142 }
143 Ok(())
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149
150 #[test]
151 fn accepts_us_number() {
152 assert!(check_phone_locale("phone", "+14155552671", "US").is_ok());
153 }
154
155 #[test]
156 fn rejects_short_e164() {
157 assert!(check_phone_e164("phone", "+123").is_err());
158 }
159}