ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **LAYER TWO'S DECLARATION HALF**, read off the tokens: the `Describe<R>` a pointed leg mints, the
//! `Habitats` walk that drives it, and the `UNBOUND` list a leg that pointed at nothing leaves
//! behind.
//!
//! What the tokens can say is that a declaration states its homes ONCE PER TARGET, that the corpus
//! names in the blueprint are the slot markers' own, and that the two halves partition the legs —
//! every leg is either a step or a row and never both. What they cannot say is that the walk MAKES
//! anything: that is `ingraph/tests/init_walk_modes.rs` and the slot maker's own suite, where a
//! launch runs and directories appear.
//!
//! Beside [`super::kind_audits`] on this directory's one-file-per-thing precedent, and reading its
//! fixtures for the same reason: the question here is about the same declarations one layer on.

use quote::ToTokens as _;

use super::{IndexModel, NodeKind};
use crate::engine::{Engine, FieldEngines};

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

/// The whole expansion with the spaces taken out — what a claim about a `const` or a call is read
/// off.
fn compact(model: &IndexModel) -> String {
  model
    .emit()
    .to_token_stream()
    .to_string()
    .split_whitespace()
    .collect()
}

/// A FULL-TEXT declaration over two elections, each pointing where the arguments say.
fn fulltext(title: Option<syn::Path>, body: Option<syn::Path>) -> IndexModel {
  let mut model = super::answers::plain();
  model.kind = NodeKind::Fulltext;
  model.embeddings = Vec::new();
  model.searchable = vec![ident("title"), ident("body")];
  model.engines = vec![
    FieldEngines {
      field: ident("title"),
      engines: vec![Engine {
        resource: title,
        ..Engine::bare(ident("lexical"))
      }],
      fanout: false,
    },
    FieldEngines {
      field: ident("body"),
      engines: vec![Engine {
        resource: body,
        stored: true,
        ..Engine::bare(ident("lexical"))
      }],
      fanout: false,
    },
  ];
  model
}

/// A VECTOR declaration whose one election points at `resource`.
fn vector(resource: Option<syn::Path>) -> IndexModel {
  let mut model = super::answers::plain();
  model.engines = vec![FieldEngines {
    field: ident("clip"),
    engines: vec![Engine {
      resource,
      ..Engine::bare(ident("toy"))
    }],
    fanout: false,
  }];
  model
}

/// **TWO ELECTIONS IN ONE MOUNT ARE TWO ROWS OF ONE BLUEPRINT**, which they have to be: `Describe<R>`
/// admits one impl per `(type, target)` pair, so a second would be `E0119` rather than a second
/// corpus.
#[test]
fn two_legs_pointing_at_one_target_share_one_impl() {
  let emitted = compact(&fulltext(
    Some(syn::parse_quote!(crate::Ft)),
    Some(syn::parse_quote!(crate::Ft)),
  ));
  assert_eq!(
    emitted.matches("implDescribe<crate::Ft>forShots").count()
      + emitted
        .matches("impl::ingraph::__private::schema::Describe<crate::Ft>forShots")
        .count(),
    1,
    "one target, one impl: {emitted}"
  );
  assert!(
    emitted.contains(
      "::ingraph::__private::slot::SlotName::new(\"shots__title__lexical\"),&[::ingraph::__privat\
       e::transform::SlotColumn::text(\"title\")],false)"
    ),
    "{emitted}"
  );
  assert!(
    emitted.contains(
      "::ingraph::__private::slot::SlotName::new(\"shots__body__lexical\"),&[::ingraph::__private\
       ::transform::SlotColumn::text(\"body\")],true)"
    ),
    "the `stored` word rides the blueprint: {emitted}"
  );
}

/// **TWO TARGETS ARE TWO IMPLS**, and the walk drives one step apiece — the mixed deployment's
/// shape, where each step is on the face its own resource declared.
#[test]
fn two_targets_are_two_impls_and_two_steps() {
  let emitted = compact(&fulltext(
    Some(syn::parse_quote!(crate::Ft)),
    Some(syn::parse_quote!(crate::Remote)),
  ));
  assert!(emitted.contains("Describe<crate::Ft>forShots"), "{emitted}");
  assert!(
    emitted.contains("Describe<crate::Remote>forShots"),
    "{emitted}"
  );
  for resource in ["crate::Ft", "crate::Remote"] {
    assert!(
      emitted.contains(&format!(
        "::ingraph::__private::drive::Creation::habitat::<{resource},::ingraph::__private::operator\
         ::Selected<{{<{resource}as::ingraph::__private::resource::DeclaredResource>::SYNC}}>,Self,\
         >(__ingraph_creation).await?;"
      )),
      "the step reads the target's own MODE off its declaration: {emitted}"
    );
  }
}

