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 BLOB RIDER — the durable `slot` word, and the two words this kind refuses with a reason.
//!
//! Beside [`artifacts`](super::artifacts) rather than inside it, on this family's one-file-per-thing
//! precedent: that file is the `extension` word and the address it ends, and this one is the naming
//! round's three verdicts on the same kind.
//!
//! # The three, and why they are one file
//!
//! `slot = "…"` is the DURABLE name — the one word in this grammar that reaches a deployment's
//! disk. `rename` and `graphql(…)` are the two the round granted every kind and this one cannot
//! spend: it mints no Rust type, and it mints no wire type. Reading the three together is what makes
//! the shape legible — an artifact family's only nameable thing is where its objects live.

use ingraph_codegen::{index::IndexModel, prelude::NodeKind};

use super::tests::refused;

/// A blob whose head says `head`.
fn blob(head: proc_macro2::TokenStream) -> syn::Result<IndexModel> {
  let (host, arguments) = crate::host::split(&syn::parse_quote! {
    #[ingraph(upstream(node = "a::A", payload = "pixels"), fs, #head)]
    struct Poster { #[ingraph(repeated)] payload: Vec<u8> }
  });
  super::super::index::parse(&host, &arguments, NodeKind::Blob)
}

#[test]
fn the_slot_word_reaches_the_model_and_the_default_is_absence() {
  let named = blob(quote::quote! { slot = "poster_frames" }).expect("a named slot is legal");
  assert_eq!(
    named.slot.as_ref().map(syn::LitStr::value).as_deref(),
    Some("poster_frames"),
  );

  // ABSENCE IS THE DEFAULT and not an unset field: a family written before the word arrived is
  // stored under `snake_case(<declaration name>)`, and that is what `None` stands for.
  let plain = blob(quote::quote! { extension = "png" }).expect("legal");
  assert!(plain.slot.is_none());
  // THE EXCLUSION: `slot` is claimed before a blob's open engine position, so it never reached the
  // store list as an engine called `slot`.
  assert_eq!(
    named
      .stores
      .iter()
      .map(|engine| engine.word.to_string())
      .collect::<Vec<_>>(),
    vec!["fs".to_owned()],
  );
}

#[test]
fn a_slot_name_the_store_could_not_hold_is_refused_where_it_was_written() {
  for written in ["", "9frames", "poster frames", "poster-frames"] {
    let head = {
      let name = syn::LitStr::new(written, proc_macro2::Span::call_site());
      quote::quote! { slot = #name }
    };
    let e = blob(head)
      .expect_err("a malformed slot name is refused")
      .into_iter()
      .map(|e| e.to_string())
      .collect::<Vec<_>>()
      .join("\n");
    assert!(e.contains("is not a slot name"), "`{written}`: {e}");
  }
}

#[test]
fn one_family_lives_in_one_place() {
  let e = blob(quote::quote! { slot = "a", slot = "b" })
    .expect_err("a second slot is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join("\n");
  assert!(e.contains("declared more than once"), "{e}");
  assert!(e.contains("ONE place"), "{e}");
}

#[test]
fn the_retrieving_kinds_are_told_the_grain_rather_than_offered_the_word() {
  for kind in [NodeKind::Fulltext, NodeKind::Vector] {
    let e = refused(
      syn::parse_quote! {
        #[ingraph(upstream(node = "a::A", body = "text"), dedup(hash), slot = "corpus")]
        struct D { #[ingraph(searchable)] body: String }
      },
      kind,
    );
    assert!(e.contains("owns one per `(field × engine)` PAIRING"), "{e}");
    assert!(
      e.contains("__"),
      "and the join that keeps two pairings apart: {e}",
    );
  }
}

#[test]
fn the_three_verdicts_of_the_naming_round_stand_together_on_this_kind() {
  // Read as one claim because they are one shape: an artifact family's only nameable thing is where
  // its objects live. `slot` is granted; `rename` and `graphql(…)` are refused, each with the
  // measurement behind it rather than with a wait.
  assert!(blob(quote::quote! { slot = "poster_frames" }).is_ok());

  let renamed = blob(quote::quote! { rename = "Poster" })
    .expect_err("this kind mints no type")
    .to_string();
  assert!(renamed.contains("lands no type in your scope"), "{renamed}");
  assert!(
    renamed.contains("slot = \"…\""),
    "and it points at the word this kind DOES have: {renamed}",
  );

  let dressed = blob(quote::quote! { graphql(async_graphql) })
    .expect_err("this kind mints no wire type")
    .to_string();
  assert!(
    dressed.contains("mints no wire type of its own"),
    "{dressed}"
  );
}