ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! The ID DEVICE's emission, read as TOKENS — the claims a compiled expansion cannot make.
//!
//! `ingraph`'s own `tests/id_device.rs` is where a declared wrapper reaches a real schema, a real
//! cursor and a real database, and where the derive's output is asserted equal to a hand-written
//! register's face for face. What is left for this file is what a COMPILING expansion says nothing
//! about:
//!
//! - that every delegation is FULLY QUALIFIED, which a bare `self.0.method(…)` would satisfy the
//!   compiler with today and resolve elsewhere the day the inner type grows an inherent method;
//! - that every framework name goes through the SHELF rather than through a direct path;
//! - that the register is UNCONDITIONAL — there is no word to withhold a face, so every one of them
//!   is in every emission;
//! - that the two GATES are ridden: the SQL pair on the family gate, the scalar on the binding one —
//!   and that `StoredAs<Db>` and the wrapper's own `IdColumn<Db>` ride NEITHER, since neither trait
//!   names a backend for one to guard;
//! - that `Ord` is absent, which is a measurement and would otherwise be invisible.

use super::IdModel;

/// Tokens with their spacing removed, so a comparison is about content rather than layout.
fn tight(tokens: impl quote::ToTokens) -> String {
  quote::ToTokens::to_token_stream(&tokens)
    .to_string()
    .replace(' ', "")
}

/// One id wrapper, as the front end would have handed it over.
fn model(inner: syn::Type) -> IdModel {
  IdModel {
    vis: syn::parse_quote!(pub),
    subject: syn::Ident::new("TrackId", proc_macro2::Span::call_site()),
    krate: syn::parse_quote!(::ingraph),
    inner,
  }
}

/// The driving declaration: a wrapper over the type both live registers wrap.
fn track() -> IdModel {
  model(syn::parse_quote!(uuid::Uuid))
}

/// THE KEY ROW, which is the one face with no hand-written alternative from outside the framework.
#[test]
fn the_transparency_assertion_is_written_through_the_shelf() {
  let emitted = tight(track().emit());
  assert!(
    emitted.contains("unsafeimpl::ingraph::__private::id::DelegatesIdforTrackId"),
    "{emitted}"
  );
  assert!(emitted.contains("typeInner=uuid::Uuid;"), "{emitted}");
  assert!(
    emitted.contains("fndelegate(&self)->&uuid::Uuid{&self.0}"),
    "{emitted}"
  );
}

/// The identity kit, delegating and FULLY QUALIFIED — the clause of the transparency contract a
/// shape check cannot close.
#[test]
fn the_identity_kit_delegates_and_is_qualified() {
  let emitted = tight(track().emit());
  for row in [
    "<uuid::Uuidas::core::cmp::PartialEq>::eq(&self.0,&__ingraph_other.0)",
    "<uuid::Uuidas::core::hash::Hash>::hash(&self.0,__ingraph_state)",
    "<uuid::Uuidas::core::clone::Clone>::clone(&self.0)",
  ] {
    assert!(emitted.contains(row), "missing {row} in {emitted}");
  }
  assert!(
    emitted.contains("impl::core::cmp::EqforTrackId"),
    "{emitted}"
  );
}

/// `Ord` is a MEASUREMENT, and an absence is only pinned by asserting it.
#[test]
fn no_order_is_minted() {
  let emitted = tight(track().emit());
  assert!(!emitted.contains("::core::cmp::Ord"), "{emitted}");
  assert!(!emitted.contains("::core::cmp::PartialOrd"), "{emitted}");
  assert!(!emitted.contains("::core::marker::Copy"), "{emitted}");
}