/// **THE TARGETS ARE THE MOUNTS** — one demand row per pointed target, which is what makes a launch
/// build a store this deployment only READS.
///
/// One row per TARGET rather than per leg, on the grouping's own rule: two elections in one mount
/// are two corpora and one store to build.
#[test]
fn the_pointed_targets_are_stated_as_demands() {
  let one = compact(&fulltext(
    Some(syn::parse_quote!(crate::Ft)),
    Some(syn::parse_quote!(crate::Ft)),
  ));
  assert!(
    one.contains(
      "constTARGETS:&'static[fn()->::ingraph::__private::resource::ResourceDemand]=&[::ingraph::__p\
       rivate::resource::ResourceDemand::of::<crate::Ft>,];"
    ),
    "two legs in one mount are ONE demand: {one}"
  );

  let two = compact(&fulltext(
    Some(syn::parse_quote!(crate::Ft)),
    Some(syn::parse_quote!(crate::Remote)),
  ));
  assert!(
    two.contains(
      "&[::ingraph::__private::resource::ResourceDemand::of::<crate::Ft>,::ingraph::__private::reso\
       urce::ResourceDemand::of::<crate::Remote>,];"
    ),
    "two targets are two demands, in the order the legs named them: {two}"
  );
}

/// A declaration that points at nothing states NO mount — the one laziness the ruling left: a
/// resource nothing points at and no enabled node demands is neither built nor walked.
#[test]
fn a_declaration_with_no_pointed_leg_states_no_mount() {
  let emitted = compact(&fulltext(None, None));
  assert!(!emitted.contains("constTARGETS:"), "{emitted}");
}

/// **A LEG THAT POINTED AT NOTHING IS A ROW AND NEVER A STEP** — there is no type for a step to
/// name, and what settles it is the deployment's catalog at launch.
#[test]
fn an_unpointed_leg_becomes_an_unbound_row() {
  let emitted = compact(&fulltext(Some(syn::parse_quote!(crate::Ft)), None));
  assert!(
    emitted.contains(
      "constUNBOUND:&'static[::ingraph::__private::drive::UnboundCorpus]=&[::ingraph::__private::dr\
       ive::UnboundCorpus::new(::ingraph::__private::slot::SlotName::new(\"shots__body__lexical\"\
       ),::ingraph::__private::resource::ResourceKind::Documents),];"
    ),
    "{emitted}"
  );
  assert!(
    !emitted.contains("Describe<crate::Remote>"),
    "an unpointed leg names no target: {emitted}"
  );
}

/// A declaration whose every leg pointed at nothing takes the trait's own empty walk — a body whose
/// parameter nothing reads is a warning in somebody else's crate.
#[test]
fn a_declaration_with_no_pointed_leg_emits_no_walk() {
  let emitted = compact(&fulltext(None, None));
  assert!(emitted.contains("constUNBOUND:"), "{emitted}");
  assert!(
    !emitted.contains("fnmake("),
    "nothing to make, so nothing is written: {emitted}"
  );
}

/// **THE VECTOR KIND DESCRIBES ITSELF IN ITS OWN VOCABULARY** — a `SlotSpec` with the WIDTH projected
/// off the elected field's own type, which is the one member the two families differ in.
#[test]
fn a_vector_corpus_carries_the_width_its_field_declares() {
  let emitted = compact(&vector(Some(syn::parse_quote!(crate::Vectors))));
  assert!(
    emitted.contains("::ingraph::__private::transform::SlotSpec::new("),
    "{emitted}"
  );
  assert!(
    emitted.contains(
      "::ingraph::__private::transform::SlotColumn::new(\"clip\",::ingraph::__private::store::Colum\
       nDoor::Bytes)"
    ),
    "a coordinate crosses as bytes: {emitted}"
  );
  assert!(
    emitted.contains(
      "<<::ingraph::Embedding<f32,4,Clip>as::ingraph::__private::election::EmbeddingMarker>::Itemas::in\
       graph::__private::election::EmbeddingShape>::WIDTH"
    ),
    "the width is the FIELD's, projected rather than restated: {emitted}"
  );
  assert!(
    emitted.contains("::ingraph::__private::resource::ResourceKind::Coordinates")
      || !emitted.contains("constUNBOUND"),
    "a pointed vector leg leaves no unbound row: {emitted}"
  );
}

/// A BLOB declaration emits the impl and nothing in it: it elects nothing, so there is no
/// `(field × label)` slot for a leg to point at.
#[test]
fn a_blob_declaration_owns_no_corpus_and_says_so() {
  let mut model = super::answers::plain();
  model.kind = NodeKind::Blob;
  model.embeddings = Vec::new();
  model.engines = Vec::new();
  let emitted = compact(&model);
  assert!(
    emitted.contains("impl::ingraph::__private::drive::HabitatsforShots{}"),
    "{emitted}"
  );
}