ingraph-derive 0.1.0

The attribute and derive macros for ingraph: the words a record, node, key, filter and vocabulary declaration is written in.
Documentation
//! THE `graphql(…)` BLOCK, read off the head — which backends dress a declaration, and every way
//! the word can be written wrong.
//!
//! Beside [`tests`](super::tests) rather than inside it, on [`engines`](super::engines)' precedent:
//! one file per thing being proved, so a reader looking for what the word MEANS finds it whole.
//!
//! What is asserted here is the MODEL and the SENTENCE. What the word does to an EMISSION is
//! `ingraph-codegen`'s `fuse/skins.rs`, where the two token streams can be compared side by side,
//! and the rendered carets are `tests/refusals/`.
//!
//! # The ACCEPTING tests state an exclusion too
//!
//! *The block parsed* would pass for a grammar that swallowed the word and produced nothing, so each
//! acceptance also names what the model has to hold: the declaration's OWN facts survive beside it,
//! and the backend list is the one that was written rather than a default.

use ingraph_codegen::{
  index::IndexModel,
  model::GraphqlKind,
  prelude::{NodeKind, Projection},
};

use super::tests::{fulltext, refused};

/// Which backends a parsed declaration named, as the words it wrote them with.
fn backends(model: &IndexModel) -> Vec<&'static str> {
  model.graphql.as_ref().map_or_else(Vec::new, |block| {
    block
      .backends
      .iter()
      .map(|backend| backend.kind.as_str())
      .collect()
  })
}

#[test]
fn a_declaration_that_wrote_no_block_names_no_backend() {
  let model = fulltext(&syn::parse_quote! {
    #[ingraph(upstream(node = "a::A", body = "text"), dedup(hash))]
    struct D { #[ingraph(searchable)] body: String }
  })
  .expect("a declaration with no wire face is legal");
  assert!(
    model.graphql.is_none(),
    "no block is *no wire face*, and it is a form rather than an unset field",
  );
  // THE EXCLUSION: the surface the ruling calls the BONES is a fact of the model whatever the word
  // said, so a declaration with no block still elects, still projects, still dedupes.
  assert_eq!(model.searchable.len(), 1);
  assert_eq!(model.source.projections.len(), 1);
}

#[test]
fn the_backend_word_is_carried_as_the_backend_it_names() {
  let model = fulltext(&syn::parse_quote! {
    #[ingraph(
      upstream(node = "a::A", body = "text"),
      dedup(hash),
      graphql(async_graphql),
    )]
    struct D { #[ingraph(searchable)] body: String }
  })
  .expect("a declaration naming a backend is legal");
  assert_eq!(backends(&model), vec![GraphqlKind::AsyncGraphql.as_str()]);
  assert_eq!(model.searchable.len(), 1, "and the election is untouched");
}

#[test]
fn the_block_reaches_the_vector_kind_by_the_same_words() {
  let (host, arguments) = crate::host::split(&syn::parse_quote! {
    #[ingraph(
      upstream(node = "a::A", clip = "clip"),
      dedup(hash),
      graphql(async_graphql),
    )]
    struct D {
      #[ingraph(embedding, knn)]
      clip: ::ingraph::Embedding<f32, 4, Clip>,
    }
  });
  let model =
    super::super::index::parse(&host, &arguments, NodeKind::Vector).expect("a vector may render");
  assert_eq!(backends(&model), vec![GraphqlKind::AsyncGraphql.as_str()]);
}

