radio-cli 0.12.0

A lightweight terminal radio and podcast player
Documentation
//! Chinese display names from Unicode CLDR 48, regenerated with
//! `uv run scripts/update_territories.py`. See locales/LICENSE.unicode.
//! This table contains display names only, not station catalog records.

use std::{collections::BTreeMap, sync::OnceLock};

fn names() -> &'static BTreeMap<String, String> {
    static NAMES: OnceLock<BTreeMap<String, String>> = OnceLock::new();
    NAMES.get_or_init(|| {
        serde_json::from_str(include_str!("../locales/territories.zh-CN.json"))
            .expect("embedded CLDR Chinese territory names")
    })
}

fn name_zh(code: &str) -> Option<&'static str> {
    let code = code.trim().to_ascii_uppercase();
    names().get(code.as_str()).map(String::as_str)
}

/// `name (CODE)` for known codes and the bare code otherwise.
pub(crate) fn display_zh(code: &str) -> String {
    let code = code.trim().to_ascii_uppercase();
    match name_zh(&code) {
        Some(name) => format!("{name} ({code})"),
        None => code,
    }
}

/// Localized label that keeps the upstream catalog name when the code is
/// missing or absent from the CLDR table, so no catalog metadata is dropped.
pub(crate) fn display_in(locale: crate::messages::Locale, code: &str, upstream: &str) -> String {
    let code = code.trim().to_ascii_uppercase();
    if locale == crate::messages::Locale::ZhCn
        && let Some(name) = name_zh(&code)
    {
        return format!("{name} ({code})");
    }
    let upstream = upstream.trim();
    if upstream.is_empty() {
        return code;
    }
    if code.is_empty() {
        return upstream.to_owned();
    }
    format!("{upstream} ({code})")
}

pub(crate) fn matching_codes(query: &str) -> impl Iterator<Item = &'static str> + '_ {
    // A single character is too ambiguous to widen a station-name search:
    // "岛" alone matches 32 territories and "国" matches 15.
    let query = query.trim();
    let eligible = query.chars().count() >= 2;
    names()
        .iter()
        .filter(move |(_, name)| eligible && name.contains(query))
        .map(|(code, _)| code.as_str())
}

/// Facet completion accepts single characters and matches name or alias prefixes.
/// Empty prefixes already match all catalog codes without name expansion.
pub(crate) fn matching_prefix_codes(prefix: &str) -> impl Iterator<Item = &'static str> + '_ {
    let prefix = prefix.trim();
    names()
        .iter()
        .filter(move |(code, name)| {
            // Search-only aliases keep the full CLDR display names intact.
            let alias = match code.as_str() {
                "HK" => Some("香港"),
                "MO" => Some("澳门"),
                _ => None,
            };
            !prefix.is_empty()
                && (name.starts_with(prefix) || alias.is_some_and(|name| name.starts_with(prefix)))
        })
        .map(|(code, _)| code.as_str())
}