use std::cell::Cell;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::sync::OnceLock;
pub const CLASSES: &[(u8, &str)] = &[
(26, "municipality_guard"), (23, "city_tail"), (1, "commune_alias"), (2, "city_alias"), (3, "abbrev2"), (4, "capitals"), (5, "street_types_cyr"), (16, "street_types_latin"), (17, "street_types_extra"), (6, "affix_extra"), (7, "noise"), (8, "noise_after"), (9, "region_markers"), (10, "countries_mid"), (11, "countries_tail"), (12, "place_junk"), (13, "place_prefix"), (14, "fr_ord_cities"), (15, "place_type_strip"), ];
pub struct Rules {
pub municipality_guard: HashMap<String, String>,
pub city_tail: HashMap<String, String>,
pub recipient_prefixes: HashSet<String>,
pub commune_alias: HashMap<String, String>,
pub city_alias: Vec<(String, String)>,
pub street_rewrites: Vec<(String, String)>, pub abbrev2: Vec<(String, String)>, pub capitals: Vec<String>,
pub types_cyr: Vec<String>,
pub types_latin: Vec<String>,
pub types_extra: HashSet<String>,
pub affix_extra: HashSet<String>,
pub noise: HashSet<String>,
pub noise_after: HashSet<String>,
pub region_markers: HashSet<String>,
pub countries_mid: HashSet<String>,
pub countries_tail: HashSet<String>,
pub place_junk: HashSet<String>,
pub place_prefix: HashSet<String>,
pub fr_ord_cities: HashSet<String>,
pub place_type_strip: HashSet<String>,
pub de_city_alias: Vec<(String, String)>,
pub de_abbrev2: Vec<(String, String)>,
pub de_street_types: Vec<String>,
pub de_countries: HashSet<String>,
pub de_admin_tail: HashSet<String>,
}
impl Rules {
pub fn is_street_type(&self, w: &str) -> bool {
self.types_extra.contains(w)
|| self.types_cyr.iter().any(|t| t == w)
|| self.types_latin.iter().any(|t| t == w)
}
pub fn is_affix(&self, w: &str) -> bool {
self.is_street_type(w) || self.affix_extra.contains(w)
}
pub fn commune_alias(&self, name: &str) -> Option<&str> {
self.commune_alias.get(name).map(|s| s.as_str())
}
fn add(&mut self, class: u8, key: String, val: String) {
match class {
26 => {
self.municipality_guard.insert(key, val);
}
25 => {
self.recipient_prefixes.insert(key);
}
23 => {
self.city_tail.insert(key, val);
}
1 => {
self.commune_alias.insert(key, val);
}
2 => self.city_alias.push((key, val)),
24 => self.street_rewrites.push((key, val)),
3 => self.abbrev2.push((key, val)),
4 => self.capitals.push(key),
5 => self.types_cyr.push(key),
16 => self.types_latin.push(key),
17 => {
self.types_extra.insert(key);
}
6 => {
self.affix_extra.insert(key);
}
7 => {
self.noise.insert(key);
}
8 => {
self.noise_after.insert(key);
}
9 => {
self.region_markers.insert(key);
}
10 => {
self.countries_mid.insert(key);
}
11 => {
self.countries_tail.insert(key);
}
12 => {
self.place_junk.insert(key);
}
13 => {
self.place_prefix.insert(key);
}
14 => {
self.fr_ord_cities.insert(key);
}
15 => {
self.place_type_strip.insert(key);
}
18 => self.de_city_alias.push((key, val)),
19 => self.de_abbrev2.push((key, val)),
20 => self.de_street_types.push(key),
21 => {
self.de_countries.insert(key);
}
22 => {
self.de_admin_tail.insert(key);
}
_ => {}
}
}
}
const D_COMMUNE_ALIAS: &[(&str, &str)] = &[
("den haag", "s gravenhage"),
("den bosch", "s hertogenbosch"),
("reggio emilia", "reggio nell emilia"),
];
const D_CITY_ALIASES: &[(&str, &str)] = &[
("спб", "санкт петербург"),
("мск", "москва"),
("екб", "екатеринбург"),
("нск", "новосибирск"),
("питер", "санкт петербург"),
("bg", "beograd"),
("ns", "novi sad"),
("amsterdam zo", "amsterdam"),
("moscow", "москва"),
("saint petersburg", "санкт петербург"),
("st petersburg", "санкт петербург"),
("rome", "roma"),
("milan", "milano"),
("naples", "napoli"),
("turin", "torino"),
("florence", "firenze"),
("venice", "venezia"),
("genoa", "genova"),
("padua", "padova"),
];
const D_ABBREV2: &[(&str, &str)] = &[
("p zza", "piazza"),
("c so", "corso"),
("v le", "viale"),
("пр кт", "проспект"),
("б р", "бульвар"),
("пр т", "проспект"),
];
const D_CAPITALS: &[&str] = &[
"москва",
"paris",
"roma",
"amsterdam",
"beograd",
"toshkent",
"ташкент",
"s gravenhage",
"санкт петербург",
];
const D_TYPES_CYR: &[&str] = &[
"улица",
"проспект",
"переулок",
"набережная",
"шоссе",
"бульвар",
"площадь",
"проезд",
"аллея",
"тупик",
"квартал",
"микрорайон",
];
const D_TYPES_LATIN: &[&str] = &[
"rue",
"avenue",
"boulevard",
"place",
"impasse",
"allee",
"chemin",
"route",
"cours",
"quai",
"passage",
"square",
"via",
"viale",
"corso",
"piazza",
"largo",
"vicolo",
"strada",
];
const D_TYPES_EXTRA: &[&str] = &[
"straat",
"laan",
"weg",
"plein",
"gracht",
"dijk",
"kade",
"steeg",
"singel",
"hof",
"ulica",
"bulevar",
"trg",
"put",
"kochasi",
"kocha",
"kuchasi",
"mavze",
"mavzesi",
"dahasi",
"daha",
"prospekti",
"piazzale",
"salita",
];
const D_AFFIX_EXTRA: &[&str] = &[
"массив",
"massiv",
"мкр",
"микрорайон",
"квартал",
"kvartal",
"жилой",
"residential",
"комплекс",
"complex",
"жк",
];
const D_NOISE: &[&str] = &["г", "гор", "город", "gorod", "shahri", "sh"];
const D_NOISE_AFTER: &[&str] = &["город", "grad", "sh"];
const D_REGION_MARKERS: &[&str] = &[
"обл",
"область",
"области",
"край",
"края",
"респ",
"республика",
"район",
"района",
"р н",
"tumani",
"tuman",
"viloyat",
"viloyati",
];
const D_COUNTRIES_MID: &[&str] = &[
"france",
"francia",
"frankrijk",
"франция",
"italia",
"italy",
"italie",
"италия",
"srbija",
"serbia",
"сербия",
"србија",
"россия",
"russia",
"рф",
"узбекистан",
"uzbekistan",
"ozbekiston",
"nederland",
"netherlands",
"holland",
"нидерланды",
];
const D_COUNTRIES_TAIL: &[&str] = &[
"france",
"francia",
"франция",
"nederland",
"netherlands",
"holland",
"нидерланды",
"italia",
"italy",
"italie",
"италия",
"srbija",
"serbia",
"сербия",
"србија",
"россия",
"russia",
"рф",
"федерация",
"российская",
"uzbekistan",
"узбекистан",
"ozbekiston",
"europe",
"европа",
];
const D_PLACE_JUNK: &[&str] = &[
"территория",
"тер",
"муниципальный",
"округ",
"поселение",
"городской",
"деревня",
"дер",
"село",
"поселок",
"пос",
"рп",
"пгт",
"аул",
"кишлак",
];
const D_PLACE_PREFIX: &[&str] = &[
"снт",
"днп",
"тсн",
"сдт",
"ст",
"садовое",
"дачное",
"некоммерческое",
"товарищество",
"массив",
"микрорайон",
"мкр",
"квартал",
"город",
"обл",
"область",
"край",
"район",
];
const D_FR_ORD_CITIES: &[&str] = &["paris", "lyon", "marseille"];
const D_PLACE_TYPE_STRIP: &[&str] = &[
"квартал",
"kvartal",
"mavze",
"mavzesi",
"даха",
"daha",
"dahasi",
"массив",
"massiv",
"мкр",
"микрорайон",
];
fn defaults() -> Rules {
let mut r = Rules {
municipality_guard: HashMap::new(),
city_tail: HashMap::new(),
recipient_prefixes: HashSet::new(),
commune_alias: HashMap::new(),
city_alias: Vec::new(),
street_rewrites: Vec::new(),
abbrev2: Vec::new(),
capitals: Vec::new(),
types_cyr: Vec::new(),
types_latin: Vec::new(),
types_extra: HashSet::new(),
affix_extra: HashSet::new(),
noise: HashSet::new(),
noise_after: HashSet::new(),
region_markers: HashSet::new(),
countries_mid: HashSet::new(),
countries_tail: HashSet::new(),
place_junk: HashSet::new(),
place_prefix: HashSet::new(),
fr_ord_cities: HashSet::new(),
place_type_strip: HashSet::new(),
de_city_alias: Vec::new(),
de_abbrev2: Vec::new(),
de_street_types: Vec::new(),
de_countries: HashSet::new(),
de_admin_tail: HashSet::new(),
};
for (k, v) in D_COMMUNE_ALIAS {
r.add(1, k.to_string(), v.to_string());
}
for (k, v) in D_CITY_ALIASES {
r.add(2, k.to_string(), v.to_string());
}
for (k, v) in D_ABBREV2 {
r.add(3, k.to_string(), v.to_string());
}
let lists: &[(u8, &[&str])] = &[
(4, D_CAPITALS),
(5, D_TYPES_CYR),
(16, D_TYPES_LATIN),
(17, D_TYPES_EXTRA),
(6, D_AFFIX_EXTRA),
(7, D_NOISE),
(8, D_NOISE_AFTER),
(9, D_REGION_MARKERS),
(10, D_COUNTRIES_MID),
(11, D_COUNTRIES_TAIL),
(12, D_PLACE_JUNK),
(13, D_PLACE_PREFIX),
(14, D_FR_ORD_CITIES),
(15, D_PLACE_TYPE_STRIP),
];
for (class, words) in lists {
for w in *words {
r.add(*class, w.to_string(), String::new());
}
}
r
}
static FALLBACK: OnceLock<Rules> = OnceLock::new();
thread_local! {
static CURRENT: Cell<Option<&'static Rules>> = const { Cell::new(None) };
}
pub fn defaults_static() -> &'static Rules {
FALLBACK.get_or_init(defaults)
}
pub struct Scope(Option<&'static Rules>);
impl Drop for Scope {
fn drop(&mut self) {
CURRENT.with(|c| c.set(self.0));
}
}
#[must_use]
pub fn scope(r: &'static Rules) -> Scope {
CURRENT.with(|c| Scope(c.replace(Some(r))))
}
pub fn rules() -> &'static Rules {
CURRENT.with(|c| c.get()).unwrap_or_else(defaults_static)
}
pub fn from_section(bytes: &[u8]) -> &'static Rules {
if bytes.len() < 4 {
return defaults_static();
}
let declared = u32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
let entries = parse_entries(bytes);
if entries.len() != declared || entries.is_empty() {
return defaults_static();
}
Box::leak(Box::new(rules_from_entries(entries)))
}
pub fn from_section_owned(bytes: &[u8]) -> Option<Box<Rules>> {
if bytes.len() < 4 {
return None;
}
let declared = u32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
let entries = parse_entries(bytes);
if entries.len() != declared || entries.is_empty() {
return None;
}
Some(Box::new(rules_from_entries(entries)))
}
pub fn parse_entries(bytes: &[u8]) -> Vec<(u8, String, String)> {
let mut out = Vec::new();
if bytes.len() < 4 {
return out;
}
let n = u32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
let mut p = 4usize;
for _ in 0..n {
if p + 3 > bytes.len() {
return out;
}
let class = bytes[p];
let kl = u16::from_le_bytes(bytes[p + 1..p + 3].try_into().unwrap()) as usize;
p += 3;
if p + kl + 2 > bytes.len() {
return out;
}
let key = String::from_utf8_lossy(&bytes[p..p + kl]).into_owned();
p += kl;
let vl = u16::from_le_bytes(bytes[p..p + 2].try_into().unwrap()) as usize;
p += 2;
if p + vl > bytes.len() {
return out;
}
out.push((
class,
key,
String::from_utf8_lossy(&bytes[p..p + vl]).into_owned(),
));
p += vl;
}
out
}
fn rules_from_entries(mut entries: Vec<(u8, String, String)>) -> Rules {
entries.sort();
let mut r = defaults(); for (class, key, val) in entries {
r.add(class, key, val);
}
r
}
pub fn serialize_entries(entries: &[(u8, String, String)]) -> Vec<u8> {
let mut out = Vec::new();
out.extend_from_slice(&(entries.len() as u32).to_le_bytes());
for (class, key, val) in entries {
out.push(*class);
let kb = key.as_bytes();
let vb = val.as_bytes();
out.extend_from_slice(&(kb.len().min(65535) as u16).to_le_bytes());
out.extend_from_slice(&kb[..kb.len().min(65535)]);
out.extend_from_slice(&(vb.len().min(65535) as u16).to_le_bytes());
out.extend_from_slice(&vb[..vb.len().min(65535)]);
}
out
}
pub fn entries_from_tsv_dir(dir: &Path) -> std::io::Result<Vec<(u8, String, String)>> {
let mut out = Vec::new();
for (class, name) in CLASSES {
let p = dir.join(format!("{name}.tsv"));
if !p.exists() {
continue;
}
for line in std::fs::read_to_string(&p)?.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let cols: Vec<&str> = line.split('\t').collect();
let (actual_class, key, val) = match (*name, cols.as_slice()) {
("city_alias", ["de", key, val, ..]) => {
(18, (*key).to_string(), (*val).to_string())
}
("abbrev2", ["de", a, b, val, ..]) => (19, format!("{a} {b}"), (*val).to_string()),
("street_types_latin", ["de", key, ..]) => (20, (*key).to_string(), String::new()),
("countries_mid" | "countries_tail", ["de", key, ..]) => {
(21, (*key).to_string(), String::new())
}
("place_prefix" | "place_type_strip", ["de", key, ..]) => {
(22, (*key).to_string(), String::new())
}
("abbrev2", ["street", key, val]) => (24, (*key).to_string(), (*val).to_string()),
("abbrev2", ["recipient", key, "1"]) => (25, (*key).to_string(), String::new()),
("abbrev2", [a, b, val, ..]) => (*class, format!("{a} {b}"), (*val).to_string()),
(_, [key, val, ..]) => (*class, (*key).to_string(), (*val).to_string()),
(_, [key]) => (*class, (*key).to_string(), String::new()),
_ => continue,
};
out.push((actual_class, key, val));
}
}
Ok(out)
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
pub(crate) fn write_fixture_rules(dir: &Path) -> std::path::PathBuf {
std::fs::create_dir_all(dir).unwrap();
for (name, rows) in [
(
"city_alias.tsv",
"de\tmunich\tmunchen\nde\tcologne\tkoln\nde\tnuremberg\tnurnberg\nde\tfrankfurt main\tfrankfurt am main\n",
),
(
"abbrev2.tsv",
"de\ta\tm\tam main\nde\ta\td\tan der\nde\ta\trh\tam rhein\nde\ti\tbay\tin bayern\n",
),
(
"street_types_latin.tsv",
"de\tstraße\nde\tstrasse\nde\tstr\nde\tweg\nde\tallee\nde\tplatz\nde\tgasse\nde\tdamm\nde\tufer\n",
),
("countries_mid.tsv", "de\tdeutschland\nde\tgermany\n"),
("countries_tail.tsv", "de\tdeutschland\nde\tgermany\n"),
("place_prefix.tsv", "de\tot\nde\tortsteil\n"),
("place_type_strip.tsv", "de\tot\nde\tortsteil\n"),
] {
std::fs::write(dir.join(name), rows).unwrap();
}
dir.to_path_buf()
}
fn sample_entries() -> Vec<(u8, String, String)> {
let s = |x: &str| x.to_string();
vec![
(2, s("spb"), s("sankt peterburg")),
(2, s("bg"), s("beograd")),
(3, s("p zza"), s("piazza")),
(3, s("c so"), s("corso")),
(4, s("paris"), String::new()),
(4, s("roma"), String::new()),
(5, s("ulica"), String::new()),
(16, s("rue"), String::new()),
(17, s("straat"), String::new()),
(18, s("munich"), s("munchen")),
(19, s("a m"), s("am main")),
(20, s("straße"), String::new()),
(21, s("germany"), String::new()),
(22, s("ortsteil"), String::new()),
]
}
#[test]
fn rule_install_does_not_depend_on_entry_arrival_order() {
let base = rules_from_entries(sample_entries());
for rotate in 1..sample_entries().len() {
let mut permuted = sample_entries();
permuted.rotate_left(rotate);
let other = rules_from_entries(permuted);
assert_eq!(
base.city_alias, other.city_alias,
"city_alias order leaked (rotate {rotate})"
);
assert_eq!(
base.abbrev2, other.abbrev2,
"abbrev2 order leaked (rotate {rotate})"
);
assert_eq!(
base.capitals, other.capitals,
"capitals order leaked (rotate {rotate})"
);
assert_eq!(
base.types_cyr, other.types_cyr,
"types_cyr order leaked (rotate {rotate})"
);
assert_eq!(
base.types_latin, other.types_latin,
"types_latin order leaked (rotate {rotate})"
);
assert_eq!(base.de_city_alias, other.de_city_alias);
assert_eq!(base.de_abbrev2, other.de_abbrev2);
assert_eq!(base.de_street_types, other.de_street_types);
}
let mut reversed = sample_entries();
reversed.reverse();
let other = rules_from_entries(reversed);
assert_eq!(base.city_alias, other.city_alias);
assert_eq!(base.abbrev2, other.abbrev2);
assert_eq!(base.de_city_alias, other.de_city_alias);
assert_eq!(base.de_abbrev2, other.de_abbrev2);
}
#[test]
fn country_scoped_de_rows_never_enter_generic_rule_families() {
let dir = write_fixture_rules(&std::env::temp_dir().join(format!(
"gridpin-de-rule-family-fixture-{}",
std::process::id()
)));
let entries = entries_from_tsv_dir(&dir).unwrap();
let rules = rules_from_entries(entries);
assert!(rules
.de_city_alias
.contains(&("munich".to_string(), "munchen".to_string())));
assert!(rules
.de_abbrev2
.contains(&("a m".to_string(), "am main".to_string())));
assert!(rules.de_street_types.iter().any(|value| value == "straße"));
assert!(rules.de_countries.contains("germany"));
assert!(rules.de_admin_tail.contains("ortsteil"));
assert!(!rules.city_alias.iter().any(|(key, _)| key == "munich"));
assert!(!rules.abbrev2.iter().any(|(key, _)| key == "a m"));
assert!(!rules.types_latin.iter().any(|value| value == "straße"));
assert!(!rules.countries_mid.contains("germany"));
assert!(!rules.countries_tail.contains("germany"));
assert!(!rules.place_prefix.contains("ortsteil"));
assert!(!rules.place_type_strip.contains("ortsteil"));
}
#[test]
fn corrupt_section_installs_nothing() {
let good = serialize_entries(&sample_entries());
for cut in [1usize, 5, 9, good.len() - 1] {
let truncated = &good[..cut.min(good.len())];
let parsed = parse_entries(truncated);
assert!(
parsed.len() < sample_entries().len(),
"truncation at {cut} parsed too much"
);
}
}
}