#[test]
fn a_bare_word_names_no_backend_and_is_refused_with_the_words_on_offer() {
  let e = refused(
    syn::parse_quote! {
      #[ingraph(upstream(node = "a::A", body = "text"), dedup(hash), graphql)]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(e.contains("names no backend"), "{e}");
  assert!(
    e.contains("graphql(async_graphql)"),
    "and the sentence spells the word its author is missing: {e}",
  );
  assert!(
    e.contains("WHOLE Rust surface"),
    "and says what is NOT missing without it: {e}",
  );
}

#[test]
fn an_unknown_backend_is_refused_with_the_ones_this_framework_renders_through() {
  let e = refused(
    syn::parse_quote! {
      #[ingraph(upstream(node = "a::A", body = "text"), dedup(hash), graphql(smear))]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(e.contains("unknown backend"), "{e}");
  assert!(e.contains(GraphqlKind::AsyncGraphql.as_str()), "{e}");
}

#[test]
fn one_backend_is_named_once_and_the_block_is_written_once() {
  let twice = refused(
    syn::parse_quote! {
      #[ingraph(
        upstream(node = "a::A", body = "text"),
        dedup(hash),
        graphql(async_graphql, async_graphql),
      )]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(twice.contains("one block per backend"), "{twice}");

  let two_blocks = refused(
    syn::parse_quote! {
      #[ingraph(
        upstream(node = "a::A", body = "text"),
        dedup(hash),
        graphql(async_graphql),
        graphql(async_graphql),
      )]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(
    two_blocks.contains("one block holds every backend"),
    "{two_blocks}",
  );
}

#[test]
fn an_artifact_family_refuses_the_block_with_its_reason() {
  let e = refused(
    syn::parse_quote! {
      #[ingraph(upstream(node = "a::A", payload = "pixels"), fs, graphql(async_graphql))]
      struct D { #[ingraph(repeated)] payload: Vec<u8> }
    },
    NodeKind::Blob,
  );
  // TRUTHFUL, which is the half the round changed: the old sentence said the surface *has not
  // arrived*, and what a blob's author needs told is that this kind mints no wire type at all.
  assert!(e.contains("mints no wire type of its own"), "{e}");
  assert!(
    e.contains("locate("),
    "and where its address and bytes actually live: {e}",
  );
  assert!(
    !e.contains("has not arrived"),
    "the wait was retired with the round: {e}",
  );
}

#[test]
fn the_word_is_claimed_before_a_blobs_open_engine_position() {
  // On a blob every word this grammar does not own is somebody's ENGINE, so a word it does own has
  // to be read above the catch-all. Were it not, `graphql` would be swallowed as a store called
  // `graphql` and the declaration would compile with a store nobody deployed.
  let e = refused(
    syn::parse_quote! {
      #[ingraph(upstream(node = "a::A", payload = "pixels"), fs, graphql(async_graphql))]
      struct D { #[ingraph(repeated)] payload: Vec<u8> }
    },
    NodeKind::Blob,
  );
  assert!(
    !e.contains("an engine is named by ONE word"),
    "the word was read as an engine rather than as this grammar's: {e}",
  );
  // And the store list is what the declaration actually named. Read through a LEGAL declaration,
  // since a refused one yields no model to look at.
  let (host, arguments) = crate::host::split(&syn::parse_quote! {
    #[ingraph(upstream(node = "a::A", payload = "pixels"), fs)]
    struct D { #[ingraph(repeated)] payload: Vec<u8> }
  });
  let model = super::super::index::parse(&host, &arguments, NodeKind::Blob).expect("legal");
  assert_eq!(
    model
      .stores
      .iter()
      .map(|engine| engine.word.to_string())
      .collect::<Vec<_>>(),
    vec!["fs".to_owned()],
  );
  assert_eq!(model.fields[0].project, Projection::Repeated);
}

/// One backend's block, as the model carried it.
fn skin(model: &IndexModel) -> &ingraph_codegen::index::IndexBackend {
  model
    .graphql
    .as_ref()
    .expect("the declaration named a backend")
    .skin(GraphqlKind::AsyncGraphql)
    .expect("that backend's block")
}

/// A full-text declaration whose backend block says `inside`.
///
/// `pub(super)` since the vocabulary correction split the base's own file off this one: the two
/// files ask about one block, so a second fixture beside them would be the same declaration written
/// twice.
pub(super) fn dressed(inside: proc_macro2::TokenStream) -> syn::Result<IndexModel> {
  let (host, arguments) = crate::host::split(&syn::parse_quote! {
    #[ingraph(
      upstream(node = "a::A", body = "text"),
      dedup(hash),
      graphql(async_graphql(#inside)),
    )]
    struct D { #[ingraph(searchable)] body: String }
  });
  super::super::index::parse(&host, &arguments, NodeKind::Fulltext)
}

#[test]
fn the_backend_block_carries_its_base_and_its_three_faces() {
  let model = dressed(quote::quote! {
    prefix = "shotSearch",
    options(rename = "ShotKnobs"),
    question(rename = "ShotAsk"),
    engine(rename = "WhichStore"),
  })
  .expect("a backend block naming its skin is legal");
  let skin = skin(&model);
  assert_eq!(
    skin.prefix.as_ref().map(syn::LitStr::value).as_deref(),
    Some("shotSearch")
  );
  assert_eq!(
    skin.options.as_ref().map(syn::LitStr::value).as_deref(),
    Some("ShotKnobs")
  );
  assert_eq!(
    skin.question.as_ref().map(syn::LitStr::value).as_deref(),
    Some("ShotAsk")
  );
  assert_eq!(
    skin.engine.as_ref().map(syn::LitStr::value).as_deref(),
    Some("WhichStore")
  );
  // THE EXCLUSION, and it is the axis law: nothing in this block touched a Rust name. The
  // declaration's own ident is what it was written as, and so is every mint built on it.
  assert_eq!(model.index.to_string(), "D");
}

#[test]
fn a_skin_word_that_is_not_a_graphql_name_is_refused_where_it_was_written() {
  let e = refused(
    syn::parse_quote! {
      #[ingraph(
        upstream(node = "a::A", body = "text"),
        dedup(hash),
        graphql(async_graphql(prefix = "9lives")),
      )]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(e.contains("is not a GraphQL name"), "{e}");
}

#[test]
fn a_rust_keyword_is_an_ordinary_word_in_a_skins_namespace() {
  // The 2026-08-13 ruling, one grammar over: these words name types in GraphQL's flat namespace and
  // nothing in Rust, so an ident parse would refuse a legal schema name for a Rust reason.
  let model =
    dressed(quote::quote! { options(rename = "type") }).expect("`type` is a legal GraphQL name");
  assert_eq!(
    skin(&model)
      .options
      .as_ref()
      .map(syn::LitStr::value)
      .as_deref(),
    Some("type"),
  );
}

/// The sentence carries a MEASUREMENT, and the measurement was re-taken after the fanned road grew
/// its answer face — so what is read here is the narrowed reason rather than the retired one.
///
/// The old sentence said *the whole answer side of a fused door is Rust-only today*, which stopped
/// being true the day `vector_fuse_by_<field>` published `<Index><Field>Sighting`. Its absence is
/// asserted beside the two clauses that survived, because a refusal that goes on stating a
/// measurement the tree has outgrown is the failure mode this whole family of pins exists against.
#[test]
fn the_evidence_face_is_refused_with_the_measurement_behind_it() {
  let e = refused(
    syn::parse_quote! {
      #[ingraph(
        upstream(node = "a::A", body = "text"),
        dedup(hash),
        graphql(async_graphql(fused(rename = "WhoAgreed"))),
      )]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(e.contains("no schema name to move"), "{e}");
  assert!(e.contains("GENERIC in the score"), "{e}");
  assert!(
    e.contains("mounts no query field at all"),
    "the TIER's door is what the word runs into: {e}",
  );
  assert!(
    !e.contains("Rust-only today"),
    "the retired over-claim: a fanned election's fused answer IS published: {e}",
  );
  assert!(
    e.contains("options(rename"),
    "and what the block CAN name: {e}",
  );
  assert!(
    e.contains("that field's own backend block"),
    "and where the live `fused` word is: {e}",
  );
}

#[test]
fn the_bone_axis_is_not_written_in_a_skins_block() {
  // A word from the OTHER axis inside this one earns the near-miss line rather than silence — and
  // the sentence says where the Rust rename lives, which is the thing its author actually wants.
  let e = refused(
    syn::parse_quote! {
      #[ingraph(
        upstream(node = "a::A", body = "text"),
        dedup(hash),
        graphql(async_graphql(evidence(rename = "WhoAgreed"))),
      )]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(e.contains("unknown entry"), "{e}");
  assert!(
    e.contains("The RUST family is re-based by the top-level"),
    "the sentence names the other axis: {e}",
  );
}