/// **THE SEAM, AT THE INNER** — the bind and the read back are written over
/// `<Inner as IdColumn<Db>>::Stored` rather than over `String`, which is the whole content of the
/// switch: what a uuid-inner id binds is now the inner's own stored form.
#[test]
fn the_storage_pair_binds_the_inners_stored_form() {
  let emitted = tight(track().emit());
  assert!(
    emitted.contains(
      "<uuid::Uuidas::ingraph::__private::id::IdColumn<__IngraphDb>>::Stored:::ingraph::__private::comparison::sql::Carrier<__IngraphDb>"
    ),
    "{emitted}"
  );
  assert!(
    emitted.contains(
      "::push_bind(&<uuid::Uuidas::ingraph::__private::id::IdColumn<__IngraphDb>>::to_stored(&self.0),"
    ),
    "{emitted}"
  );
  // The DECODE reads back through the seam's door, and the ROW is what carries the dialect there.
  assert!(
    emitted.contains(
      "<uuid::Uuidas::ingraph::__private::id::IdColumn<<__IngraphRowas::ingraph::__private::sqlx::Row>::Database>>::from_stored(__ingraph_stored)"
    ),
    "{emitted}"
  );
  // And NOT through the text road it replaced.
  assert!(
    !emitted.contains(
      "<::std::string::Stringas::ingraph::__private::comparison::sql::Carrier<__IngraphDb>>::push_bind"
    ),
    "{emitted}"
  );
  assert!(
    !emitted.contains("<Selfas::core::str::FromStr>::from_str(&__ingraph_stored)"),
    "{emitted}"
  );
}

/// The FOURTH face: `StoredAs<Db>` reads its word off the same projection the pair above binds, so
/// the declared column and the bound value cannot disagree — and unlike the pair, it rides NO gate,
/// because the trait itself names no `sqlx::Database` bound for one to guard.
#[test]
fn the_stored_word_is_the_seams_and_rides_no_gate() {
  let emitted = tight(track().emit());
  assert!(
    emitted
      .contains("impl<__IngraphDb>::ingraph::__private::column::StoredAs<__IngraphDb>forTrackId"),
    "{emitted}"
  );
  assert!(
    emitted.contains(
      "constWORDS:&'static[&'static::core::primitive::str]=<<uuid::Uuidas::ingraph::__private::id::IdColumn<__IngraphDb>>::Storedas::ingraph::__private::column::StoredAs<__IngraphDb>>::WORDS;"
    ),
    "{emitted}"
  );
  // Delegated, not restated — no literal word is spelled here.
  assert!(
    !emitted.contains("WORDS:&'static[&'static::core::primitive::str]=&["),
    "{emitted}"
  );
  // And NOT through `String`, which is what the row said before the seam.
  assert!(
    !emitted.contains("::std::string::String:::ingraph::__private::column::StoredAs<__IngraphDb>"),
    "{emitted}"
  );
}

/// The seam read ONE LAYER OUT: the wrapper carries its own `IdColumn<Db>` row, delegating, so an id
/// over an id stores — and the row rides no gate, the trait naming no database.
#[test]
fn the_wrapper_joins_the_seam_by_delegating() {
  let emitted = tight(track().emit());
  assert!(
    emitted.contains("impl<__IngraphDb>::ingraph::__private::id::IdColumn<__IngraphDb>forTrackId"),
    "{emitted}"
  );
  assert!(
    emitted.contains(
      "typeStored=<uuid::Uuidas::ingraph::__private::id::IdColumn<__IngraphDb>>::Stored;"
    ),
    "{emitted}"
  );
  assert!(
    emitted.contains(
      "typeRefusal=<uuid::Uuidas::ingraph::__private::id::IdColumn<__IngraphDb>>::Refusal;"
    ),
    "{emitted}"
  );
}

/// The one value-column face `ingraph` DOES ship for a bare uuid is delegated to rather than
/// re-derived — the asymmetry the census found, emitted.
#[test]
fn the_cursor_position_delegates_to_the_inner() {
  let emitted = tight(track().emit());
  assert!(
    emitted.contains(
      "<uuid::Uuidas::ingraph::__private::cursor::CursorValue>::write_cursor(&self.0,__ingraph_out)"
    ),
    "{emitted}"
  );
}

