//! Merchant keys: the same shop written differently by a phone, a bank and a person becomes one key. //! Rules and subscriptions are keyed on it. /// `PAYPAL *SPOTIFY P1234` → `spotify`; `ALBERT 1234 PRAHA 4 CZ` → `albert praha`; `Žabka` → `zabka`. pub fn merchant_key(raw: &str) -> String { let folded: String = raw.chars().flat_map(fold).collect::().to_lowercase(); // A processor's prefix before `*`: "paypal *spotify", "sumup *kavarna", "sq *bistro". let rest = match folded.split_once('*') { Some((head, tail)) if !tail.trim().is_empty() && head.split_whitespace().count() <= 2 => tail, _ => folded.as_str(), }; let cleaned: String = rest.chars().map(|c| if c.is_ascii_alphanumeric() { c } else { ' ' }).collect(); let mut words: Vec<&str> = cleaned .split_whitespace() .filter(|w| !w.bytes().any(|b| b.is_ascii_digit())) .collect(); while words.len() > 1 && matches!(words.last(), Some(&"cz") | Some(&"cze") | Some(&"czech")) { words.pop(); } words.join(" ") } /// Whether two merchant keys can be the same shop: equal, one inside the other, or the same first word of /// at least three letters. An empty key is never compatible. pub fn compatible(a: &str, b: &str) -> bool { if a.is_empty() || b.is_empty() { return false; } if a == b || contains_words(a, b) || contains_words(b, a) { return true; } match (a.split(' ').next(), b.split(' ').next()) { (Some(x), Some(y)) => x == y && x.len() >= 3, _ => false, } } /// `needle`'s words appear in `hay` as a run of whole words. fn contains_words(hay: &str, needle: &str) -> bool { let h: Vec<&str> = hay.split(' ').collect(); let n: Vec<&str> = needle.split(' ').collect(); n.len() <= h.len() && h.windows(n.len()).any(|w| w == n.as_slice()) } /// ASCII folding of the Latin letters a Czech (or Slovak, German, Polish) shop name uses. fn fold(c: char) -> std::iter::Once { let r = match c { 'á' | 'ä' | 'à' | 'â' | 'ą' => 'a', 'Á' | 'Ä' | 'À' | 'Â' | 'Ą' => 'A', 'č' | 'ć' | 'ç' => 'c', 'Č' | 'Ć' | 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'é' | 'ě' | 'ë' | 'è' | 'ê' | 'ę' => 'e', 'É' | 'Ě' | 'Ë' | 'È' | 'Ê' | 'Ę' => 'E', 'í' | 'ï' | 'î' => 'i', 'Í' | 'Ï' | 'Î' => 'I', 'ľ' | 'ĺ' | 'ł' => 'l', 'Ľ' | 'Ĺ' | 'Ł' => 'L', 'ň' | 'ń' => 'n', 'Ň' | 'Ń' => 'N', 'ó' | 'ö' | 'ô' | 'ő' => 'o', 'Ó' | 'Ö' | 'Ô' | 'Ő' => 'O', 'ř' | 'ŕ' => 'r', 'Ř' | 'Ŕ' => 'R', 'š' | 'ś' => 's', 'Š' | 'Ś' => 'S', 'ť' => 't', 'Ť' => 'T', 'ú' | 'ů' | 'ü' | 'ű' => 'u', 'Ú' | 'Ů' | 'Ü' | 'Ű' => 'U', 'ý' | 'ÿ' => 'y', 'Ý' => 'Y', 'ž' | 'ź' | 'ż' => 'z', 'Ž' | 'Ź' | 'Ż' => 'Z', 'ß' => 's', other => other, }; std::iter::once(r) } #[cfg(test)] mod tests { use super::*; #[test] fn keys() { assert_eq!(merchant_key("PAYPAL *SPOTIFY P1234"), "spotify"); assert_eq!(merchant_key("ALBERT 1234 PRAHA 4 CZ"), "albert praha"); assert_eq!(merchant_key("Žabka"), "zabka"); assert_eq!(merchant_key("NETFLIX.COM"), "netflix com"); assert_eq!(merchant_key("SumUp *Kavárna Dobrá"), "kavarna dobra"); assert_eq!(merchant_key(" "), ""); assert_eq!(merchant_key("123 456"), ""); assert_eq!(merchant_key("Kč * "), "kc"); } #[test] fn compatibility() { assert!(compatible("albert", "albert praha")); assert!(compatible("albert praha", "albert brno")); assert!(compatible("netflix com", "netflix")); assert!(!compatible("lidl", "albert")); assert!(!compatible("", "albert")); assert!(!compatible("od praha", "od brno"), "a two-letter first word is not enough"); } }