ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **THE ADDRESS FACE'S TWO HALVES MOVE TOGETHER** — `key(rename = "Address")` publishes `Address`
//! and `AddressInput`, and the Rust name of the sending half stays where it was.
//!
//! ```text
//!   (nothing declared)          →  type VideoTrackKey   /  input VideoTrackKeyInput
//!   graphql(key(rename = "…"))  →  type Address         /  input AddressInput
//! ```
//!
//! # Why a file of its own beside [`tests`](super::tests)
//!
//! That suite reads the whole wire face — the output object, the order-by, the filter, the two input
//! faces' trait lists — and its subject is what each face IS. This one's subject is a single naming
//! LAW, and the law is the one exception in a grammar whose every other rename is verbatim: the
//! `Input` suffix is the ONE blessed derivation, deterministic and ecosystem-conventional, and no
//! later reader may cite it as licence for a second composition.
//!
//! # What the PAIR proves that either half alone does not
//!
//! The shape this replaces moved the OUTPUT and left the INPUT on the record's own stem — a
//! half-working rename, where a client reading `Address` had to send `VideoTrackKeyInput`. So the
//! assertion is an equality over BOTH published names at once, in both states: a repair that moved
//! the input and dropped the output's own resolution would pass either half on its own.

use crate::{model::RecordModel, name::RustSafeName};

/// [`crate::tests::wired`] with whatever word a case declares for its address — `None` for the
/// silence the base answers.
///
/// `wired`'s key is COMPOUND, which is what puts the argument object in the emission at all: a
/// root's address crosses inward as its id and mints no type, so a root fixture could not carry this
/// claim.
fn addressed(word: Option<&str>) -> RecordModel {
  let mut model = crate::tests::wired();
  let record = model.record.clone();
  let block = model
    .graphql
    .as_mut()
    .expect("`wired` declares a wire face");
  block.key_output = match word {
    Some(word) => RustSafeName::new(word, proc_macro2::Span::call_site()),
    None => RustSafeName::from_ident(&crate::model::default_key_output_name(&record)),
  };
  assert!(
    model.key.compound(),
    "`{record}` is the compound fixture this reading needs",
  );
  model
}

/// The whole expansion with the spaces taken out.
fn tight(model: &RecordModel) -> String {
  model.emit().to_string().split_whitespace().collect()
}

/// The string literal `needle` opens, taken from the LAST such literal before `anchor` — because
/// `name="…"` rides an ATTRIBUTE, and an attribute is written before the item it is about.
fn quoted_before(emitted: &str, anchor: &str, needle: &str) -> String {
  let at = emitted
    .find(anchor)
    .unwrap_or_else(|| panic!("`{anchor}` is missing: {emitted}"));
  let head = &emitted[..at];
  let at = head
    .rfind(needle)
    .unwrap_or_else(|| panic!("`{needle}` is missing before `{anchor}`: {emitted}"));
  read(&head[at + needle.len()..])
}

/// The string literal `needle` opens, taken from the FIRST such literal after `anchor` — because a
/// `type_name` body is written inside the impl it belongs to.
fn quoted_after(emitted: &str, anchor: &str, needle: &str) -> String {
  let at = emitted
    .find(anchor)
    .unwrap_or_else(|| panic!("`{anchor}` is missing: {emitted}"));
  let rest = &emitted[at..];
  let at = rest
    .find(needle)
    .unwrap_or_else(|| panic!("`{needle}` is missing after `{anchor}`: {emitted}"));
  read(&rest[at + needle.len()..])
}

/// Everything up to the closing quote.
fn read(rest: &str) -> String {
  let end = rest.find('"').expect("a string literal closes");
  rest[..end].to_owned()
}

/// The SDL name each half of the address face publishes.
///
/// Read off the RENDERED emission rather than off the model, because the whole question is what a
/// client reads — a helper that asked the model would be asserting the input of the function under
/// test.
///
/// The OBJECT's word rides the attribute async-graphql reads and is anchored at the impl that
/// carries it; the ARGUMENT's rides this crate's own hand-written `InputType::type_name`, anchored
/// at the impl that declares it.
///
/// `pub(super)` since the 2026-08-16 ruling: [`super::key_faces_tests`] reads the same pair off the
/// same emission to prove the MEMBER seats, and two readers of one pair of anchors is two things to
/// keep in step with async-graphql's attribute shape.
pub(super) fn published(model: &RecordModel) -> (String, String) {
  let emitted = tight(model);
  let object = model
    .graphql
    .as_ref()
    .expect("the fixture declares a face")
    .key_output
    .safe()
    .to_string();
  (
    quoted_before(&emitted, &format!(")]impl{object}{{"), "name=\""),
    quoted_after(
      &emitted,
      "InputTypeforVideoTrackKeyInput{",
      "Cow::Borrowed(\"",
    ),
  )
}

/// **THE SILENCE PUBLISHES THE RECORD'S OWN STEM AT BOTH HALVES** — `<Record>Key` and
/// `<Record>KeyInput`, which is exactly what an un-renamed record published before this law and must
/// go on publishing.
///
/// The suffix on the sending half is not decoration: GraphQL keeps input and output types in ONE
/// namespace, so the two halves of an address must differ by something, and the shorter word goes to
/// the half a client READS far more often.
#[test]
fn an_unrenamed_address_publishes_the_record_stem_at_both_halves() {
  assert_eq!(
    published(&addressed(None)),
    ("VideoTrackKey".to_owned(), "VideoTrackKeyInput".to_owned()),
  );
}

/// **A DECLARED WORD RE-BASES BOTH HALVES** — `Address` and `AddressInput`, and neither is left on
/// the record's own stem.
#[test]
fn a_declared_word_re_bases_the_object_and_the_argument_together() {
  assert_eq!(
    published(&addressed(Some("Address"))),
    ("Address".to_owned(), "AddressInput".to_owned()),
  );
}

/// **THE SENDING HALF'S RUST NAME DOES NOT MOVE**, which is the axis law this composition rides
/// inside: a rename word moves a FACE, and the argument object is a type nothing in the grammar
/// names.
///
/// Asserted on the DECLARED items rather than on the roster, so what is read is the type an author's
/// scope receives.
#[test]
fn the_argument_objects_rust_name_stays_where_the_emitter_put_it() {
  let file: syn::File =
    syn::parse2(addressed(Some("Address")).emit()).expect("the emission parses");
  let landed: Vec<String> = file
    .items
    .iter()
    .filter_map(|item| match item {
      syn::Item::Struct(declared) => Some(declared.ident.to_string()),
      _ => None,
    })
    .collect();
  assert!(
    landed.contains(&"VideoTrackKeyInput".to_owned()),
    "the schema word moved a Rust name: {landed:?}",
  );
  assert!(
    !landed.iter().any(|name| name == "AddressInput"),
    "the schema word minted a Rust name: {landed:?}",
  );
}