ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! What the MODEL itself decides, put to a model built by hand.
//!
//! The grammar's own tests live beside the front end that reads tokens; these are about the model a
//! validated declaration becomes — the questions the emission asks it, which are answerable without
//! a single token.

use super::{ArmModel, MemberModel, SearchModel, SharedBinding, SharedBridge};

/// A recipe carrying `members`, and nothing else the emission would read.
fn recipe(members: Vec<MemberModel>) -> SearchModel {
  SearchModel {
    krate: syn::parse_quote!(::ingraph),
    name: syn::parse_quote!(TravelSearch),
    vis: syn::parse_quote!(pub),
    seats: Vec::new(),
    dedup: crate::index::Dedup::Hash,
    limit: None,
    graphql: None,
    members,
  }
}

/// A plain coordinate member — a field, an index, and nothing declared about its route.
fn member(field: &str, arms: Vec<ArmModel>) -> MemberModel {
  MemberModel {
    field: syn::Ident::new(field, proc_macro2::Span::call_site()),
    ty: syn::parse_quote!(Shots),
    weight: None,
    depth: None,
    floor: None,
    arms,
  }
}

/// One bound tower, on the total crossing.
fn arm(name: &str) -> ArmModel {
  ArmModel {
    name: syn::Ident::new(name, proc_macro2::Span::call_site()),
    tower: syn::parse_quote!(crate::ClipText),
    shared: Some(SharedBinding {
      seat: syn::Ident::new("text", proc_macro2::Span::call_site()),
      bridge: SharedBridge::Total,
    }),
    floor: None,
  }
}

/// A recipe of PLAIN members binds nothing, whatever else it declared.
#[test]
fn a_recipe_with_no_arm_is_not_bound() {
  assert!(!recipe(vec![member("subs", Vec::new())]).bound());
}

/// ONE bound member is enough: the question layer arrives for the whole recipe or for none of it.
#[test]
fn one_bound_member_binds_the_recipe() {
  let model = recipe(vec![
    member("subs", Vec::new()),
    member("shots", vec![arm("text")]),
  ]);
  assert!(model.bound());
}

/// The four words, each answering the two questions the emission spends them on.
#[test]
fn every_bridge_states_its_word_and_whether_it_can_fail() {
  let hook: syn::Path = syn::parse_quote!(crate::hooks::widen);
  let table = [
    (SharedBridge::Total, "shared", false, false),
    (SharedBridge::Fallible, "try", true, false),
    (SharedBridge::Hook(hook.clone()), "with", false, true),
    (SharedBridge::FallibleHook(hook), "try_with", true, true),
  ];

  for (bridge, word, fallible, hooked) in table {
    assert_eq!(bridge.word(), word);
    assert_eq!(bridge.fallible(), fallible, "{word}");
    assert_eq!(bridge.hook().is_some(), hooked, "{word}");
  }
}

/// A HOOK is carried as the path it was written as — the emission calls it, so nothing here may
/// re-spell it.
#[test]
fn a_hook_is_the_path_the_word_named() {
  let bridge = SharedBridge::FallibleHook(syn::parse_quote!(crate::hooks::widen));
  let hook = bridge.hook().expect("the word named one");
  assert_eq!(
    quote::quote!(#hook).to_string().replace(' ', ""),
    "crate::hooks::widen"
  );
}