ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! What the resource declaration's expansion says, read off its tokens.
//!
//! The expansion's BEHAVIOUR is asserted where it can be run — `ingraph-derive`'s own fixtures,
//! which expand a declaration for real and reach the citizenship through it. What is here is the
//! two things a token stream can be read for: that both halves are emitted, and that every
//! framework path they name goes through the shelf.

use super::{ResourceMode, ResourceModel};
use crate::prefix::EmissionPrefix;

/// A declaration, as the derive builds one.
fn egemma() -> ResourceModel {
  ResourceModel {
    prefix: EmissionPrefix::default(),
    krate: syn::parse_quote!(::ingraph),
    name: syn::parse_quote!(Egemma),
    kind: None,
    mode: ResourceMode::Block,
  }
}

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

/// The rendered expansion, formatted — what a failing claim prints.
fn rendered(model: &ResourceModel) -> String {
  prettyplease::unparse(&syn::parse2(model.emit()).expect("the expansion parses as a file"))
}

/// The CITIZENSHIP: one impl, for the declared type, of the trait the walk enumerates.
#[test]
fn a_resource_declares_its_citizenship() {
  let model = egemma();
  let emitted = compact(&model);
  assert!(
    emitted.contains(
      "impl::ingraph::__private::resource::DeclaredResourceforEgemma{constSYNC:bool=true;}"
    ),
    "the citizenship impl is emitted: {}",
    rendered(&model)
  );
}

/// ONE ITEM and no audit beside it.
///
/// A second, `declares`-shaped assert was written here and removed: rustc reports the missing
/// contract at the declaration's own type name through the impl's SUPERTRAIT either way, so the
/// audit was a duplicate error. This is what stops it being rediscovered — a reader adding one
/// makes this red rather than making the fixtures grow a second copy of their own message.
///
/// The impl is no longer EMPTY — it carries the mode the declaration recorded — and the count is
/// what the rule was always about: ONE item, whatever is inside it.
#[test]
fn the_citizenship_impl_is_the_whole_expansion() {
  let model = egemma();
  let rendered = rendered(&model);
  assert!(!rendered.contains("declares"), "{rendered}");
  assert!(!rendered.contains("const _"), "{rendered}");
  assert_eq!(
    rendered
      .lines()
      .filter(|line| line.contains("impl"))
      .count(),
    1,
    "{rendered}"
  );
}

/// 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 model = egemma();
  let emitted = compact(&model);
  for path in emitted.match_indices("::ingraph::").map(|(at, _)| at) {
    assert!(
      emitted[path..].starts_with("::ingraph::__private::"),
      "a framework path skips the shelf: {}",
      rendered(&model)
    );
  }
}

/// A renamed dependency moves every emitted path and nothing else.
#[test]
fn a_renamed_crate_moves_the_paths() {
  let model = ResourceModel {
    prefix: EmissionPrefix::default(),
    krate: syn::parse_quote!(crate::shim),
    name: syn::parse_quote!(Egemma),
    kind: None,
    mode: ResourceMode::Block,
  };
  let emitted = compact(&model);
  assert!(
    emitted.contains(
      "crate::shim::__private::resource::DeclaredResourceforEgemma{constSYNC:bool=true;}"
    ),
    "the citizenship impl reaches the named crate: {}",
    rendered(&model)
  );
  assert!(
    !emitted.contains("::ingraph::"),
    "nothing still names the default crate: {}",
    rendered(&model)
  );
}