ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! THE RECORD's own shape: what a recipe states, and — the half that matters — what it refuses to
//! state on the declaration's behalf.

use core::num::NonZeroU32;

use super::SearchModel;
use crate::{
  index::Dedup,
  search::{ArmModel, MemberModel, SeatModel, SharedBinding, SharedBridge},
};

fn ident(name: &str) -> syn::Ident {
  syn::Ident::new(name, proc_macro2::Span::call_site())
}

/// A member reading `ty`, with nothing declared about its route and no tower bound.
fn member(field: &str, ty: &str) -> MemberModel {
  MemberModel {
    field: ident(field),
    ty: syn::parse_str(ty).expect("a member type"),
    weight: None,
    depth: None,
    floor: None,
    arms: Vec::new(),
  }
}

/// One arm of a member's question, bound to `tower`.
fn arm(name: &str, tower: &str) -> ArmModel {
  ArmModel {
    name: ident(name),
    tower: syn::parse_str(tower).expect("a tower type"),
    shared: None,
    floor: None,
  }
}

/// A recipe over `members`, joining under `dedup`.
fn recipe(members: Vec<MemberModel>, dedup: Dedup) -> SearchModel {
  SearchModel {
    krate: syn::parse_quote!(::ingraph),
    vis: syn::parse_quote!(pub),
    name: ident("TravelSearch"),
    seats: Vec::new(),
    dedup,
    limit: None,
    graphql: None,
    members,
  }
}

/// The record with its spaces taken out.
fn tight(model: &SearchModel) -> String {
  model.record().to_string().split_whitespace().collect()
}

/// The LOCKED example's own recipe, as a model.
fn travel() -> SearchModel {
  let mut subs = member("subs", "Subs");
  subs.weight = Some(1.0);
  subs.depth = NonZeroU32::new(50);
  subs.arms.push(ArmModel {
    shared: Some(SharedBinding {
      seat: ident("text"),
      bridge: SharedBridge::Total,
    }),
    ..arm("zh", "crate::ZhQuery")
  });

  let mut shots = member("shots", "Shots");
  shots.weight = Some(2.0);
  shots.depth = NonZeroU32::new(100);
  shots.arms.push(ArmModel {
    shared: Some(SharedBinding {
      seat: ident("text"),
      bridge: SharedBridge::Total,
    }),
    floor: Some(syn::parse_quote!(crate::floors::SEM_TEXT)),
    ..arm("text", "crate::ClipText")
  });
  shots.arms.push(ArmModel {
    shared: Some(SharedBinding {
      seat: ident("text"),
      bridge: SharedBridge::Total,
    }),
    ..arm("image", "crate::ClipImage")
  });

  let mut model = recipe(vec![subs, shots], Dedup::Hash);
  model.seats.push(SeatModel {
    name: ident("text"),
    ty: syn::parse_quote!(String),
  });
  model.limit = NonZeroU32::new(20);
  model
}

/// **THE LOCKED DECLARATION STATES THE LOCKED ROW** — the record the round's constitution wrote out
/// by hand, now minted.
#[test]
fn the_locked_recipe_states_the_locked_record() {
  let emitted = tight(&travel());
  assert!(
    emitted.contains("impl::ingraph::__private::retrieve::DeclaredSearchforTravelSearch{"),
    "{emitted}",
  );
  assert!(
    emitted.contains("&[::ingraph::__private::retrieve::Seat::of(\"text\"),]"),
    "the broadcast seat is missing from the record: {emitted}",
  );
  assert!(
    emitted.contains("Member::of(\"subs\").weighted(1f64).deep(50u32).bound(&["),
    "the member's declared numbers are missing: {emitted}",
  );
  assert!(
    emitted.contains("Binding::of(\"zh\").shared(::ingraph::__private::retrieve::Bridge::Total)"),
    "the arm's bridge is missing: {emitted}",
  );
  assert!(
    emitted.contains(
      "LIMIT:::core::option::Option<::core::num::NonZeroU32>=::core::num::NonZeroU32::new(20u32)"
    ),
    "the recipe's own limit is missing: {emitted}",
  );
}

/// **THE FLOOR IS A FACT AND NEVER A VALUE** — the const is in the ENGINE's own currency, an open
/// type with no seat on a shared row, so what crosses the wall is `floored()` and nothing else.
#[test]
fn a_floor_crosses_as_the_fact_and_not_the_const() {
  let emitted = tight(&travel());
  assert!(
    emitted.contains(
      "Binding::of(\"text\").shared(::ingraph::__private::retrieve::Bridge::Total).floored()"
    ),
    "the arm's floor did not cross as a fact: {emitted}",
  );
  assert!(
    !emitted.contains("SEM_TEXT"),
    "the record carries a value in a currency it cannot type: {emitted}",
  );
}

/// **AN OMITTED WORD IS AN ABSENT ROW** — never the framework's own number written down, which is
/// what would make an unwritten word outrank a declaration that wrote one.
#[test]
fn an_omitted_word_states_nothing() {
  let emitted = tight(&recipe(vec![member("subs", "Subs")], Dedup::Hash));
  assert!(
    emitted.contains("Member::of(\"subs\").bound(&[])"),
    "a member that declared nothing carries a value: {emitted}",
  );
  assert!(
    emitted.contains(
      "LIMIT:::core::option::Option<::core::num::NonZeroU32>=::core::option::Option::None"
    ),
    "an omitted limit became a number: {emitted}",
  );
  assert!(
    emitted.contains("SEATS:&'static[::ingraph::__private::retrieve::Seat]=&[]"),
    "a recipe with no broadcast states one: {emitted}",
  );
  assert!(!emitted.contains("20u32"), "{emitted}");
}

/// **EVERY BRIDGE WORD REACHES ITS OWN VARIANT**, the hook's PATH staying out of the record — it
/// names an item, and the recipe's own emission is what calls it.
#[test]
fn each_crossing_states_its_own_word() {
  for (bridge, word) in [
    (SharedBridge::Total, "Total"),
    (SharedBridge::Fallible, "Fallible"),
    (SharedBridge::Hook(syn::parse_quote!(app::cross)), "Hook"),
    (
      SharedBridge::FallibleHook(syn::parse_quote!(app::cross)),
      "FallibleHook",
    ),
  ] {
    let mut subs = member("subs", "Subs");
    subs.arms.push(ArmModel {
      shared: Some(SharedBinding {
        seat: ident("text"),
        bridge,
      }),
      ..arm("zh", "crate::ZhQuery")
    });
    let emitted = tight(&recipe(vec![subs], Dedup::Hash));
    assert!(
      emitted.contains(&format!("Bridge::{word})")),
      "the crossing did not reach its variant: {emitted}",
    );
    assert!(!emitted.contains("app::cross"), "{emitted}");
  }
}