mod script;
mod segment;
pub use script::script_of;
pub use segment::segment_by_script;
use crate::source::input::{DataSource, InputFeature};
use crate::tile::feature::PropValue;
struct Lang {
code: &'static str,
osm: &'static str,
overture: &'static str,
natural_earth: &'static str,
}
macro_rules! langs {
($($code:literal),+ $(,)?) => {
[$(Lang {
code: $code,
osm: concat!("name:", $code),
overture: concat!("names.common.", $code),
natural_earth: concat!("name_", $code),
}),+]
};
}
const ALLOWED: [Lang; 41] = langs![
"ar", "cs", "bg", "da", "de", "el", "en", "es", "et", "fa", "fi", "fr", "ga", "he", "hi", "hr",
"hu", "id", "it", "ja", "ko", "lt", "lv", "ne", "nl", "no", "mr", "mt", "pl", "pt", "ro", "ru",
"sk", "sl", "sv", "tr", "uk", "ur", "vi", "zh-Hans", "zh-Hant",
];
pub fn languages() -> impl Iterator<Item = &'static str> {
ALLOWED.iter().map(|lang| lang.code)
}
#[must_use]
pub fn canonical_language(code: &str) -> Option<&'static str> {
ALLOWED
.iter()
.find(|lang| lang.code.eq_ignore_ascii_case(code))
.map(|lang| lang.code)
}
pub fn push_names(feature: &InputFeature, out: &mut Vec<(String, PropValue)>) {
if let Some(local) = feature
.get_str("names.primary")
.or_else(|| feature.get_str("name"))
{
push_local(local, out);
}
match &feature.source {
DataSource::Osm { .. } => push_langs(feature, out, |l| l.osm, "name:zh"),
DataSource::Overture { .. } => push_langs(feature, out, |l| l.overture, "names.common.zh"),
DataSource::NaturalEarth { .. } => push_natural_earth_langs(feature, out),
_ => {}
}
}
fn push_local(name: &str, out: &mut Vec<(String, PropValue)>) {
const KEYS: [(&str, &str); 3] = [
("name", "script"),
("name2", "script2"),
("name3", "script3"),
];
for (segment, (name_key, script_key)) in segment_by_script(name).iter().zip(KEYS) {
out.push((name_key.to_string(), PropValue::Str(segment.clone())));
let script = script_of(segment);
if script != "Latin" && script != "Generic" {
out.push((script_key.to_string(), PropValue::Str(script)));
}
}
}
fn push_langs(
feature: &InputFeature,
out: &mut Vec<(String, PropValue)>,
key_of: fn(&Lang) -> &'static str,
zh_key: &str,
) {
for lang in &ALLOWED {
let backfill = match lang.code {
"zh-Hans" | "zh-Hant" => feature.get_str(zh_key),
_ => None,
};
if let Some(value) = feature.get_str(key_of(lang)).or(backfill) {
out.push((
format!("name:{}", lang.code),
PropValue::Str(value.to_string()),
));
}
}
}
fn push_natural_earth_langs(feature: &InputFeature, out: &mut Vec<(String, PropValue)>) {
for lang in &ALLOWED {
let column = match lang.code {
"zh-Hans" => "name_zh",
"zh-Hant" => "name_zht",
_ => lang.natural_earth,
};
if let Some(value) = feature.get_str(column) {
out.push((
format!("name:{}", lang.code),
PropValue::Str(value.to_string()),
));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
fn osm(tags: &[(&str, &str)]) -> InputFeature {
feature(
DataSource::Osm {
element_type: crate::source::input::OsmElementType::Node,
relations: Vec::new(),
},
tags,
)
}
fn overture(props: &[(&str, &str)]) -> InputFeature {
feature(
DataSource::Overture {
theme: "divisions".to_string(),
otype: "division".to_string(),
},
props,
)
}
fn natural_earth(columns: &[(&str, &str)]) -> InputFeature {
feature(
DataSource::NaturalEarth {
source_layer: "ne_50m_lakes".to_string(),
},
columns,
)
}
fn feature(source: DataSource, props: &[(&str, &str)]) -> InputFeature {
InputFeature {
id: 1,
geometry: std::sync::Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0))),
properties: props
.iter()
.map(|(k, v)| (k.to_string(), PropValue::Str(v.to_string())))
.collect::<HashMap<_, _>>(),
bbox: None,
source,
}
}
fn names_of(feature: &InputFeature) -> Vec<(String, String)> {
let mut out = Vec::new();
push_names(feature, &mut out);
out.into_iter()
.map(|(k, v)| match v {
PropValue::Str(s) => (k, s),
other => panic!("name attribute {k} is not a string: {other:?}"),
})
.collect()
}
fn pairs(want: &[(&str, &str)]) -> Vec<(String, String)> {
want.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
}
#[test]
fn urdu_place_with_an_english_name() {
let karachi = osm(&[
("place", "city"),
("name", "کراچی"),
("name:en", "Karachi"),
("name:ur", "کراچی"),
]);
assert_eq!(
names_of(&karachi),
pairs(&[
("name", "کراچی"),
("script", "Arabic"),
("name:en", "Karachi"),
("name:ur", "کراچی"),
])
);
}
#[test]
fn latin_local_name_has_no_script() {
let delhi = osm(&[("name", "Delhi"), ("name:hi", "दिल्ली")]);
assert_eq!(
names_of(&delhi),
pairs(&[("name", "Delhi"), ("name:hi", "दिल्ली")])
);
}
#[test]
fn two_script_local_name_is_split() {
let street = osm(&[("name", "Quartier 7 / حارة 7"), ("name:en", "Quarter 7")]);
assert_eq!(
names_of(&street),
pairs(&[
("name", "Quartier 7"),
("name2", "حارة 7"),
("script2", "Arabic"),
("name:en", "Quarter 7"),
])
);
}
#[test]
fn three_script_local_name_keeps_three_segments() {
let casablanca = osm(&[("name", "Casablanca ⴰⵏⴼⴰ الدار البيضاء")]);
assert_eq!(
names_of(&casablanca),
pairs(&[
("name", "Casablanca"),
("name2", "ⴰⵏⴼⴰ"),
("script2", "Tifinagh"),
("name3", "الدار البيضاء"),
("script3", "Arabic"),
])
);
}
#[test]
fn languages_outside_the_allow_list_are_dropped() {
let place = osm(&[
("name", "Köln"),
("name:dk", "x"),
("name:zh", "y"),
("name:de", "Köln"),
("name:fr", "Cologne"),
("name:old", "z"),
("alt_name", "w"),
("name:en:pronunciation", "v"),
]);
let got = names_of(&place);
assert!(got.contains(&("name:de".to_string(), "Köln".to_string())));
assert!(got.contains(&("name:fr".to_string(), "Cologne".to_string())));
for dropped in ["name:dk", "name:old", "alt_name", "name:en:pronunciation"] {
assert!(got.iter().all(|(k, _)| k != dropped), "{dropped} was kept");
}
}
#[test]
fn plain_chinese_fills_both_script_variants() {
let beijing = osm(&[("name", "北京"), ("name:zh", "北京市")]);
assert_eq!(
names_of(&beijing),
pairs(&[
("name", "北京"),
("script", "Han"),
("name:zh-Hans", "北京市"),
("name:zh-Hant", "北京市"),
])
);
}
#[test]
fn explicit_chinese_variants_are_not_overwritten() {
let taipei = osm(&[
("name", "台北"),
("name:zh", "台北市"),
("name:zh-Hant", "臺北市"),
]);
assert_eq!(
names_of(&taipei),
pairs(&[
("name", "台北"),
("script", "Han"),
("name:zh-Hans", "台北市"),
("name:zh-Hant", "臺北市"),
])
);
}
#[test]
fn overture_division_takes_names_primary_and_common() {
let lahore = overture(&[
("names.primary", "لاہور"),
("names.common.en", "Lahore"),
("names.common.ur", "لاہور"),
("names.common.xx", "ignored"),
]);
assert_eq!(
names_of(&lahore),
pairs(&[
("name", "لاہور"),
("script", "Arabic"),
("name:en", "Lahore"),
("name:ur", "لاہور"),
])
);
}
#[test]
fn overture_chinese_fills_both_variants_too() {
let place = overture(&[
("names.primary", "香港"),
("names.common.zh", "香港特别行政区"),
]);
let got = names_of(&place);
assert!(got.contains(&("name:zh-Hans".to_string(), "香港特别行政区".to_string())));
assert!(got.contains(&("name:zh-Hant".to_string(), "香港特别行政区".to_string())));
}
#[test]
fn natural_earth_lake_maps_its_columns() {
let lake = natural_earth(&[
("name", "Ladoga"),
("name_en", "Lake Ladoga"),
("name_ru", "Ладожское озеро"),
("name_zh", "拉多加湖"),
("name_zht", "拉多加湖"),
("name_bn", "not in the list"),
]);
assert_eq!(
names_of(&lake),
pairs(&[
("name", "Ladoga"),
("name:en", "Lake Ladoga"),
("name:ru", "Ладожское озеро"),
("name:zh-Hans", "拉多加湖"),
("name:zh-Hant", "拉多加湖"),
])
);
}
#[test]
fn feature_without_a_name_gets_no_attributes() {
assert!(names_of(&osm(&[("highway", "service")])).is_empty());
assert!(names_of(&osm(&[("name", "")])).is_empty());
}
#[test]
fn languages_are_the_protomaps_allow_list() {
let codes: Vec<_> = languages().collect();
assert_eq!(codes.len(), 41);
assert_eq!(codes.first(), Some(&"ar"));
assert_eq!(codes.last(), Some(&"zh-Hant"));
assert!(codes.contains(&"en") && codes.contains(&"zh-Hans"));
assert!(!codes.contains(&"zh") && !codes.contains(&"dk"));
}
#[test]
fn canonical_language_ignores_case() {
assert_eq!(canonical_language("EN"), Some("en"));
assert_eq!(canonical_language("zh-hans"), Some("zh-Hans"));
assert_eq!(canonical_language("zh"), None);
assert_eq!(canonical_language("dk"), None);
}
}