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 core::str::FromStr;

use smol_bytes::Utf8Bytes;

use super::WritingSystem;

/// Every member's word round-trips, and the escape is not one of them.
#[test]
fn the_roster_round_trips_through_its_own_words() {
  assert_eq!(WritingSystem::VARIANTS.len(), 35);
  assert!(
    WritingSystem::VARIANTS
      .iter()
      .all(|member| !member.is_other()),
    "the escape is not a member"
  );

  for member in WritingSystem::VARIANTS {
    assert_eq!(&WritingSystem::new(member.as_str()), member);

    // OUT THROUGH `Display`, back in through `FromStr` — the round trip spelled as the two halves it
    // is, rather than as one call that happens to be equivalent.
    let written = member.to_string();
    assert_eq!(written, member.as_str());
    assert_eq!(
      &WritingSystem::from_str(&written).expect("an open door cannot refuse"),
      member
    );
  }
}

/// **THE STANDARD'S OWN SPELLING PARSES**, which is what makes this ISO 15924 and not fourteen
/// four-letter words: a BCP-47 script subtag arrives title-cased and lands on the same value the
/// framework's own lowercase word does.
#[test]
fn the_door_folds_case() {
  for spelling in ["latn", "Latn", "LATN", "lAtN"] {
    assert_eq!(
      WritingSystem::new(spelling),
      WritingSystem::Latn,
      "at {spelling}"
    );
  }
  for spelling in ["hani", "Hani", "HANI"] {
    assert_eq!(
      WritingSystem::new(spelling),
      WritingSystem::Hani,
      "at {spelling}"
    );
  }
  assert_eq!(WritingSystem::new("Zzzz"), WritingSystem::Zzzz);
  assert_eq!(WritingSystem::new("Zyyy"), WritingSystem::Zyyy);
  assert_eq!(WritingSystem::new("Zinh"), WritingSystem::Zinh);
}

/// **THE ESCAPE CARRIES THE FOLDED WORD**, which is what makes its `Eq` about names rather than
/// spellings.
#[test]
fn a_word_outside_the_roster_escapes_folded() {
  let written = WritingSystem::new("Cher");
  assert!(written.is_other());
  assert_eq!(written.as_str(), "cher");
  assert_eq!(
    written,
    WritingSystem::Other(Utf8Bytes::from_static("cher"))
  );
  // Two spellings of one unrostered code are ONE value.
  assert_eq!(written, WritingSystem::new("CHER"));
  assert_eq!(written, WritingSystem::new("cher"));
  assert_ne!(written, WritingSystem::new("runr"));
  // And it reads back out through the same door.
  assert_eq!(WritingSystem::new(written.as_str()), written);
}

/// **THE TWO ESCAPES ARE DIFFERENT QUESTIONS**, and the type keeps them apart.
#[test]
fn a_codepoint_never_produces_the_word_escape() {
  assert!(
    !WritingSystem::of('\u{16A0}').is_other(),
    "runic is Zzzz, not Other"
  );
  assert_eq!(WritingSystem::of('\u{16A0}'), WritingSystem::Zzzz);
  assert!(WritingSystem::new("runr").is_other());
  assert_ne!(WritingSystem::new("runr"), WritingSystem::Zzzz);
}

/// The predicate the segmenter's resolution is written in, and the one value it deliberately
/// excludes.
#[test]
fn only_common_and_inherited_are_contextual() {
  for member in WritingSystem::VARIANTS {
    let contextual = matches!(member, WritingSystem::Zyyy | WritingSystem::Zinh);
    assert_eq!(member.is_contextual(), contextual, "at {member}");
  }
  assert!(
    !WritingSystem::Zzzz.is_contextual(),
    "the fallback bucket cuts its own run rather than being swallowed"
  );
  assert!(!WritingSystem::new("cher").is_contextual());
}

/// The two conversions a caller reaches the vocabulary through.
#[test]
fn the_crossings_agree_with_the_door() {
  assert_eq!(WritingSystem::from("hani"), WritingSystem::Hani);
  assert_eq!(WritingSystem::Hani.as_ref(), "hani");
  assert_eq!(AsRef::<str>::as_ref(&WritingSystem::new("cher")), "cher");
}