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::{ID, InterfaceArm, InterfaceColumn, InterfaceKind, KIND, UnknownKind};
use crate::FieldName;

/// A two-member vocabulary written BY HAND, which is what a third party writes and what the derive
/// emits: two facts, and the crossing inherited.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MediaKind {
  Video,
  Audio,
}

impl InterfaceKind for MediaKind {
  const INTERFACE: &'static str = "Media";
  const ALL: &'static [Self] = &[Self::Video, Self::Audio];

  fn as_str(self) -> &'static str {
    match self {
      Self::Video => "video",
      Self::Audio => "audio",
    }
  }
}

/// **The crossing is the two facts and nothing else** — an implementor states `ALL` and `as_str`,
/// and both directions follow.
#[test]
fn a_word_round_trips_through_the_vocabulary_it_names() {
  for kind in MediaKind::ALL.iter().copied() {
    assert_eq!(MediaKind::parse(kind.as_str()), Ok(kind));
  }
}

/// **A word nobody declared is refused carrying the word**, because the word is the whole evidence:
/// a cursor minted under another interface and a row from a table the union does not join are told
/// apart by reading it.
#[test]
fn a_word_no_member_carries_is_refused_by_name() {
  let refused = MediaKind::parse("caption").expect_err("no member is called that");

  assert_eq!(refused.interface(), "Media");
  assert_eq!(refused.word(), "caption");
  assert_eq!(
    refused.to_string(),
    "`Media` has no member called `caption`"
  );
  assert_eq!(refused, UnknownKind::new("Media", "caption"));
}

/// **The discriminant is lower-case ASCII at every member**, which is the module note's collation
/// argument held as a test rather than as a comment: a word with no case distinction in it sorts
/// one way under every default collation the three shipped dialects have.
#[test]
fn every_discriminant_is_a_word_no_collation_can_reorder() {
  for kind in MediaKind::ALL.iter().copied() {
    let word = kind.as_str();
    assert!(
      word
        .bytes()
        .all(|byte| byte.is_ascii_lowercase() || byte == b'_'),
      "`{word}` carries a character a collation may fold"
    );
  }
}

/// **The synthesized column wears the fence and the real one does not**, which is the whole of what
/// the naming law buys: `__ig_kind__` is a name `ingraph-derive` refuses a declaration for, so an
/// interface may promise a shared field called `kind` and collide with nothing — while `id` is a
/// member's own column under one output name and stays an ordinary one.
#[test]
fn a_synthesized_column_wears_the_fence_and_a_real_one_does_not() {
  assert_eq!(KIND.as_str(), "__ig_kind__");
  assert!(
    KIND.as_str().starts_with("__") && KIND.as_str().ends_with("__"),
    "the fence is `__` at BOTH ends, which is what the derive refuses a column for"
  );
  assert_eq!(ID.as_str(), "id");
}

/// **An arm's two names differ exactly where a member block moved the column**, and both are
/// carried: the arm that renames and the arm that does not are one rendering.
#[test]
fn an_arm_carries_what_each_member_calls_the_shared_column() {
  const COLUMNS: &[InterfaceColumn] = &[
    InterfaceColumn::new(FieldName::new("title"), FieldName::new("title")),
    InterfaceColumn::new(FieldName::new("created_at"), FieldName::new("imported_at")),
  ];
  const ARM: InterfaceArm<MediaKind> = InterfaceArm::new(MediaKind::Audio, COLUMNS);

  assert_eq!(ARM.kind(), MediaKind::Audio);
  assert_eq!(ARM.columns().len(), 2);
  assert_eq!(ARM.columns()[0].field().as_str(), "title");
  assert_eq!(ARM.columns()[0].column().as_str(), "title");
  assert_eq!(ARM.columns()[1].field().as_str(), "created_at");
  assert_eq!(ARM.columns()[1].column().as_str(), "imported_at");
}