Skip to main content

pactum_annotations/
phone.rs

1use crate::error::FieldIssue;
2
3/// Dial-plan entry for a locale (ISO 3166-1 alpha-2).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
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: 3,
59        national_max: 12,
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
81#[must_use]
82pub fn dial_plan(locale: &str) -> Option<&'static DialPlan> {
83    let locale = locale.trim();
84    DIAL_PLANS
85        .iter()
86        .find(|plan| plan.locale.eq_ignore_ascii_case(locale))
87}
88
89/// Strict ITU-T E.164: leading `+`, non-zero country code digit, 7..=15 total digits.
90pub fn check_phone_e164(field: &str, value: &str) -> Result<(), FieldIssue> {
91    let Some(digits) = value.strip_prefix('+') else {
92        return Err(FieldIssue::new(
93            field,
94            "INVALID_PHONE",
95            "phone must be E.164 with a leading +",
96        ));
97    };
98    if !digits.bytes().all(|b| b.is_ascii_digit()) {
99        return Err(FieldIssue::new(
100            field,
101            "INVALID_PHONE",
102            "phone may only contain + and digits",
103        ));
104    }
105    if !(7..=15).contains(&digits.len()) {
106        return Err(FieldIssue::new(
107            field,
108            "INVALID_PHONE",
109            "phone digit count must be between 7 and 15",
110        ));
111    }
112    if digits.starts_with('0') {
113        return Err(FieldIssue::new(
114            field,
115            "INVALID_PHONE",
116            "phone country code cannot start with 0",
117        ));
118    }
119    Ok(())
120}
121
122/// Locale-aware phone: E.164 whose country code and national length match the dial plan.
123pub fn check_phone_locale(field: &str, value: &str, locale: &str) -> Result<(), FieldIssue> {
124    check_phone_e164(field, value)?;
125    let Some(plan) = dial_plan(locale) else {
126        return Err(FieldIssue::new(
127            field,
128            "INVALID_PHONE_LOCALE",
129            format!("unknown phone locale {locale}"),
130        ));
131    };
132    let digits = &value[1..];
133    if !digits.starts_with(plan.country_code) {
134        return Err(FieldIssue::new(
135            field,
136            "INVALID_PHONE",
137            format!("phone must start with +{}", plan.country_code),
138        ));
139    }
140    let national = digits.len() - plan.country_code.len();
141    if national < plan.national_min || national > plan.national_max {
142        return Err(FieldIssue::new(
143            field,
144            "INVALID_PHONE",
145            format!(
146                "national number length must be {}..={}",
147                plan.national_min, plan.national_max
148            ),
149        ));
150    }
151    Ok(())
152}
153
154#[cfg(test)]
155mod tests {
156    use super::*;
157
158    #[test]
159    fn accepts_us_and_german_numbers() {
160        assert!(check_phone_locale("phone", "+14155552671", "US").is_ok());
161        assert!(check_phone_locale("phone", "+49301234567", "DE").is_ok());
162        assert!(check_phone_locale("phone", "+4915123456789", "de").is_ok());
163    }
164
165    #[test]
166    fn rejects_short_or_zero_cc_e164() {
167        assert!(check_phone_e164("phone", "+123").is_err());
168        assert!(check_phone_e164("phone", "+0123456789").is_err());
169    }
170
171    #[test]
172    fn rejects_untrimmed_e164() {
173        assert!(check_phone_e164("phone", "+14155552671 ").is_err());
174        assert!(check_phone_e164("phone", " +14155552671").is_err());
175    }
176}