1use crate::macros::string_newtype;
2
3string_newtype! {
4 pub struct PhoneNumber;
12 error = PhoneNumberError;
13 expecting = "an Uzbek phone number: 998 followed by 9 digits";
14}
15
16impl PhoneNumber {
17 pub const DIGIT_LEN: usize = 12;
19 pub const COUNTRY_CODE: &str = "998";
21 pub const OPERATOR_CODE_LEN: usize = 2;
23
24 pub const MOBILE_CODES: &[&str] = &[
39 "20", "33", "50", "70", "77", "80", "87", "88", "90", "91", "92", "93", "94", "95", "97",
40 "98", "99",
41 ];
42
43 pub const GEOGRAPHIC_CODES: &[&str] = &[
49 "61", "62", "65", "66", "67", "69", "70", "71", "72", "73", "74", "75", "76", "79",
50 ];
51
52 pub const SIP_CODES: &[&str] = &["55"];
57
58 pub const NON_GEOGRAPHIC_FIXED_CODES: &[&str] = &["78"];
63
64 #[deprecated(note = "aniq hudud kodlari uchun PhoneNumber::GEOGRAPHIC_CODES dan foydalaning")]
66 pub const REGIONAL_CODES: core::ops::RangeInclusive<u8> = 60..=79;
67
68 fn normalize(s: &mut String) {
69 if s.starts_with('+') {
70 s.remove(0);
71 }
72 if !s.bytes().all(|b| b.is_ascii_digit()) {
74 s.retain(|c| !(c.is_ascii_whitespace() || matches!(c, '-' | '(' | ')' | '.')));
75 }
76 }
77
78 fn validate(s: &str) -> Result<(), PhoneNumberError> {
79 if !s.bytes().all(|b| b.is_ascii_digit()) {
80 return Err(PhoneNumberError::Format);
81 }
82 if s.len() != Self::DIGIT_LEN {
83 return Err(PhoneNumberError::Length);
84 }
85 if !s.starts_with(Self::COUNTRY_CODE) {
86 return Err(PhoneNumberError::Prefix);
87 }
88 Ok(())
89 }
90
91 pub fn parse_strict(value: &str) -> Result<Self, PhoneNumberError> {
97 let phone = Self::parse(value)?;
98 if !phone.is_known_operator() {
99 return Err(PhoneNumberError::UnknownOperatorCode);
100 }
101 Ok(phone)
102 }
103
104 #[inline]
126 #[must_use]
127 pub fn operator_code(&self) -> &str {
128 let start = Self::COUNTRY_CODE.len();
129 &self.0[start..start + Self::OPERATOR_CODE_LEN]
130 }
131
132 #[inline]
134 #[must_use]
135 pub fn subscriber_number(&self) -> &str {
136 &self.0[Self::COUNTRY_CODE.len() + Self::OPERATOR_CODE_LEN..]
137 }
138
139 #[inline]
141 #[must_use]
142 pub fn is_mobile(&self) -> bool {
143 Self::MOBILE_CODES.contains(&self.operator_code())
144 }
145
146 #[inline]
148 #[must_use]
149 pub fn is_geographic(&self) -> bool {
150 Self::GEOGRAPHIC_CODES.contains(&self.operator_code())
151 }
152
153 #[inline]
155 #[must_use]
156 pub fn is_sip(&self) -> bool {
157 Self::SIP_CODES.contains(&self.operator_code())
158 }
159
160 #[inline]
162 #[must_use]
163 pub fn is_non_geographic_fixed(&self) -> bool {
164 Self::NON_GEOGRAPHIC_FIXED_CODES.contains(&self.operator_code())
165 }
166
167 #[must_use]
175 pub fn is_known_operator(&self) -> bool {
176 self.is_mobile() || self.is_geographic() || self.is_sip() || self.is_non_geographic_fixed()
177 }
178
179 #[must_use]
181 pub fn to_international(&self) -> String {
182 format!("+{}", self.0)
183 }
184}
185
186#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
188#[non_exhaustive]
189pub enum PhoneNumberError {
190 #[error("phone number length is invalid, expected 12 digits")]
192 Length,
193 #[error("phone number format is invalid")]
195 Format,
196 #[error("phone number must start with 998")]
198 Prefix,
199 #[error("phone number has an unknown operator or region code")]
201 UnknownOperatorCode,
202}
203
204#[cfg(test)]
205mod tests {
206 use super::*;
207
208 #[test]
209 fn separators_are_tolerated() {
210 for input in [
211 "+998 (90) 123-45-67",
212 "998 90 123 45 67",
213 "+998901234567",
214 "998901234567",
215 " +998-90-123-45-67 ",
216 "998.90.123.45.67",
217 "+998 (90) 123.45.67",
218 ] {
219 assert_eq!(
220 PhoneNumber::parse(input).unwrap().as_str(),
221 "998901234567",
222 "{input}"
223 );
224 }
225 }
226
227 #[test]
228 fn structure_vs_registry() {
229 let fake = PhoneNumber::parse("998000000000").unwrap(); assert!(!fake.is_known_operator());
231 assert_eq!(
232 PhoneNumber::parse_strict("998000000000"),
233 Err(PhoneNumberError::UnknownOperatorCode)
234 );
235 assert!(
236 PhoneNumber::parse("998711234567")
237 .unwrap()
238 .is_known_operator()
239 );
240 assert_eq!(
241 PhoneNumber::parse("997901234567"),
242 Err(PhoneNumberError::Prefix)
243 );
244 assert_eq!(
245 PhoneNumber::parse("99890123456"),
246 Err(PhoneNumberError::Length)
247 );
248 assert_eq!(
249 PhoneNumber::parse("998a01234567"),
250 Err(PhoneNumberError::Format)
251 );
252 assert_eq!(
253 PhoneNumber::parse("998+901234567"),
254 Err(PhoneNumberError::Format)
255 );
256 }
257
258 fn with_code(code: &str) -> String {
259 format!("998{code}1234567")
260 }
261
262 #[test]
263 fn exact_code_sets_are_classified() {
264 assert_eq!(
265 PhoneNumber::MOBILE_CODES,
266 &[
267 "20", "33", "50", "70", "77", "80", "87", "88", "90", "91", "92", "93", "94", "95",
268 "97", "98", "99"
269 ]
270 );
271 assert_eq!(
272 PhoneNumber::GEOGRAPHIC_CODES,
273 &[
274 "61", "62", "65", "66", "67", "69", "70", "71", "72", "73", "74", "75", "76", "79"
275 ]
276 );
277 assert_eq!(PhoneNumber::SIP_CODES, &["55"]);
278 assert_eq!(PhoneNumber::NON_GEOGRAPHIC_FIXED_CODES, &["78"]);
279
280 let overlap = PhoneNumber::parse(&with_code("70")).unwrap();
281 assert!(overlap.is_mobile());
282 assert!(overlap.is_geographic());
283
284 for code in ["80", "87", "92"] {
285 let value = with_code(code);
286 let phone = PhoneNumber::parse_strict(&value).unwrap();
287 assert!(phone.is_mobile());
288 }
289
290 let sip = PhoneNumber::parse(&with_code("55")).unwrap();
291 assert!(sip.is_known_operator());
292 assert!(sip.is_sip());
293 assert!(!sip.is_mobile());
294
295 let fixed = PhoneNumber::parse(&with_code("78")).unwrap();
296 assert!(fixed.is_known_operator());
297 assert!(fixed.is_non_geographic_fixed());
298
299 let geographic = PhoneNumber::parse(&with_code("71")).unwrap();
300 assert!(geographic.is_geographic());
301 assert!(!geographic.is_mobile());
302 }
303
304 #[test]
305 fn unassigned_codes_are_only_structurally_valid() {
306 for code in ["00", "60", "63", "64", "68"] {
307 let value = with_code(code);
308 let phone = PhoneNumber::parse(&value).unwrap();
309 assert!(!phone.is_known_operator(), "{code}");
310 assert_eq!(
311 PhoneNumber::parse_strict(&value),
312 Err(PhoneNumberError::UnknownOperatorCode),
313 "{code}"
314 );
315 }
316 }
317}