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::*;

bitflags::bitflags! {
  /// A declaration's shape, hand-written: `#[ingraph::flags(u32)]` emits exactly this plus the two
  /// constants below, so a test written against it is a test of what a declaration gets.
  #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  pub struct Disposition: u32 {
    const DEFAULT = 0x1u32;
    const FORCED = 0x2u32;
    const HEARING_IMPAIRED = 0x4u32;
  }
}

impl FlagsValue for Disposition {
  const GRAPHQL_NAME: &'static str = "Disposition";
  const FIELDS: &'static [&'static str] = &["default", "forced", "hearingImpaired"];
}

/// **The bit table is one table read twice**, so the word a text form spells and the field a
/// selection set writes are two casings of one declaration and never two lists that drifted.
#[test]
fn every_bit_carries_its_word_its_field_and_its_value() {
  let table: Vec<_> = bits::<Disposition>().collect();

  assert_eq!(
    table
      .iter()
      .map(|bit| (bit.word, bit.field))
      .collect::<Vec<_>>(),
    [
      ("DEFAULT", "default"),
      ("FORCED", "forced"),
      ("HEARING_IMPAIRED", "hearingImpaired"),
    ]
  );
  assert_eq!(
    table.iter().map(|bit| bit.value).collect::<Vec<_>>(),
    [
      Disposition::DEFAULT,
      Disposition::FORCED,
      Disposition::HEARING_IMPAIRED
    ]
  );
}

/// The whole declared mask, and the empty value beside it — the two operands every sugar word is
/// written against.
#[test]
fn the_mask_is_every_declared_bit_and_no_other() {
  assert_eq!(all::<Disposition>().bits(), 0b111);
  assert_eq!(empty::<Disposition>().bits(), 0);
  assert!(all::<Disposition>().is_all());
  assert!(empty::<Disposition>().is_empty());
}

/// **The strict text form is `bitflags`' own grammar**, in declaration order, ` | `-separated.
#[test]
fn the_text_form_is_the_declared_words_in_declaration_order() {
  assert_eq!(text(&Disposition::empty()).as_str(), "");
  assert_eq!(text(&Disposition::FORCED).as_str(), "FORCED");
  assert_eq!(
    text(&(Disposition::HEARING_IMPAIRED | Disposition::DEFAULT)).as_str(),
    "DEFAULT | HEARING_IMPAIRED",
    "declaration order, not the order the operands were written in"
  );
}

/// **A one-bit text is the declared word itself**, taken through `Utf8Bytes::from_static` rather
/// than assembled — which is what buys the `Utf8Bytes` seat, since that constructor inlines a short
/// word and borrows a long one and allocates in neither case.
#[test]
fn a_single_bit_takes_its_declared_word_whole() {
  assert_eq!(
    text(&Disposition::HEARING_IMPAIRED),
    Utf8Bytes::from_static("HEARING_IMPAIRED")
  );
}

/// The name list carries WORDS, each of them the declared static.
#[test]
fn the_name_list_is_the_set_bits_words() {
  let listed = names(&(Disposition::DEFAULT | Disposition::HEARING_IMPAIRED));
  assert_eq!(
    listed.iter().map(Utf8Bytes::as_str).collect::<Vec<_>>(),
    ["DEFAULT", "HEARING_IMPAIRED"]
  );
  assert!(names(&Disposition::empty()).is_empty());
}

/// **The three operand doors agree.** One value, three spellings, one answer — which is what a wide
/// door means, and what keeps a client free to write whichever form its own layer produces.
#[test]
fn the_list_the_text_and_the_bits_read_one_value() {
  let expected = Disposition::DEFAULT | Disposition::FORCED;

  assert_eq!(
    of_words::<Disposition>(["DEFAULT", "FORCED"]).expect("both words are declared"),
    expected
  );
  assert_eq!(
    of_text::<Disposition>("DEFAULT | FORCED").expect("the text form is the declared grammar"),
    expected
  );
  assert_eq!(
    of_bits::<Disposition>(0b11).expect("both bits are declared"),
    expected
  );
}

/// The text door takes a HEX token, which is `bitflags`' grammar — and takes it only while every
/// bit it names is declared.
#[test]
fn the_text_door_reads_a_hex_token_within_the_declared_mask() {
  assert_eq!(
    of_text::<Disposition>("0x3").expect("both bits are declared"),
    Disposition::DEFAULT | Disposition::FORCED
  );
  assert_eq!(
    of_text::<Disposition>("DEFAULT | 0x4").expect("a name and a token in one text"),
    Disposition::DEFAULT | Disposition::HEARING_IMPAIRED
  );
}

/// A single name is the text form with one element — the sugar is ABSORBED rather than being a
/// fourth door, so there is no second parse to keep in step.
#[test]
fn one_name_is_the_text_form_of_length_one() {
  assert_eq!(
    of_text::<Disposition>("FORCED").expect("one declared word"),
    Disposition::FORCED
  );
  assert_eq!(
    of_text::<Disposition>("").expect("the empty text is the empty set"),
    Disposition::empty()
  );
}

/// **An undeclared bit is refused at every door**, and the refusal names the offending spelling.
#[test]
fn every_door_refuses_a_bit_the_declaration_never_named() {
  let by_word =
    of_words::<Disposition>(["DEFAULT", "COMMENTARY"]).expect_err("`COMMENTARY` is not declared");
  assert_eq!(by_word.word(), "COMMENTARY");

  let by_token = of_text::<Disposition>("0x8").expect_err("bit 3 is not declared");
  assert_eq!(by_token.word(), "0x8");

  let by_bits = of_bits::<Disposition>(0b1001).expect_err("bit 3 is not declared");
  assert_eq!(
    by_bits.word(),
    "0x8",
    "the UNDECLARED bits, not the whole value"
  );
}

/// A text the grammar does not accept at all is refused whole, because what is wrong with it is its
/// shape rather than one token.
#[test]
fn an_ill_formed_text_is_refused_whole() {
  let refused =
    of_text::<Disposition>("DEFAULT |").expect_err("a trailing separator is not a flag");
  assert_eq!(refused.word(), "DEFAULT |");
}

/// The sentence every door quotes back: the declared words and the whole mask, assembled once so
/// that two refusals cannot describe one vocabulary differently.
#[test]
fn the_refusal_names_the_declared_words_and_the_whole_mask() {
  let listed = vocabulary::<Disposition>();
  for word in ["`DEFAULT`", "`FORCED`", "`HEARING_IMPAIRED`"] {
    assert!(listed.contains(word), "{listed}");
  }
  assert!(
    listed.contains("DEFAULT | FORCED | HEARING_IMPAIRED"),
    "{listed}"
  );
}

/// **`isAll` is an EQUALITY under this crate's strictness**, which is the identity the filter's
/// sugar is written on: no value in the system carries an undeclared bit, so `is_all` — which
/// tolerates one — and `bits == ALL` cannot disagree.
#[test]
fn is_all_and_equality_with_the_mask_are_one_question() {
  for bits in 0u32..=0b111 {
    let value = Disposition::from_bits(bits).expect("every pattern inside the mask is declared");
    assert_eq!(value.is_all(), value == all::<Disposition>(), "{bits:#b}");
  }
}