Cotton code
43 lines · 2.4 KB · RustRaw
use super::*;fn found(text: &str, me: Option<&str>) -> Vec<(String, String)> {    let units: Vec<u16> = text.encode_utf16().collect();    numbers_in(text, me).into_iter().map(|n| (String::from_utf16(&units[n.at[0] as usize..n.at[1] as usize]).unwrap(), n.number)).collect()}/// RULE (FEEDBACK-2 A): a number written with its country code is found however it is spaced, where it stands, as/// E.164; one without a code only in the person's own country, and only when it is a number there.#[test]fn a_number_in_words_is_found_where_it_stands() {    assert_eq!(found("Call me on +1 555 010 7001 tonight", None), vec![("+1 555 010 7001".into(), "+15550107001".into())]);    assert_eq!(found("New number: +91-98765-43210.", None), vec![("+91-98765-43210".into(), "+919876543210".into())]);    assert_eq!(found("ring +44 20 7946 0958 or +1 (555) 010-7002", None).len(), 2);    assert_eq!(found("it is 98765 43210", Some("+919800000001")), vec![("98765 43210".into(), "+919876543210".into())], "his own country reads it");    assert_eq!(found("it is 98765 43210", None), vec![], "no country to be sure of");}/// RULE: what is not a number is not marked: a price, a year, a date, a code inside a word, a run too long.#[test]fn what_is_not_a_number_is_left_alone() {    for words in ["It cost 1500 rupees", "Back on 2026-11-23", "order AB12345678CD", "id 1234567890123456789", "+1 555"] {        assert!(found(words, Some("+15550100000")).is_empty(), "{words}");    }}/// RULE: where it stands is counted in UTF-16, as a search's marks are, so a phone marks the very characters/// whatever comes before it.#[test]fn where_it_stands_is_counted_as_the_phones_count() {    let text = "😀 namaste, +91 98765 43210";    let at = numbers_in(text, None)[0].at;    assert_eq!(at, [12, 27]);    assert_eq!(found(text, None)[0].0, "+91 98765 43210");}// RULE: a number written with its country code counts even where libphonenumber holds the rest invalid (a test range, a range newer than its metadata): the writer named the country, and a sign-in takes such a number; without a code it must be valid in the person's own country.#[test]fn a_number_written_with_its_code_counts_even_when_libphonenumber_doubts_it() {    let region = region_of("15550190000");    assert_eq!(e164_in("+1 555 019 0001", region).as_deref(), Some("+15550190001"));    assert_eq!(e164_in("555 019 0002", region), None, "without its code, an invalid number is not guessed");}