ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **B-27 — THE INTERFACE IS A ROOT CITIZEN**, read back out of the fold's own tokens.
//!
//! Beside [`super::tests`] rather than inside it, which is this workspace's shape for an axis that
//! arrives after a walk has been proven: the roster a reader walks stays the one it was, and the new
//! tier reads as one file.
//!
//! What is pinned here is the D-2 and D-3 rulings at the code that answers them — the interface's own
//! mount word, the independence of the two tiers' membership, and the ONE-HOP projection that reaches
//! `Graphql::Query` without a `Transformer` an interface cannot implement.

use crate::graph::{GraphModel, InterfaceMount};

/// A root with three nodes and two interfaces, each tier's membership handed in separately.
fn root(queried: &[&str], interfaces: &[(&str, bool)]) -> GraphModel {
  let nodes: Vec<syn::Type> = ["media::Reel", "media::Take", "media::Cutter"]
    .iter()
    .map(|path| syn::parse_str(path).expect("a node path"))
    .collect();
  GraphModel {
    krate: syn::parse_quote!(::ingraph),
    vis: syn::parse_quote!(pub),
    name: syn::parse_quote!(Media),
    nodes,
    mounts: vec![
      syn::parse_quote!(reels),
      syn::parse_quote!(takes),
      syn::parse_quote!(cutters),
    ],
    queried: queried
      .iter()
      .map(|path| syn::parse_str(path).expect("a node path"))
      .collect(),
    interfaces: interfaces
      .iter()
      .enumerate()
      .map(|(at, (path, queried))| InterfaceMount {
        mount: quote::format_ident!("mount{at}"),
        ty: syn::parse_str(path).expect("an interface path"),
        queried: *queried,
      })
      .collect(),
    fused: Vec::new(),
    resources: Vec::new(),
    origins: Vec::new(),
    engines: Vec::new(),
    retired: None,
    generations: None,
  }
}

/// The emitted tokens, whitespace-normalised, which is how every token assertion in this crate reads
/// an emission back.
fn emitted(model: &GraphModel) -> String {
  model.query().to_string()
}

/// **THE INTERFACE'S UNIT IS REACHED IN ONE HOP**, and the missing hop is FORCED: `Transformer` is
/// `Identifiable`'s subtrait and an interface's id is a sum no member's value holds one of, so
/// `Transformer` is unimplementable there. The seat is the same seat — `Graphql::Query` — which is
/// what D-3's *no second mounting mechanism* comes to in tokens.
#[test]
fn an_interface_is_projected_through_graphql_alone() {
  let tokens = emitted(&root(&[], &[("media::Screened", true)]));

  assert!(
    tokens.contains(
      "< media :: Screened as :: ingraph :: __private :: transform :: Graphql > :: Query"
    ),
    "{tokens}"
  );
  assert!(
    !tokens.contains("Screened as :: ingraph :: __private :: transform :: Transformer"),
    "an interface has no `Transformer` to project through:\n{tokens}"
  );
  assert!(
    !tokens.contains("ScreenedQueries"),
    "the unit's NAME is never written here either:\n{tokens}"
  );
}

/// **THE TWO TIERS FOLD TOGETHER**, into the one `MergedObject` a client reads as one root type.
#[test]
fn the_nodes_and_the_interfaces_are_folded_by_one_assembly() {
  let tokens = emitted(&root(&["media::Reel"], &[("media::Screened", true)]));

  assert!(tokens.contains("media :: Reel"), "{tokens}");
  assert!(tokens.contains("media :: Screened"), "{tokens}");
  assert_eq!(
    tokens.matches("MergedObject").count(),
    1,
    "one fold, not one per tier:\n{tokens}"
  );
}

/// **D-2: THE MOUNT WORD IS THE INTERFACE'S OWN.** An interface field that did not write `query`
/// publishes no root field, which is presence-is-enablement at the tier the ruling is about — 乙
/// (implicit mounting) is what this refuses.
#[test]
fn an_interface_that_did_not_write_the_word_is_not_folded() {
  let tokens = emitted(&root(&["media::Reel"], &[("media::Screened", false)]));

  assert!(
    !tokens.contains("Screened"),
    "an unmounted interface is no member of the fold:\n{tokens}"
  );
  assert!(tokens.contains("media :: Reel"), "{tokens}");
}

/// **AND MEMBER MOUNTS ARE INDEPENDENT**: a graph that mounts the interface and none of its members
/// emits the interface's unit alone — 丙 (coupled mounting) refused from the other side.
#[test]
fn an_interface_mounts_without_any_member_mounting() {
  let tokens = emitted(&root(&[], &[("media::Screened", true)]));

  assert!(tokens.contains("media :: Screened"), "{tokens}");
  assert!(
    !tokens.contains("media :: Reel"),
    "no member joined, and none was dragged in:\n{tokens}"
  );
  assert!(tokens.contains("struct MediaQuery"), "{tokens}");
}

/// **A ROOT WHOSE ONLY MEMBER IS AN INTERFACE STILL HAS A QUERY**, and one whose members are all
/// unmounted still has none — the emission's condition reads BOTH tiers rather than the node list
/// alone.
#[test]
fn the_query_condition_reads_both_tiers() {
  assert!(!emitted(&root(&[], &[("media::Screened", true)])).is_empty());
  assert!(emitted(&root(&[], &[("media::Screened", false)])).is_empty());
  assert!(emitted(&root(&[], &[])).is_empty());
}