cal_core/
utils.rs

1use phonenumber::country;
2use phonenumber::country::Id;
3
4pub fn clean_str_parse_country(country_code: &str, str: &str) -> String {
5    let id = country_code.parse().unwrap_or_else(|_| country::GB);
6    clean_str(id, str)
7}
8
9pub fn clean_str(country: Id, str: &str) -> String {
10    
11    let clean: String = str
12        .replace("+", "")
13        .replace("<sip:", "")
14        .replace(">", "")
15        .split(":")
16        .next()
17        .unwrap_or(str)
18        .into();
19    
20    match phonenumber::parse(Some(country), clean) {
21        Ok(number) => {
22            let valid = phonenumber::is_valid(&number);
23            if valid {
24                format!("{}{}", number.code().value(), number.national().value())
25            } else {
26                str.to_string()
27            }
28        }
29        Err(_) => str.to_string(),
30    }
31}