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 three scalar doors: the JSON shape they admit, the one they refuse, and the canonical slug
//! they write back.

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

use super::{AudioCodec, SubtitleCodec, VideoCodec};

/// One operand through the door, or the sentence it was refused with — the spelling
/// [`lang::graphql::tests`](crate::primitives::lang) uses, so a failing assertion reads the same way
/// in both houses.
fn door(sent: Value) -> Result<VideoCodec, String> {
  <VideoCodec as ScalarType>::parse(sent)
    .map_err(|refused| refused.into_server_error(Pos::default()).message)
}

/// **THE DOOR IS THE TYPE's OWN GRAMMAR** — every spelling the type admits, the scalar admits.
#[test]
fn the_door_is_the_types_own_grammar() {
  for sent in ["h264", "H264", "av1", "some_codec_9000"] {
    assert_eq!(
      door(Value::String(String::from(sent))),
      Ok(VideoCodec::new(sent)),
      "`{sent}`",
    );
  }
}

/// **EVERY SPELLING IS WRITTEN BACK AS THE CANONICAL ONE** — strict out, whichever case came in.
#[test]
fn every_spelling_is_written_back_as_the_canonical_one() {
  for sent in ["h264", "H264", "H264"] {
    let admitted = door(Value::String(String::from(sent))).expect("a string is admitted");
    assert_eq!(
      admitted.to_value(),
      Value::String(String::from("h264")),
      "`{sent}` is written back canonical",
    );
  }

  // The escape arm crosses out losslessly, as it stores and pages.
  let unknown = door(Value::String(String::from("some_codec_9000"))).expect("admitted");
  assert_eq!(
    unknown.to_value(),
    Value::String(String::from("some_codec_9000")),
  );
}

/// **A NON-STRING IS REFUSED, AND IT IS THE ONLY REFUSAL THERE IS.**
///
/// The number case is the one the module note argues: `012v` is a codec name whose spelling a JSON
/// number could not carry, so admitting bare digits would make the door's grammar depend on which
/// codec a client asked about.
#[test]
fn a_non_string_is_refused_and_it_is_the_only_refusal() {
  for sent in [
    Value::Number(7.into()),
    Value::Boolean(true),
    Value::Null,
    Value::List(vec![Value::String(String::from("h264"))]),
  ] {
    assert!(door(sent.clone()).is_err(), "{sent:?} is refused");
  }

  // And EVERY string is admitted — there is no spelling this door turns away.
  for sent in ["", " ", "!!!", "日本語", "not a codec at all"] {
    assert!(
      door(Value::String(String::from(sent))).is_ok(),
      "`{sent}` is admitted",
    );
  }
}

/// **VALIDATION AND PARSING ADMIT THE SAME VALUES**, so a query that validates is one that parses.
///
/// async-graphql's default `is_valid` answers `true` for everything, so a scalar narrowing in
/// `parse` alone would land its refusal at execution rather than at validation, where a client is
/// looking.
#[test]
fn validation_and_parsing_admit_the_same_values() {
  let sent = [
    Value::String(String::from("h264")),
    Value::String(String::from("")),
    Value::String(String::from("!!!")),
    Value::Number(7.into()),
    Value::Boolean(false),
    Value::Null,
  ];

  for value in sent {
    assert_eq!(
      <VideoCodec as ScalarType>::is_valid(&value),
      VideoCodec::parse(value.clone()).is_ok(),
      "{value:?} — the validator and the parser must agree",
    );
    assert_eq!(
      <AudioCodec as ScalarType>::is_valid(&value),
      AudioCodec::parse(value.clone()).is_ok(),
      "{value:?}",
    );
    assert_eq!(
      <SubtitleCodec as ScalarType>::is_valid(&value),
      SubtitleCodec::parse(value.clone()).is_ok(),
      "{value:?}",
    );
  }
}

/// **THE OTHER TWO DOORS ARE THE SAME DOOR**, over their own rosters.
#[test]
fn the_other_two_doors_are_the_same_door() {
  let audio = AudioCodec::parse(Value::String(String::from("AAC"))).expect("admitted");
  assert_eq!(audio, AudioCodec::new("aac"));
  assert_eq!(audio.to_value(), Value::String(String::from("aac")));

  let subtitle = SubtitleCodec::parse(Value::String(String::from("SubRip"))).expect("admitted");
  assert_eq!(subtitle, SubtitleCodec::new("subrip"));
  assert_eq!(subtitle.to_value(), Value::String(String::from("subrip")));

  assert!(AudioCodec::parse(Value::Number(7.into())).is_err());
  assert!(SubtitleCodec::parse(Value::Number(7.into())).is_err());
}