ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! The PAGE'S TWO SDL NAMES, read back out of the tokens the two naming functions write.
//!
//! Two questions, separable on purpose: what is emitted when a declaration renamed NOTHING — the
//! answer is the tower's BASE, the record's plural with `Connection` and `Edge` on it — and what is
//! emitted when one did.
//!
//! The functions rather than the whole face, because they are the whole of the naming: they read
//! `Graphql::connection`, `Graphql::edge` and the record's own resolved plural, and nothing else in
//! the model reaches them. The face's SIDE of it — that the markers land inside its anonymous const
//! and that both paged legs' return types close on them — is executed against a real schema in
//! `ingraph/tests/wire_page_names.rs`, where a renamed page is asked to answer. The TIERS themselves
//! are `page_tower_tests.rs`, beside this file.

use quote::ToTokens as _;

use crate::model::{Graphql, RecordModel};

/// The fixture with the knob set however a case wants it.
fn record(knob: impl FnOnce(&mut Graphql)) -> RecordModel {
  let mut model = crate::tests::wired();
  knob(model.graphql.as_mut().expect("the fixture has a wire face"));
  model
}

/// The markers a model mints for a record that HAS a page, as token text.
fn minted(model: &RecordModel) -> String {
  let graphql = model.graphql.as_ref().expect("the wire face");
  model
    .page_names(graphql, true)
    .to_token_stream()
    .to_string()
}

/// The same, for a record whose list leg was not emitted.
fn unpaged(model: &RecordModel) -> String {
  let graphql = model.graphql.as_ref().expect("the wire face");
  model
    .page_names(graphql, false)
    .to_token_stream()
    .to_string()
}

/// The two type arguments a paged leg's `Connection<…>` closes with.
fn closed(model: &RecordModel) -> (String, String) {
  let (connection, edge) = model.page_name_arguments();
  (connection.to_string(), edge.to_string())
}

/// **A RECORD THAT RENAMED NOTHING GETS THE PLURAL**, and the page closes on its own markers rather
/// than on the library's defaults.
///
/// This assertion is the INVERSE of the one this file shipped one round ago, and the inversion is
/// the 2026-08-13 ruling: *跟着复数走 … 我们是 tiered, 一层覆盖一层*. The base of the tower is the
/// derived plural, and `DefaultConnectionName` renders `<Node>Connection` over the node's SINGULAR
/// name — so there is no longer a declaration whose page can be left to the library, and the marker
/// pair is minted for every record that has a page.
///
/// `#222`'s L-4 — *marker types nothing names would be the vocabulary's first zombie* — is not
/// weakened by that. Its condition was a READER, and the reader is the two paged legs' return types;
/// `a_record_with_no_page_mints_nothing_even_having_declared_both_words` below is where the refusal
/// still bites.
#[test]
fn a_record_that_renamed_nothing_takes_the_plural_base() {
  let model = record(|_| {});
  let emitted = minted(&model);

  assert!(
    emitted.contains("\"VideoTracksConnection\""),
    "the page is the record's PLURAL, not `VideoTrackConnection`:\n{emitted}"
  );
  assert!(
    emitted.contains("\"VideoTracksEdge\""),
    "and the edge rides the same tower:\n{emitted}"
  );
  assert!(
    !emitted.contains("DefaultConnectionName") && !emitted.contains("DefaultEdgeName"),
    "the library's singular-based defaults are named nowhere:\n{emitted}"
  );

  assert_eq!(
    closed(&model),
    (
      "VideoTrackConnectionName".to_owned(),
      "VideoTrackEdgeName".to_owned()
    ),
    "and the page closes on this record's own markers"
  );
}

/// **A DECLARED WORD MINTS ITS MARKER AND IS SPENT IN IT** — the type is declared, the hook is
/// implemented on it, and the word travels as a STRING rather than as an identifier.
#[test]
fn a_declared_connection_word_is_minted_and_returned() {
  let model = record(|graphql| graphql.connection = Some("TrackReel".to_owned()));
  let emitted = minted(&model);

  assert!(
    emitted.contains("struct VideoTrackConnectionName"),
    "the marker is declared:\n{emitted}"
  );
  assert!(
    emitted.contains("ConnectionNameType for VideoTrackConnectionName"),
    "and implements the hook:\n{emitted}"
  );
  assert!(
    emitted.contains("\"TrackReel\""),
    "the word is a string the body hands back, never an ident:\n{emitted}"
  );

  assert_eq!(
    closed(&model),
    (
      "VideoTrackConnectionName".to_owned(),
      "VideoTrackEdgeName".to_owned()
    ),
    "the page takes the pair whichever tier decided each word"
  );
  assert!(
    emitted.contains("\"VideoTracksEdge\""),
    "and the edge, unasked, keeps the tower's base:\n{emitted}"
  );
}

