ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **THE RECIPE'S `dedup` WORD, READ OFF THE DOOR IT BUYS** — the framework join the fold names and
//! the `where` row the door states about its root parameter.
//!
//! The root's own suite, migrated. What is pinned is that the emission READS the word rather than
//! picks one — a matrix over both spellings, each claim stated as *this row is present AND the other
//! is not*. A pin that only asserted presence would pass on an emission that wrote both.
//!
//! One claim did not survive the move and did not need to: the root's *one word governs all three
//! doors* counted doors, and a recipe mints one.

use super::{
  SearchModel,
  tests::{member, recipe, tight},
};
use crate::index::Dedup;

/// The two members every claim below is made over.
fn members() -> Vec<crate::search::MemberModel> {
  vec![member("subs", "Subs"), member("shots", "Shots")]
}

/// The two rows one mechanism owns, as the emission spells them: the framework join path, and the
/// `where` row about the root parameter.
fn rows(dedup: Dedup) -> (&'static str, &'static str) {
  match dedup {
    Dedup::Hash => (
      "::ingraph::__private::retrieve::HashJoin",
      "where__INGRAPH_ROOT__:::core::hash::Hash+::core::cmp::Eq+::core::clone::Clone,",
    ),
    Dedup::Eq => (
      "::ingraph::__private::retrieve::EqJoin",
      "where__INGRAPH_ROOT__:::core::cmp::PartialEq+::core::clone::Clone,",
    ),
  }
}

/// **THE JOIN PATH AND THE BOUNDS ROW BOTH FOLLOW THE WORD**, and the other mechanism's rows are
/// ABSENT — the pin that a `HashJoin` left hardcoded beside an `EqJoin` would fail.
#[test]
fn both_rows_follow_the_word_and_the_other_mechanism_is_absent() {
  for (chosen, other) in [(Dedup::Hash, Dedup::Eq), (Dedup::Eq, Dedup::Hash)] {
    let emitted = tight(&recipe(members(), chosen));
    let (join, admits) = rows(chosen);
    let (unchosen_join, unchosen_admits) = rows(other);

    assert!(
      emitted.contains(join),
      "`dedup({})` did not reach the fold's join: {emitted}",
      chosen.as_str(),
    );
    assert!(
      emitted.contains(admits),
      "`dedup({})` did not reach the door's `where` row: {emitted}",
      chosen.as_str(),
    );
    assert!(
      !emitted.contains(unchosen_join),
      "`dedup({})` emitted the other mechanism's join too: {emitted}",
      chosen.as_str(),
    );
    assert!(
      !emitted.contains(unchosen_admits),
      "`dedup({})` emitted the other mechanism's bounds row too: {emitted}",
      chosen.as_str(),
    );
  }
}

/// **THE DEFAULT IS `hash`, AND IT IS THE ROW THE DOOR HAS ALWAYS STATED** — a recipe built with
/// [`Dedup::default`] emits exactly what a recipe that wrote `dedup(hash)` emits.
#[test]
fn the_default_emits_what_the_hash_word_emits() {
  assert_eq!(
    tight(&recipe(members(), Dedup::default())),
    tight(&recipe(members(), Dedup::Hash)),
    "the default is not the `hash` word",
  );
  assert_ne!(
    tight(&recipe(members(), Dedup::Eq)),
    tight(&recipe(members(), Dedup::Hash)),
    "the two mechanisms emit one door, so neither pin above proves anything",
  );
}

/// **THE DOOR'S OWN DOC NAMES THE MECHANISM AND WHY IT IS THE RECIPE'S** — the dedup law's sentence,
/// carried to the reader of the emitted door rather than left in this crate.
#[test]
fn the_emitted_doc_names_the_mechanism_it_chose() {
  for dedup in [Dedup::Hash, Dedup::Eq] {
    let emitted = recipe(members(), dedup).fuse().to_string();

    assert!(
      emitted.contains(&format!("Roots are placed through `{}`", dedup.join())),
      "the door's doc does not name the mechanism: {emitted}",
    );
    assert!(
      emitted.contains(&format!("this recipe's `dedup({})` chose", dedup.as_str())),
      "the door's doc does not name the word that chose it: {emitted}",
    );
    assert!(
      emitted.contains("a key that cannot hash is retrievable"),
      "the door's doc drops the law the choice exists for: {emitted}",
    );
  }
}

/// **THE WORD TOUCHES THE DOOR AND NOTHING ELSE** — the packages, the evidence enum and the refusal
/// enum are the same tokens under either mechanism, so the two emissions differ only where the
/// mechanism is spent.
#[test]
fn the_packages_and_the_evidence_do_not_move_with_the_word() {
  let hash = recipe(members(), Dedup::Hash);
  let eq = recipe(members(), Dedup::Eq);
  let tokens = |model: &SearchModel, part: fn(&SearchModel) -> proc_macro2::TokenStream| {
    part(model).to_string()
  };

  assert_eq!(
    tokens(&hash, SearchModel::packages_emission),
    tokens(&eq, SearchModel::packages_emission),
    "the mechanism moved the packages",
  );
  assert_eq!(
    tokens(&hash, SearchModel::evidence),
    tokens(&eq, SearchModel::evidence),
    "the mechanism moved the evidence and refusal enums",
  );
  assert_ne!(
    tokens(&hash, SearchModel::door),
    tokens(&eq, SearchModel::door),
    "the mechanism did not reach the door it is spent in",
  );
}