ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! What the journal declaration's expansion says, read off its tokens.
//!
//! The expansion's BEHAVIOUR is asserted where it can be run — `ingraph-derive`'s own corpus, which
//! mounts a journal for real, drives an observation into it and reads the row back out of a live
//! database. What is here is what a token stream can be read for: that the canonical shape is the
//! one the organ is keyed by, and that every framework path the row contract names goes through the
//! shelf.

use quote::ToTokens as _;

use super::{columns, row};

/// The rendered row contract with every space taken out — what a claim about a PATH or a call is
/// read off, for [`resource`](crate::resource)'s reason: a path long enough to wrap arrives with
/// newlines inside its own turbofish.
fn compact(krate: &syn::Path) -> String {
  rendered(krate).split_whitespace().collect()
}

/// The rendered row contract, formatted — what a failing claim prints.
fn rendered(krate: &syn::Path) -> String {
  let emitted = row(
    krate,
    &syn::parse_quote!(VideoJournal),
    &syn::parse_quote!(MediaId),
  );
  prettyplease::unparse(&syn::parse2(emitted).expect("the expansion parses as a file"))
}

/// The four columns a journal is keyed and read by, in the order the emitted struct declares them.
fn shape() -> Vec<(String, String)> {
  columns(&syn::parse_quote!(MediaId), &syn::parse_quote!(::ingraph))
    .named
    .into_iter()
    .map(|field| {
      (
        field.ident.expect("a named field").to_string(),
        field.ty.to_token_stream().to_string().replace(' ', ""),
      )
    })
    .collect()
}

/// One field's `#[ingraph(…)]` markers, compacted.
fn markers(field: &str) -> String {
  columns(&syn::parse_quote!(MediaId), &syn::parse_quote!(::ingraph))
    .named
    .into_iter()
    .find(|declared| declared.ident.as_ref().is_some_and(|name| name == field))
    .expect("the canonical column")
    .attrs
    .iter()
    .filter(|attr| attr.path().is_ident("ingraph"))
    .map(|attr| attr.to_token_stream().to_string().replace(' ', ""))
    .collect()
}

/// **THE CANONICAL SHAPE** — four columns, and each is the one the organ's semantics names.
#[test]
fn a_journal_is_its_four_columns() {
  assert_eq!(
    shape(),
    [
      ("root".to_owned(), "MediaId".to_owned()),
      ("node".to_owned(), "::std::string::String".to_owned()),
      ("station".to_owned(), "::std::string::String".to_owned()),
      (
        "at".to_owned(),
        "::ingraph::__private::primitives::Timestamp".to_owned()
      ),
    ]
  );
}

/// **`(root, node)` is the KEY, and nothing declares it**: `root` is the lineage and `node` is the
/// id, so `key = lineage ⊕ id` spells the pair the entry is replaced on.
///
/// The lineage half is the FRONT END's — it writes `lineage(fields(root))` — and the id half is
/// here, on the field. What this pins is the second: an id anywhere else makes the pair something
/// else, and `at` is the one that would make the table an append log.
#[test]
fn the_node_carries_the_id_and_the_instant_does_not() {
  assert_eq!(markers("node"), "#[ingraph(id)]");
  assert_eq!(markers("at"), "#[ingraph(scalar)]");
}

/// A KEY-side column carries no family label, and cannot: the record grammar refuses one there,
/// a key column's crossing being decided by its type.
#[test]
fn the_key_side_columns_carry_no_label() {
  assert_eq!(markers("root"), "");
  assert_eq!(markers("node"), "#[ingraph(id)]");
}

/// **THE MINT DOES NOT KNOW WHICH RUNG IT LANDS ON**, and after the participation inversion it has
/// nothing it could know it for.
///
/// This test used to read *an unstored journal mints no `persist` marker* and took a `stored` flag
/// to prove it, because the marker was what put a column in a row and the mint wrote one only where
/// a storage block had been. Data fields ARE the row now; no journal mints that marker, so the flag
/// had one value and the branch it selected was gone. What survives is the claim underneath, which
/// is the stronger one: the four columns and their four markers are the SAME whether the journal
/// named a store or not, so the rung is the front end's decision alone and no column shape follows
/// from it.
#[test]
fn the_mint_is_one_shape_on_either_rung() {
  assert_eq!(markers("root"), "");
  assert_eq!(markers("node"), "#[ingraph(id)]");
  assert_eq!(markers("station"), "#[ingraph(string)]");
  assert_eq!(markers("at"), "#[ingraph(scalar)]");
}

/// The row contract is the ONE thing the record expansion does not already write, and it is the same
/// trait the hand path implements by hand.
#[test]
fn the_row_contract_is_emitted_for_the_named_root() {
  let emitted = compact(&syn::parse_quote!(::ingraph));
  assert!(
    emitted.contains("impl::ingraph::__private::drive::JournalRow<MediaId>forVideoJournal"),
    "{}",
    rendered(&syn::parse_quote!(::ingraph))
  );
}

/// **The `SystemTime` crossing STOPS rather than substituting**, which is the posture the module
/// note records — a `From` that saturated would write a durable row asserting a moment that never
/// happened.
#[test]
fn the_instant_crossing_refuses_rather_than_rounding() {
  let emitted = compact(&syn::parse_quote!(::ingraph));
  assert!(
    emitted.contains("::core::convert::TryFrom<::std::time::SystemTime")
      && emitted.contains("::try_from(at)"),
    "{}",
    rendered(&syn::parse_quote!(::ingraph))
  );
  assert!(
    emitted.contains("::core::panic!"),
    "the refusal arm stops: {}",
    rendered(&syn::parse_quote!(::ingraph))
  );
}

/// Every framework path goes through the shelf, so a declaration needs `ingraph` in its manifest and
/// nothing else.
#[test]
fn every_framework_path_goes_through_the_shelf() {
  let krate = syn::parse_quote!(::ingraph);
  let emitted = compact(&krate);
  for path in emitted.match_indices("::ingraph::").map(|(at, _)| at) {
    assert!(
      emitted[path..].starts_with("::ingraph::__private::"),
      "a framework path skips the shelf: {}",
      rendered(&krate)
    );
  }
}

/// A renamed dependency moves every emitted path and nothing else — including the `at` column's own
/// declared type, which is the one place a canonical column names the framework from inside the
/// declarer's own struct.
#[test]
fn a_renamed_crate_moves_the_paths() {
  let krate: syn::Path = syn::parse_quote!(crate::shim);
  let emitted = compact(&krate);
  assert!(
    emitted.contains("crate::shim::__private::drive::JournalRow<MediaId>forVideoJournal"),
    "{}",
    rendered(&krate)
  );
  assert!(
    !emitted.contains("::ingraph::"),
    "nothing still names the default crate: {}",
    rendered(&krate)
  );

  let at = columns(&syn::parse_quote!(MediaId), &krate)
    .named
    .into_iter()
    .find(|field| field.ident.as_ref().is_some_and(|name| name == "at"))
    .expect("the instant column")
    .ty
    .to_token_stream()
    .to_string()
    .replace(' ', "");
  assert_eq!(at, "crate::shim::__private::primitives::Timestamp");
}