spoolframe-types 0.17.1

Spoolframe's client-facing types: the media model and its identifiers.
Documentation
//! Language codes as files write them: ISO 639-2 in streams (`eng`, and
//! both `ger` and `deu`), often ISO 639-1 in subtitle file names (`en`).
//! [`same_language`] treats all spellings of one language as equal.

/// Two- and three-letter codes of one language: 639-1, 639-2/B, 639-2/T.
/// The languages files commonly carry; a code not here only equals itself.
const ALIASES: &[&[&str]] = &[
    &["ar", "ara"],
    &["bg", "bul"],
    &["ca", "cat"],
    &["cs", "cze", "ces"],
    &["da", "dan"],
    &["de", "ger", "deu"],
    &["el", "gre", "ell"],
    &["en", "eng"],
    &["es", "spa"],
    &["et", "est"],
    &["fa", "per", "fas"],
    &["fi", "fin"],
    &["fr", "fre", "fra"],
    &["he", "heb"],
    &["hi", "hin"],
    &["hr", "hrv"],
    &["hu", "hun"],
    &["id", "ind"],
    &["is", "ice", "isl"],
    &["it", "ita"],
    &["ja", "jpn"],
    &["ko", "kor"],
    &["lt", "lit"],
    &["lv", "lav"],
    &["ms", "may", "msa"],
    &["nb", "nob"],
    &["nl", "dut", "nld"],
    &["no", "nor"],
    &["pl", "pol"],
    &["pt", "por"],
    &["ro", "rum", "ron"],
    &["ru", "rus"],
    &["sk", "slo", "slk"],
    &["sl", "slv"],
    &["sr", "srp"],
    &["sv", "swe"],
    &["th", "tha"],
    &["tr", "tur"],
    &["uk", "ukr"],
    &["vi", "vie"],
    &["zh", "chi", "zho"],
];

/// One spelling for every code of a language: its first in [`ALIASES`],
/// or the code itself, lowercased.
fn canonical(code: &str) -> String {
    let code = code.to_ascii_lowercase();
    ALIASES
        .iter()
        .find(|codes| codes.contains(&code.as_str()))
        .map_or(code, |codes| codes[0].to_owned())
}

/// A language code as BCP 47 (HLS's `LANGUAGE`) writes it: the two-letter
/// code where the language has one (`eng` and `en` are `en`), else the
/// code as it is, lowercased.
pub fn bcp47(code: &str) -> String {
    canonical(code)
}

/// A language code as ISO 639-2 writes it, three letters, the
/// bibliographic code where there are two (`en` and `eng` are `eng`,
/// `de` is `ger`): what MP4's language field holds, and ffmpeg's MP4
/// muxer writes `und` for any other. A code not known stays as it is,
/// lowercased.
pub fn iso639_2(code: &str) -> String {
    let code = code.to_ascii_lowercase();
    ALIASES
        .iter()
        .find(|codes| codes.contains(&code.as_str()))
        .map_or(code, |codes| codes[1].to_owned())
}

/// The language a device's tag names, as [`bcp47`] writes it: `en-US`, and
/// `en_US` as a POSIX locale has it, are `en`. `None` for a tag that names
/// none (`und`, `mul`, `mis`, `zxx`), a private one (`x-foo`) or anything
/// else that is no language: a track tagged `und` must not match it.
pub fn of_tag(tag: &str) -> Option<String> {
    let primary = tag.split(['-', '_']).next()?;
    let language = (2..=3).contains(&primary.len())
        && primary.bytes().all(|b| b.is_ascii_alphabetic())
        && !["und", "mul", "mis", "zxx"]
            .iter()
            .any(|none| primary.eq_ignore_ascii_case(none));
    language.then(|| bcp47(primary))
}

/// Whether two language codes name the same language.
pub fn same_language(a: &str, b: &str) -> bool {
    canonical(a) == canonical(b)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_device_s_tag_is_its_language() {
        for (tag, language) in [
            ("en-US", "en"),
            ("EN_us", "en"),
            ("it", "it"),
            ("zh-Hant-TW", "zh"),
            ("deu-DE", "de"),
            ("sco", "sco"),
        ] {
            assert_eq!(of_tag(tag).as_deref(), Some(language), "{tag}");
        }
    }

    #[test]
    fn a_tag_that_names_no_language_is_none() {
        for tag in ["", "und", "UND-US", "mul", "x-private", "C", "POSIX", "e1"] {
            assert_eq!(of_tag(tag), None, "{tag}");
        }
    }

    #[test]
    fn spellings_of_one_language_are_the_same() {
        assert!(same_language("en", "eng"));
        assert!(same_language("ger", "deu"));
        assert!(same_language("DE", "ger"));
        assert!(same_language("chi", "zho"));
    }

    #[test]
    fn different_languages_differ() {
        assert!(!same_language("eng", "spa"));
        assert!(!same_language("nob", "nor"));
    }

    #[test]
    fn a_code_in_iso_639_2_is_three_letters() {
        assert_eq!(iso639_2("en"), "eng");
        assert_eq!(iso639_2("DE"), "ger");
        assert_eq!(iso639_2("deu"), "ger");
        assert_eq!(iso639_2("spa"), "spa");
        assert_eq!(iso639_2("tlh"), "tlh");
    }

    #[test]
    fn an_unknown_code_equals_only_itself() {
        assert!(same_language("tlh", "TLH"));
        assert!(!same_language("tlh", "eng"));
    }
}