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
//! The scalar doors, read the way a client reads them — through the server error a refusal becomes.
//!
//! What is pinned here is the JSON SHAPE and nothing about the subtag grammar: the grammar is the
//! type's, tested at the type, and a door that read it differently would be a second grammar. So
//! each of these asks two questions — does a string reach the type's own `new`, and is everything
//! else refused — plus the one property a door owns outright, which is STRICT OUT.

use async_graphql::{Pos, ScalarType, Value};

use crate::primitives::lang::{Language, Region, ScriptSubtag};

/// One operand through the door, or the sentence it was refused with.
fn door(sent: Value) -> Result<Language, String> {
  <Language as ScalarType>::parse(sent)
    .map_err(|refused| refused.into_server_error(Pos::default()).message)
}

/// One operand the door must take, unwrapped.
fn admitted(sent: Value) -> Language {
  door(sent.clone()).unwrap_or_else(|refused| panic!("{sent} is a language: {refused}"))
}

/// **WIDE IN.** Every spelling the type folds reaches the door unchanged, because the door IS the
/// type's own `new`.
#[test]
fn the_door_is_the_types_own_grammar() {
  for (sent, canonical) in [
    ("de", "de"),
    ("DE", "de"),
    ("ger", "de"),
    ("deu", "de"),
    ("iw", "he"),
    ("und", "und"),
    ("xyz", "xyz"),
  ] {
    assert_eq!(admitted(Value::from(sent)).as_str(), canonical, "`{sent}`");
  }
}

/// **STRICT OUT.** Every spelling of one language is written back as the same one word, so a value
/// read from this schema and sent back to it is the same value by the shortest route.
#[test]
fn every_spelling_is_written_back_as_the_canonical_one() {
  let canonical = Value::from("de");

  for sent in ["de", "DE", "De", "ger", "GER", "deu"] {
    assert_eq!(
      admitted(Value::from(sent)).to_value(),
      canonical,
      "`{sent}`"
    );
  }
}

/// **A NUMBER IS REFUSED**, which matters because one of this family's grammars is digits.
///
/// `419` is a region subtag and its door reads it — from the STRING. A JSON number has no leading
/// zeros, so `001` could not survive the round trip as one, and a door admitting `419` while
/// refusing `001` would have a grammar that depended on which area a client asked about.
#[test]
fn a_non_string_is_refused() {
  for sent in [
    Value::from(419),
    Value::from(1.0),
    Value::Boolean(true),
    Value::Null,
    Value::List(vec![Value::from("de")]),
  ] {
    assert!(door(sent.clone()).is_err(), "{sent}");
  }
}

/// The refusal carries the TYPE's sentence, so a client is told which rule broke rather than that
/// its input was malformed.
#[test]
fn a_refusal_names_the_rule_that_broke() {
  let underscore = door(Value::from("zh_CN")).expect_err("an underscore is not a letter");
  assert!(underscore.contains('_'), "{underscore}");
  assert!(underscore.contains("letters"), "{underscore}");

  let short = door(Value::from("e")).expect_err("one letter is not a subtag");
  assert!(short.contains("two letters"), "{short}");
}

/// **`is_valid` and `parse` are ONE rule**, so a query that validates is a query that parses.
///
/// async-graphql's default validator answers `true` for every value, so a scalar that narrows in
/// `parse` alone ships a validator admitting what its own parser rejects — and the failure lands at
/// execution rather than at validation, where a client is looking.
#[test]
fn validation_and_parsing_admit_the_same_values() {
  for sent in [
    Value::from("de"),
    Value::from("GER"),
    Value::from("xyz"),
    Value::from("zh_CN"),
    Value::from("e"),
    Value::from(419),
    Value::Null,
  ] {
    assert_eq!(
      <Language as ScalarType>::is_valid(&sent),
      door(sent.clone()).is_ok(),
      "{sent}"
    );
  }
}

// ---------------------------------------------------------------------------------------------
// The script's door — the same three questions, and the one answer that is this type's own.
// ---------------------------------------------------------------------------------------------

/// One script through its door, or the sentence it was refused with.
fn script_door(sent: Value) -> Result<ScriptSubtag, String> {
  <ScriptSubtag as ScalarType>::parse(sent)
    .map_err(|refused| refused.into_server_error(Pos::default()).message)
}

