ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
use super::LanguageTag;

use crate::WritingSystem;

/// What each subtag reduces to — the table the type's own note states, asserted.
#[test]
fn the_reduction_keeps_the_language_and_the_script_and_nothing_else() {
  for (tag, language, script) in [
    ("en", "en", None),
    ("en-US", "en", None),
    ("en-Latn-US", "en", Some("Latn")),
    ("en-Latn-US-posix", "en", Some("Latn")),
    ("zh", "zh", None),
    ("zh-Hant", "zh", Some("Hant")),
    ("zh-Hant-TW", "zh", Some("Hant")),
    ("de-CH-1901", "de", None),
    ("sr-Cyrl-RS", "sr", Some("Cyrl")),
    // Five-to-eight-letter primaries are legal and carry no extlang.
    ("klingon", "klingon", None),
  ] {
    let reduced = LanguageTag::parse(tag).unwrap_or_else(|| panic!("{tag} is well formed"));
    assert_eq!(reduced.language(), language, "at {tag}");
    assert_eq!(reduced.script_subtag(), script, "at {tag}");
  }
}

/// **EXTLANG IS DROPPED**: `zh-cmn` is a variety of `zh`, and the lane is `zh`.
#[test]
fn an_extlang_is_skipped_and_the_script_after_it_still_reads() {
  let reduced = LanguageTag::parse("zh-cmn-Hans-CN").expect("well formed");
  assert_eq!(reduced.language(), "zh");
  assert_eq!(reduced.script_subtag(), Some("Hans"));

  // Three of them is the standard's ceiling, and the script behind them still lands.
  let deep = LanguageTag::parse("zh-aaa-bbb-ccc-Hant").expect("well formed");
  assert_eq!(deep.script_subtag(), Some("Hant"));
}

/// A three-letter subtag after a LONG primary is not an extlang, so it cannot eat a script.
#[test]
fn only_a_short_primary_takes_an_extlang() {
  let reduced = LanguageTag::parse("klingon-abc-Latn").expect("well formed");
  assert_eq!(reduced.language(), "klingon");
  // `abc` is not an extlang here and is not four letters, so nothing is read as a script.
  assert_eq!(reduced.script_subtag(), None);
}

/// `_` reads as a separator beside `-` — the *broad in* half meeting POSIX and Java spellings.
#[test]
fn both_separators_read() {
  let dashed = LanguageTag::parse("zh-Hant-TW").expect("well formed");
  let scored = LanguageTag::parse("zh_Hant_TW").expect("well formed");

  assert_eq!(dashed.language(), scored.language());
  assert_eq!(dashed.script_subtag(), scored.script_subtag());
}

/// What the grammar cannot read — and every one of them is a SHAPE rather than a registry lookup.
#[test]
fn a_tag_with_no_readable_primary_subtag_is_refused() {
  for tag in [
    "",
    "x",
    "x-private",
    "i-klingon",
    "1",
    "12",
    "e",
    "toolongsubtag",
    "zh1",
    "-zh",
  ] {
    assert!(LanguageTag::parse(tag).is_none(), "{tag} should not parse");
  }
}

/// **NO REGISTRY**: a language nobody registered parses fine, because whether it names a lane is the
/// roster's question rather than IANA's.
#[test]
fn an_unregistered_subtag_still_parses() {
  for tag in ["qaa", "zzz", "sw", "xyz"] {
    assert!(LanguageTag::parse(tag).is_some(), "{tag} should parse");
  }
}

/// The crossing into the segmenter's vocabulary: what a tag SAYS, beside what a codepoint IS.
#[test]
fn a_script_subtag_reads_through_the_segmenters_vocabulary() {
  assert_eq!(
    LanguageTag::parse("en-Latn-US").unwrap().script(),
    Some(WritingSystem::Latn)
  );
  assert_eq!(LanguageTag::parse("en-US").unwrap().script(), None);
  // Han is unified, so `Hant` is a word no codepoint could ever answer — it lands on the escape.
  assert_eq!(
    LanguageTag::parse("zh-Hant").unwrap().script(),
    Some(WritingSystem::new("hant"))
  );
  assert!(
    LanguageTag::parse("zh-Hant")
      .unwrap()
      .script()
      .unwrap()
      .is_other()
  );
}

/// The comparison the gate is built out of: case and separator fold, and the SCRIPT is part of the
/// key rather than a wildcard.
#[test]
fn matching_folds_case_and_separators_but_not_the_scripts_presence() {
  let traditional = LanguageTag::parse("zh-Hant-TW").expect("well formed");

  assert!(traditional.matches("zh-Hant"));
  assert!(traditional.matches("zh_hant"));
  assert!(traditional.matches("ZH-HANT"));
  assert!(
    !traditional.matches("zh"),
    "an absent script is not a wildcard"
  );
  assert!(!traditional.matches("zh-Hans"));
  assert!(!traditional.matches("ja"));

  let plain = LanguageTag::parse("en-GB").expect("well formed");
  assert!(plain.matches("en"));
  assert!(plain.matches("EN"));
  assert!(!plain.matches("en-Latn"));

  // A candidate this grammar cannot read matches nothing.
  assert!(!plain.matches("x-private"));
  assert!(!plain.matches(""));
}