Cotton code
104 lines · 3.9 KB · RustRaw
//! INK NARROWING (owner, 2 Nov 2026; numbers 3 Nov 2026): letters written//! above the fan keep the records some word of whose columns starts with//! them — never by a stop word ("the", "to"), case-, accent- and//! width-blind. A number's grouping commas are part of it and its fraction//! is no word of its own, so "₹7,250.50" is the one word 7250.//!//! The deck's columns are its title, its slip's figure and its search//! columns, each once, a secret one never (`shell.deck {ink}` reads them).use crate::definitions::TableDefinition;use unicode_normalization::UnicodeNormalization;/// The shipped stop words (the ink alphabet's, `ink::shipped`), folded.pub fn stop_words() -> Vec<String> {    crate::ink::shipped().stop_words.iter().map(|word| fold(word)).collect()}/// The columns `definition`'s deck narrows by: its title, its slip's figure/// and its search columns, each once, a secret one never.pub fn columns(definition: &TableDefinition) -> Vec<String> {    let mut keys: Vec<String> = Vec::new();    let named = std::iter::once(definition.title.clone())        .chain(definition.slip.figure.clone())        .chain(definition.search.clone().unwrap_or_default());    for key in named {        let Some(column) = definition.column(&key) else { continue };        if column.secret == Some(true) || keys.contains(&key) {            continue;        }        keys.push(key);    }    keys}/// Whether some word of `text` other than a stop word starts with `letters`;/// empty letters hold everything, no text holds nothing.pub fn holds(text: Option<&str>, letters: &str, stops: &[String]) -> bool {    let wanted = fold(letters);    if wanted.is_empty() {        return true;    }    let Some(text) = text else { return false };    words(&fold(text)).iter().any(|word| !stops.contains(word) && word.starts_with(&wanted))}/// The words of `text`: its runs of letters and digits, a number's grouping/// commas inside it and its fraction dropped.pub fn words(text: &str) -> Vec<String> {    let characters: Vec<char> = text.chars().collect();    let mut words = Vec::new();    let mut word = String::new();    let mut fraction = false;    for (at, character) in characters.iter().enumerate() {        if character.is_alphanumeric() {            if !fraction {                word.push(*character);            }            continue;        }        let between = at > 0 && characters[at - 1].is_numeric() && characters.get(at + 1).is_some_and(|next| next.is_numeric());        if between && *character == ',' && !fraction {            continue;        }        if !word.is_empty() {            words.push(std::mem::take(&mut word));        }        fraction = between && *character == '.';    }    if !word.is_empty() {        words.push(word);    }    words}/// Case-, accent- and width-blind: compatibility-decomposed, its combining/// marks dropped, lower case.pub fn fold(text: &str) -> String {    text.nfkd().filter(|ch| !matches!(ch, '\u{300}'..='\u{36f}')).collect::<String>().to_lowercase()}#[cfg(test)]mod tests {    use super::*;    // RULE (owner, 2 Nov 2026): a T keeps the todos whose own words start with T, never one by its "the" or "to".    #[test]    fn a_letter_keeps_words_never_stop_words() {        let stops = stop_words();        assert!(stops.contains(&"the".to_string()));        assert!(holds(Some("Take the bins out"), "t", &stops));        assert!(!holds(Some("Go to the shop"), "t", &stops));        assert!(holds(Some("Éclair recipe"), "ec", &stops), "accent-blind");        assert!(holds(Some("anything"), "", &stops));        assert!(!holds(None, "a", &stops));    }    // RULE (owner, 3 Nov 2026): an amount narrows by its whole figure from its first digit.    #[test]    fn an_amount_is_one_word() {        assert_eq!(words("₹7,250.50 paid"), ["7250", "paid"]);        assert!(holds(Some("₹7,250.50"), "72", &[]));        assert!(!holds(Some("₹7,250.50"), "50", &[]));    }}