/// **WIDE IN, STRICT OUT** over a script: every case reaches the door, and Titlecase comes back.
#[test]
fn the_script_door_folds_case_and_writes_back_titlecase() {
  let canonical = Value::from("Latn");

  for sent in ["Latn", "latn", "LATN", "lAtN"] {
    let held =
      script_door(Value::from(sent)).unwrap_or_else(|refused| panic!("`{sent}`: {refused}"));
    assert_eq!(held.to_value(), canonical, "`{sent}`");
  }
}

/// **THE TWO HAN VARIANTS ARE TWO OPERANDS**, read at the door — so a client that sends `Hant` gets
/// `Hant` back and never a folded `Hans`.
#[test]
fn the_script_door_folds_no_han_variant_onto_the_other() {
  let simplified = script_door(Value::from("hans")).expect("a script");
  let traditional = script_door(Value::from("HANT")).expect("a script");

  assert_eq!(simplified.to_value(), Value::from("Hans"));
  assert_eq!(traditional.to_value(), Value::from("Hant"));
  assert_ne!(simplified, traditional);
}

/// The script's door refuses the same JSON shapes and names the same kind of rule.
#[test]
fn the_script_door_refuses_by_name() {
  let short = script_door(Value::from("Lat")).expect_err("three letters is not a script");
  assert!(short.contains("exactly four letters"), "{short}");

  assert!(
    script_door(Value::from(4)).is_err(),
    "a number is not a script"
  );
  assert!(script_door(Value::Null).is_err());

  for sent in [
    Value::from("Latn"),
    Value::from("lat"),
    Value::from("Lat1"),
    Value::from(4),
  ] {
    assert_eq!(
      <ScriptSubtag as ScalarType>::is_valid(&sent),
      script_door(sent.clone()).is_ok(),
      "{sent}"
    );
  }
}

// ---------------------------------------------------------------------------------------------
// The region's door — two grammars, and the JSON-number refusal that keeps them one door.
// ---------------------------------------------------------------------------------------------

/// One region through its door, or the sentence it was refused with.
fn region_door(sent: Value) -> Result<Region, String> {
  <Region as ScalarType>::parse(sent)
    .map_err(|refused| refused.into_server_error(Pos::default()).message)
}

/// **BOTH GRAMMARS ARRIVE AS STRINGS**, and the case fold reaches only the one that has a case.
#[test]
fn the_region_door_reads_both_grammars_from_strings() {
  for (sent, canonical) in [
    ("DE", "DE"),
    ("de", "DE"),
    ("dE", "DE"),
    ("419", "419"),
    ("001", "001"),
    ("BU", "MM"),
    ("ZZ", "ZZ"),
  ] {
    let held =
      region_door(Value::from(sent)).unwrap_or_else(|refused| panic!("`{sent}`: {refused}"));
    assert_eq!(held.to_value(), Value::from(canonical), "`{sent}`");
  }
}

/// **A JSON NUMBER IS REFUSED, AND `001` IS WHY.**
///
/// The M.49 codes are the one place in this family where a number LOOKS like it would work. It
/// would not: a JSON number has no leading zeros, so `001` cannot survive the round trip as one,
/// and a door that admitted `419` while refusing `001` would have a grammar that depended on which
/// area a client asked about. Both are strings, and neither is special.
#[test]
fn a_numeric_region_operand_is_refused_so_that_leading_zeros_survive() {
  assert!(
    region_door(Value::from(419)).is_err(),
    "even though `419` is a region"
  );
  assert!(region_door(Value::from(1)).is_err());

  assert_eq!(
    region_door(Value::from("001"))
      .expect("a region")
      .to_value(),
    Value::from("001"),
    "the zeros are part of the code"
  );
  assert!(
    region_door(Value::from("1")).is_err(),
    "and are not stripped or restored"
  );
}

/// The region's refusals name which of the two grammars was nearly satisfied.
#[test]
fn a_region_refusal_names_the_arm_it_was_nearest() {
  let letters = region_door(Value::from("DEU")).expect_err("three letters is not a region");
  assert!(letters.contains("exactly two"), "{letters}");

  let digits = region_door(Value::from("41")).expect_err("two digits is not a region");
  assert!(digits.contains("exactly three"), "{digits}");

  let mixed = region_door(Value::from("D1")).expect_err("neither grammar");
  assert!(mixed.contains("never a mixture"), "{mixed}");

  for sent in [
    Value::from("DE"),
    Value::from("419"),
    Value::from("DEU"),
    Value::from("D1"),
    Value::from(419),
  ] {
    assert_eq!(
      <Region as ScalarType>::is_valid(&sent),
      region_door(sent.clone()).is_ok(),
      "{sent}"
    );
  }
}