/// The two gates, each over the half that names a vendored crate.
#[test]
fn the_two_gates_are_ridden() {
  let emitted = tight(track().emit());
  assert!(
    emitted.contains("::ingraph::__private::gate::if_sqlx!"),
    "{emitted}"
  );
  assert!(
    emitted.contains("::ingraph::__private::gate::if_async_graphql!"),
    "{emitted}"
  );
}

/// The scalar reaches async-graphql THROUGH THE SHELF, crate override included — the token walk
/// cannot see a string literal, so the emitted path is pinned here or nowhere.
#[test]
fn the_scalar_reaches_async_graphql_through_the_shelf() {
  let emitted = tight(track().emit());
  assert!(
    emitted.contains("crate=\"::ingraph::__private::async_graphql\""),
    "{emitted}"
  );
  assert!(emitted.contains("name=\"TrackId\""), "{emitted}");
}

/// The filter reading is MINTED beside the type and named for it, because a downstream resolves it
/// through an associated type.
#[test]
fn the_reading_is_minted_and_named() {
  let emitted = tight(track().emit());
  assert!(emitted.contains("pubenumTrackIdWire{}"), "{emitted}");
  assert!(
    emitted.contains("constNAME:&'static::core::primitive::str=\"TrackIdFilter\";"),
    "{emitted}"
  );
  assert!(
    emitted
      .contains("typeFilter=::ingraph::__private::graphql::ScalarFilter<TrackId,TrackIdWire>;"),
    "{emitted}"
  );
}

/// The four casts and the constructor, at the declaration's own visibility and outside the anonymous
/// const — an inherent impl written inside one is a door nobody can open.
#[test]
fn the_casts_are_public_and_reachable() {
  let emitted = tight(track().emit());
  for row in [
    "pubconstfnnew(__ingraph_inner:uuid::Uuid)->Self",
    "pubconstfnas_inner(&self)->&uuid::Uuid",
    "pubconstfnfrom_ref(__ingraph_inner:&uuid::Uuid)->&Self",
    "pubconstfnfrom_slice(__ingraph_inner:&[uuid::Uuid])->&[Self]",
    "pubconstfnas_inner_slice(__ingraph_this:&[Self])->&[uuid::Uuid]",
    "pubfninto_inner(self)->uuid::Uuid",
  ] {
    assert!(emitted.contains(row), "missing {row} in {emitted}");
  }
  assert!(
    emitted.contains("::core::mem::transmute::<&uuid::Uuid,&Self>"),
    "{emitted}"
  );
  assert!(
    emitted.contains("::core::mem::transmute::<&[uuid::Uuid],&[Self]>"),
    "{emitted}"
  );
}

/// A `crate = "…"` moves EVERY framework path, which is the only thing that word is for.
#[test]
fn the_crate_path_moves_every_shelf_reach() {
  let mut model = track();
  model.krate = syn::parse_quote!(other::place);
  let emitted = tight(model.emit());
  assert!(
    emitted.contains("other::place::__private::id::DelegatesId"),
    "{emitted}"
  );
  assert!(
    emitted.contains("crate=\"other::place::__private::async_graphql\""),
    "{emitted}"
  );
  assert!(!emitted.contains("::ingraph::__private"), "{emitted}");
}

/// The register does not depend on WHAT is wrapped: a text-carried id gets the same rows, because
/// there is no word to withhold one.
#[test]
fn the_register_is_unconditional() {
  let text = tight(model(syn::parse_quote!(::std::string::String)).emit());
  for row in [
    "DelegatesIdforTrackId",
    "impl::core::fmt::DisplayforTrackId",
    "impl::core::str::FromStrforTrackId",
    "ColumnKindforTrackId",
    "IdColumn<__IngraphDb>forTrackId",
    "StoredAs<__IngraphDb>forTrackId",
    "ToGraphqlInputforTrackId",
    "ToGraphqlOutputforTrackId",
    "DefaultMarkerforTrackId",
    "CompositeFilterMarkerforTrackId",
  ] {
    assert!(text.contains(row), "missing {row} in {text}");
  }
}