Skip to main content

teaql_tool_extra/
phone.rs

1use teaql_tool_core::{Result, TeaQLToolError};
2
3#[derive(Debug, Clone)]
4pub struct PhoneTool;
5
6impl PhoneTool {
7    pub fn new() -> Self { Self }
8
9    pub fn parse(&self, number: &str) -> Result<phonenumber::PhoneNumber> {
10        phonenumber::parse(None, number).map_err(|e| TeaQLToolError::ParseError(e.to_string()))
11    }
12
13    pub fn is_valid(&self, number: &str) -> bool {
14        phonenumber::parse(None, number).map(|p| phonenumber::is_valid(&p)).unwrap_or(false)
15    }
16
17    pub fn format_international(&self, number: &str) -> Result<String> {
18        self.parse(number).map(|p| p.format().mode(phonenumber::Mode::International).to_string())
19    }
20}
21
22impl Default for PhoneTool {
23    fn default() -> Self { Self::new() }
24}