<commit_before>#![feature(ascii_ctype)]
use std::ascii::AsciiExt;


fn check_countrycode(mut phone: Vec<u8>) -> Option<String> {
    println!("{:?}", phone);

    if phone[0] != b'1' {
        return None;
    }

    String::from_utf8(phone.drain(1..).collect()).ok()
}

pub fn number(phone: &str) -> Option<String> {
    let result = phone
        .chars()
        .filter(|x| x.is_ascii_digit())
        .map(|x| x as u8)
        .collect::<Vec<u8>>();

    match result.len() {
        11 => check_countrycode(result),
        10 => String::from_utf8(result).ok(),
        _ => None,
    }
}

pub fn area_code(phone: &str) -> Option<String> {
    number(phone).map(|r| r[..3].to_string())
}

pub fn pretty_print(phone: &str) -> String {
    let v = number(phone);

    if v.is_none() {
        return "invalid".to_string();
    }

    let r = v.unwrap();

    format!("({}) {}-{}", &r[..3], &r[3..6], &r[6..])
}
<commit_msg>Improve rust code for phone-number<commit_after>#![feature(ascii_ctype)]
use std::ascii::AsciiExt;

pub fn number<S>(phone: S) -> Option<String> where S: Into<String> {
    let result = phone
        .into()
        .chars()
        .filter(|x| x.is_ascii_digit())
        .collect::<String>();

    match result.len() {
        11 if result.starts_with('1') => Some(result[1..].to_string()),
        10 => Some(result),
        _ => None,
    }
}

pub fn area_code<S>(phone: S) -> Option<String> where S: Into<String> {
    number(phone).map(|r| r[..3].to_string())
}

pub fn pretty_print<S>(phone: S) -> String where S: Into<String> {
    number(phone).map_or("invalid".to_string(), |r| {
        format!("({}) {}-{}", &r[..3], &r[3..6], &r[6..])
    })
}

{'fraction_non_alphanumeric': 0.22136498516320474, 'fraction_numerical': 0.01661721068249258, 'mean_word_length': 2.730088495575221, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2985667', 'n_tokens_mistral': 644, 'n_tokens_neox': 623, 'n_words': 121}