namba 0.1.0

A fast, lightweight library for parsing, validating, and formatting phone numbers
Documentation
//! Bounded phone number extraction from text.

use namba::{FindOptions, FoundNumbers, Region};

#[test]
fn finds_valid_numbers_with_source_ranges() {
    let text = "Call +44 20 7946 0018, then email us.";
    let found = FoundNumbers::new(text, FindOptions::default())
        .next()
        .unwrap();
    assert_eq!(found.source(), "+44 20 7946 0018");
    assert_eq!(&text[found.range()], found.source());
}

#[test]
fn extraction_obeys_result_bounds() {
    let text = "+1 650 253 0000 / +44 20 7946 0018";
    let options = FindOptions::new().with_max_results(1);
    assert_eq!(FoundNumbers::new(text, options).count(), 1);
}

#[test]
fn prose_after_a_number_does_not_consume_the_candidate() {
    let text = "Call +44 20 7946 0018 then email us";
    let found = FoundNumbers::new(text, FindOptions::default())
        .next()
        .unwrap();
    assert_eq!(found.source(), "+44 20 7946 0018");
}

#[test]
fn extraction_handles_unicode_digits_without_invalid_ranges() {
    let text = "Call ९८७६५४३२१० now";
    let options = FindOptions::new().with_default_region(Region::new("IN").unwrap());
    let found = FoundNumbers::new(text, options).next().unwrap();
    assert_eq!(found.source(), "९८७६५४३२१०");
    assert_eq!(&text[found.range()], found.source());
}

#[test]
fn extraction_recognizes_a_full_width_plus_sign() {
    let text = "Call +64 3 331 6005 today.";
    let found = FoundNumbers::new(text, FindOptions::default())
        .next()
        .unwrap();

    assert_eq!(found.source(), "+64 3 331 6005");
    assert_eq!(found.number().national_number(), "33316005");
}

#[test]
fn possible_results_still_require_a_plausible_length() {
    let options = FindOptions::new().with_possible(true);
    assert_eq!(FoundNumbers::new("Call +1 650 253 000", options).count(), 0);
}

#[test]
fn possible_results_report_possible_confidence() {
    let options = FindOptions::new().with_possible(true);
    let found = FoundNumbers::new("Call +1 123 456 7890", options)
        .next()
        .unwrap();
    assert_eq!(found.confidence(), namba::Confidence::Possible);
}

#[test]
fn found_number_debug_redacts_the_source() {
    let text = "Call +44 20 7946 0018";
    let found = FoundNumbers::new(text, FindOptions::default())
        .next()
        .unwrap();
    let debug = format!("{found:?}");
    assert!(debug.contains("**redacted**"));
    assert!(!debug.contains("7946"));
}