/// **THE TWO HOOKS ARE INDEPENDENT** — an edge word alone moves the edge and leaves the page on the
/// tower's base, which is the pair a shared derivation could not produce.
#[test]
fn an_edge_word_alone_moves_only_the_edge() {
  let model = record(|graphql| graphql.edge = Some("TrackItem".to_owned()));
  let emitted = minted(&model);

  assert!(
    emitted.contains("struct VideoTrackEdgeName"),
    "the edge marker:\n{emitted}"
  );
  assert!(
    emitted.contains("EdgeNameType for VideoTrackEdgeName"),
    "and its hook:\n{emitted}"
  );
  assert!(
    emitted.contains("\"TrackItem\""),
    "carrying the declared word:\n{emitted}"
  );
  assert!(
    emitted.contains("\"VideoTracksConnection\"") && !emitted.contains("\"TrackItemConnection\""),
    "and the page keeps the plural, derived from NOTHING the edge wrote:\n{emitted}"
  );
}

/// **BOTH WORDS TOGETHER** mint two markers and close both positions, with neither derived from the
/// other: the words share no stem here, so a suffix rule would be visible as a wrong answer.
#[test]
fn both_words_mint_two_markers_and_close_both_positions() {
  let model = record(|graphql| {
    graphql.connection = Some("TrackReel".to_owned());
    graphql.edge = Some("SplicePoint".to_owned());
  });
  let emitted = minted(&model);

  assert!(emitted.contains("\"TrackReel\""), "{emitted}");
  assert!(emitted.contains("\"SplicePoint\""), "{emitted}");
  assert!(
    !emitted.contains("VideoTracksConnection") && !emitted.contains("VideoTracksEdge"),
    "and the tower's base is nowhere, both tiers having been covered:\n{emitted}"
  );
  assert_eq!(
    closed(&model),
    (
      "VideoTrackConnectionName".to_owned(),
      "VideoTrackEdgeName".to_owned()
    )
  );
}

/// **THE MARKER'S RUST NAME IS THE RECORD'S, NOT THE WORD'S** — a declaration renaming its page
/// `TrackReel` still gets `VideoTrackConnectionName`, because the two are different axes: the Rust
/// name has to be unique per record inside one expansion, and the SDL word is whatever a schema
/// should read.
///
/// Load-bearing: a marker named after the WORD would collide the moment two records chose one word,
/// and the collision would be reported inside an expansion its author cannot edit. It is the same
/// reason the marker is not named after the PLURAL either — two records may pluralise alike.
#[test]
fn the_markers_rust_name_follows_the_record_and_not_the_word() {
  let model = record(|graphql| graphql.connection = Some("TrackReel".to_owned()));
  let emitted = minted(&model);

  assert!(
    emitted.contains("struct VideoTrackConnectionName"),
    "{emitted}"
  );
  assert!(
    !emitted.contains("struct TrackReel") && !emitted.contains("struct VideoTracksConnection"),
    "the word names a schema type and never a Rust one:\n{emitted}"
  );
}

/// **A RECORD WITH NO PAGE MINTS NOTHING, whatever it declared** — the entries are parsed for every
/// wire face, so a declaration may carry the words while having no list leg to spend them on.
///
/// Load-bearing rather than tidy, and the ONE condition the unconditional minting above did not
/// spend: a marker nothing names is `dead_code`, and it would be reported inside an expansion its
/// author cannot edit. The condition is the LIST LEG's own presence, handed in by the caller that
/// just emitted it, so the two cannot drift apart.
#[test]
fn a_record_with_no_page_mints_nothing_even_having_declared_both_words() {
  let model = record(|graphql| {
    graphql.connection = Some("TrackReel".to_owned());
    graphql.edge = Some("TrackItem".to_owned());
  });

  assert!(
    unpaged(&model).is_empty(),
    "not one token where there is no page to name:\n{}",
    unpaged(&model)
  );
  assert!(
    !minted(&model).is_empty(),
    "and the same declaration WITH a page does mint"
  );
}

/// **AND A RECORD THAT DECLARED NOTHING MINTS NOTHING EITHER, WITHOUT A PAGE** — the guard is the
/// leg's presence and not the words', which the unconditional minting makes worth stating on its own
/// row.
#[test]
fn a_record_with_no_page_mints_nothing_having_declared_neither_word() {
  let model = record(|_| {});

  assert!(
    unpaged(&model).is_empty(),
    "the plural base is a word for a page, and there is no page:\n{}",
    unpaged(&model)
  );
}