namba 0.1.0

A fast, lightweight library for parsing, validating, and formatting phone numbers
Documentation
use crate::{NumberKind, PhoneNumber, Region};

/// Returns a bundled valid example number for `region`.
///
/// Example numbers are suitable for documentation and tests, not for calling.
pub fn example_number(region: Region) -> Option<PhoneNumber> {
    let plan = crate::plans::by_region(region.as_str())?;
    let source = if plan.fixed_line.example.is_empty() {
        &plan.mobile.example
    } else {
        &plan.fixed_line.example
    };
    parse_example(region, source)
}

/// Returns a bundled valid example of `kind` for `region`.
///
/// Example numbers are suitable for documentation and tests, not for calling.
pub fn example_number_for_kind(region: Region, kind: NumberKind) -> Option<PhoneNumber> {
    let plan = crate::plans::by_region(region.as_str())?;
    let examples: &[&str] = match kind {
        NumberKind::FixedLine => &[&plan.fixed_line.example],
        NumberKind::Mobile => &[&plan.mobile.example],
        NumberKind::FixedLineOrMobile => &[&plan.fixed_line.example, &plan.mobile.example],
        NumberKind::TollFree => &[&plan.toll_free.example],
        NumberKind::PremiumRate => &[&plan.premium_rate.example],
        NumberKind::SharedCost => &[&plan.shared_cost.example],
        NumberKind::Personal => &[&plan.personal.example],
        NumberKind::Voip => &[&plan.voip.example],
        NumberKind::Pager => &[&plan.pager.example],
        NumberKind::UniversalAccess => &[&plan.universal_access.example],
        NumberKind::Voicemail => &[&plan.voicemail.example],
        NumberKind::Unknown => &[],
    };
    examples.iter().find_map(|source| {
        let number = parse_example(region, source)?;
        (number.assess().kind() == kind).then_some(number)
    })
}

fn parse_example(region: Region, source: &str) -> Option<PhoneNumber> {
    if source.is_empty() {
        None
    } else {
        crate::parse_in(region, source).ok()
    }
}