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 async_graphql::InputType as _;

use super::*;

bitflags::bitflags! {
  #[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"];
}

/// One operand, read off the wire.
fn read(value: async_graphql::Value) -> Result<Disposition, String> {
  FlagsInput::<Disposition>::parse(Some(value))
    .map(FlagsInput::into_inner)
    .map_err(|error| error.into_server_error(Default::default()).message)
}

/// **The two seats are one word apart**, and neither name is spelled twice — which is what keeps an
/// object and a scalar from claiming one registry entry.
#[test]
fn the_object_and_the_operand_take_two_names_from_one_word() {
  assert_eq!(
    <FlagsView<Disposition> as async_graphql::OutputType>::type_name(),
    "Disposition"
  );
  assert_eq!(
    <FlagsInput<Disposition> as async_graphql::InputType>::type_name(),
    "DispositionInput"
  );
}

/// **The wide door, all three spellings.**
#[test]
fn the_operand_reads_a_list_an_integer_and_the_text_form() {
  let expected = Disposition::DEFAULT | Disposition::FORCED;
  for (spelling, value) in [
    ("list", async_graphql::value!(["DEFAULT", "FORCED"])),
    ("integer", async_graphql::value!(3)),
    ("text", async_graphql::value!("DEFAULT | FORCED")),
    ("one name", async_graphql::value!("DEFAULT | FORCED")),
  ] {
    assert_eq!(
      read(value).unwrap_or_else(|error| panic!("{spelling}: {error}")),
      expected,
      "{spelling}"
    );
  }
}

/// **STRICT OUT.** Three spellings in, one back — so a round trip through this seat is canonical.
#[test]
fn the_operand_renders_the_text_form_whatever_arrived() {
  for value in [
    async_graphql::value!(["DEFAULT", "FORCED"]),
    async_graphql::value!(3),
    async_graphql::value!("FORCED | DEFAULT"),
  ] {
    let parsed = FlagsInput::<Disposition>::parse(Some(value)).expect("a declared operand");
    assert_eq!(
      parsed.to_value(),
      async_graphql::Value::String("DEFAULT | FORCED".to_owned())
    );
  }
}

/// A GraphQL enum literal is NOT a fourth door: an unquoted word reaches this seat as
/// `Value::Enum`, and the text form already covers it with quotes.
#[test]
fn an_unquoted_word_is_refused_naming_the_three_forms() {
  let refused = read(async_graphql::Value::Enum(async_graphql::Name::new(
    "DEFAULT",
  )))
  .expect_err("an enum literal is not one of the three forms");
  assert!(refused.contains("is none of the three"), "{refused}");
}

/// Every refusal quotes the vocabulary back, from the one list a schema also advertises.
#[test]
fn every_refusal_names_the_declared_words() {
  for (label, value) in [
    ("an undeclared word", async_graphql::value!(["COMMENTARY"])),
    ("an undeclared bit", async_graphql::value!(8)),
    ("a negative number", async_graphql::value!(-1)),
    ("a boolean", async_graphql::value!(true)),
    ("a non-string element", async_graphql::value!([1])),
  ] {
    let refused = read(value).expect_err(label);
    assert!(refused.contains("`DEFAULT`"), "{label}: {refused}");
  }
}

/// A number wider than the declared width is refused rather than wrapped into a pattern the
/// declaration never wrote.
#[test]
fn a_number_past_the_declared_width_is_refused() {
  let refused = read(async_graphql::value!(4_294_967_296u64)).expect_err("2^32 is not a u32");
  assert!(refused.contains("does not fit the width"), "{refused}");
}

/// **The object face's field list, whole** — the three readings, the two sentences, the six methods,
/// then one boolean per declared bit.
#[test]
fn the_object_face_publishes_twelve_fixed_fields_then_the_fan() {
  assert_eq!(
    fixed_fields().collect::<Vec<_>>(),
    [
      "bits",
      "text",
      "names",
      "isEmpty",
      "isAll",
      "complement",
      "contains",
      "intersects",
      "intersection",
      "union",
      "difference",
      "symmetricDifference",
    ]
  );
  assert_eq!(
    crate::flags::bits::<Disposition>()
      .map(|bit| bit.field)
      .collect::<Vec<_>>(),
    ["default", "forced", "hearingImpaired"]
  );
}

/// The five self-returning fields are exactly the ones a selection may nest into.
#[test]
fn the_self_returning_fields_are_the_five_that_derive_a_value() {
  let nested: Vec<_> = fixed_fields()
    .filter(|field| is_self_returning(field))
    .collect();
  assert_eq!(
    nested,
    [
      "complement",
      "intersection",
      "union",
      "difference",
      "symmetricDifference"
    ]
  );
}

/// **Every derivation is `bitflags`' own operator**, so this crate invents no set algebra.
#[test]
fn the_derivation_table_is_the_vendored_set_algebra() {
  let value = Disposition::DEFAULT | Disposition::FORCED;
  let operand = Disposition::FORCED | Disposition::HEARING_IMPAIRED;

  assert_eq!(
    derive(value, "complement", Disposition::empty()),
    Some(Disposition::HEARING_IMPAIRED)
  );
  assert_eq!(
    derive(value, "intersection", operand),
    Some(Disposition::FORCED)
  );
  assert_eq!(derive(value, "union", operand), Some(Disposition::all()));
  assert_eq!(
    derive(value, "difference", operand),
    Some(Disposition::DEFAULT)
  );
  assert_eq!(
    derive(value, SYMMETRIC_DIFFERENCE, operand),
    Some(Disposition::DEFAULT | Disposition::HEARING_IMPAIRED)
  );
  assert_eq!(derive(value, "bits", operand), None);
}

/// The two predicates, and the vacuous answers an empty operand earns — `contains(∅)` is true and
/// `intersects(∅)` is false, which is the vendored crate's reading and the SQL's alike.
#[test]
fn the_predicates_answer_and_the_empty_operand_is_vacuous() {
  let value = Disposition::DEFAULT;

  assert_eq!(
    predicate(value, "contains", Disposition::DEFAULT),
    Some(true)
  );
  assert_eq!(
    predicate(value, "contains", Disposition::FORCED),
    Some(false)
  );
  assert_eq!(
    predicate(value, "contains", Disposition::empty()),
    Some(true)
  );
  assert_eq!(
    predicate(value, "intersects", Disposition::empty()),
    Some(false)
  );
  assert_eq!(predicate(value, "text", Disposition::empty()), None);
}