namba 0.1.0

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

mod binary;
mod prefix;

use prefix::PrefixTable;

#[cfg(feature = "geocoding")]
const LOCATIONS: PrefixTable =
    PrefixTable::new(include_bytes!("enrichment/artifacts/locations.bin"));
#[cfg(feature = "carrier")]
const CARRIER_NAMES: PrefixTable =
    PrefixTable::new(include_bytes!("enrichment/artifacts/carrier_names.bin"));
#[cfg(feature = "timezone")]
const TIME_ZONES: PrefixTable =
    PrefixTable::new(include_bytes!("enrichment/artifacts/time_zones.bin"));

#[cfg(feature = "geocoding")]
/// An approximate English location for a numbering range.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Location(&'static str);

#[cfg(feature = "geocoding")]
impl Location {
    /// Returns the location name.
    pub fn as_str(&self) -> &str {
        self.0
    }

    /// Looks up an approximate English location for a number.
    pub fn for_number(number: &PhoneNumber) -> Option<Self> {
        LOCATIONS
            .longest_number(number.calling_code().value(), number.national_number())
            .map(Self)
    }
}

#[cfg(feature = "geocoding")]
impl AsRef<str> for Location {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

#[cfg(feature = "carrier")]
/// The original carrier's English name for a numbering range.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CarrierName(&'static str);

#[cfg(feature = "carrier")]
impl CarrierName {
    /// Returns the carrier name.
    pub fn as_str(&self) -> &str {
        self.0
    }

    /// Looks up the original carrier for a numbering range.
    ///
    /// Mobile number portability means this cannot identify the current carrier.
    pub fn for_number(number: &PhoneNumber) -> Option<Self> {
        CARRIER_NAMES
            .longest_number(number.calling_code().value(), number.national_number())
            .map(Self)
    }
}

#[cfg(feature = "carrier")]
impl AsRef<str> for CarrierName {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

#[cfg(feature = "timezone")]
/// IANA time zone identifiers for a numbering range.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TimeZones(&'static str);

#[cfg(feature = "timezone")]
impl TimeZones {
    /// Iterates over the time zone identifiers.
    pub fn iter(&self) -> impl Iterator<Item = &str> {
        self.0.split('&')
    }

    /// Looks up time zones associated with a number.
    pub fn for_number(number: &PhoneNumber) -> Option<Self> {
        TIME_ZONES
            .longest_number(number.calling_code().value(), number.national_number())
            .map(Self)
    }
}

#[cfg(feature = "timezone")]
impl<'a> IntoIterator for &'a TimeZones {
    type Item = &'a str;
    type IntoIter = core::str::Split<'a, char>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.split('&')
    }
}