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 `receipt` WORD's grammar, read straight off the pass.
//!
//! A suite of its own beside [`crate::parse::options_tests`], whose shape this borrows for the same
//! reason: what is asserted here is ONE word — which shape it takes, which it refuses, and whether a
//! refusal names the fix.
//!
//! The word REFERS — it names a type the declaration did not mint — so it is written quoted, which
//! is the mint-bare/refer-quoted law rather than an exception to it.
//!
//! **Its absence is a different thing from `options`'s absence, and that is the one asymmetry worth
//! reading twice.** A node with no `options` word IS configured, by `()`, and the emission says so;
//! a declaration with no `receipt` word states NOTHING, and the emission is silent. The seat is a
//! sibling trait rather than a member of a facts trait exactly so that silence can cost nothing.
//!
//! **The pass is run at [`NodeKind::Entity`], because that is where the word lives.** A receipt is a
//! property of the thing GIVEN rather than of the seat that answers for it, so the word is
//! kind-gated to the rungs that MINT a message — entity, record, aggregate — and the kinds that mint
//! none earn a refusal of their own, asserted at the bottom of this file and fenced by a `.stderr`
//! in `ingraph/tests/receipt_words/`.

use ingraph_codegen::prelude::*;

/// The pass under test, at the kind this word belongs to.
fn entity(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
  let (host, options) = crate::host::split(input);
  crate::parse::parse(&host, &options, NodeKind::Entity)
}

/// Every refusal one declaration collects, as text.
fn refusals(input: syn::DeriveInput) -> Vec<String> {
  entity(&input)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect()
}

/// Every refusal one declaration collects AT `kind`, for the rules that are about the kind.
fn refusals_at(kind: NodeKind, input: syn::DeriveInput) -> Vec<String> {
  let (host, options) = crate::host::split(&input);
  crate::parse::parse(&host, &options, kind)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect()
}

/// Whether any of `messages` names `rule`.
fn names(messages: &[String], rule: &str) -> bool {
  messages.iter().any(|message| message.contains(rule))
}

/// The word's own reading — a path, kept as a path.
#[test]
fn the_word_is_a_quoted_path() {
  let model = entity(&syn::parse_quote! {
    #[ingraph(receipt = "media::SceneId")]
    struct SceneOpen {
      #[ingraph(id)]
      id: u64,
    }
  })
  .expect("one word, one type");
  let receipt = model.receipt.expect("the declaration named one");
  assert_eq!(quote::quote!(#receipt).to_string(), "media :: SceneId");
}

/// **ABSENT IS NOTHING AT ALL**, which is where this word parts from `options`: no path in the
/// model, and — asserted in the emitter's own suite — no impl in the expansion. Zero ceremony for
/// the node that does not receipt is the whole reason the seat is a sibling.
#[test]
fn a_declaration_with_no_word_names_no_type() {
  let model = entity(&syn::parse_quote! {
    struct Keep {
      #[ingraph(id)]
      id: u64,
    }
  })
  .expect("a node that receipts nothing is a node");
  assert!(model.receipt.is_none());
}

/// One message is answered with one type: the said-it-twice refusal, this word's own.
#[test]
fn the_word_is_written_once() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(receipt = "a::One", receipt = "b::Other")]
    struct SceneOpen {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&refused, "one message is answered with one type"),
    "{refused:?}"
  );
}

/// A value that is not a path is refused HERE, where the author can still edit it, rather than
/// pasted into an expansion to fail somewhere downstream — `options`' own rule, one word over.
#[test]
fn a_value_that_is_not_a_path_is_refused_at_the_word() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(receipt = "not a path!")]
    struct SceneOpen {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(
      &refused,
      "must be a path to the type a consumer answers this message with"
    ),
    "{refused:?}"
  );
}

/// A BARE word is refused too: the type is what the word carries, so there is nothing for a bare
/// spelling to mean.
#[test]
fn the_word_carries_its_type() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(receipt)]
    struct SceneOpen {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(
      &refused,
      "must be a path to the type a consumer answers this message with"
    ),
    "{refused:?}"
  );
}

/// The word is in the NEAR-MISS vocabulary, so a typo of it is sent to the word rather than left to
/// the reader — the `unknown` door's own law, which asks every position to decide what a typo of its
/// words looks like.
#[test]
fn a_typo_of_the_word_is_named() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(reciept = "media::SceneId")]
    struct SceneOpen {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(names(&refused, "did you mean `receipt`"), "{refused:?}");
}

/// **THE WORD IS THE MESSAGE'S**, and a declaration that mints none is refused by name.
///
/// The re-key's grammar half. The seat shipped keyed on the CONSUMER, so an
/// `#[ingraph::operator(receipt = "…")]` is exactly what an author who read the old book writes —
/// which is why the refusal has to say where the word WENT rather than only that it is unknown here.
///
/// Two things are asserted about the sentence beyond the fact of the refusal: that it names the
/// rungs that DO take the word, so the fix is in the message rather than in a search; and that it
/// names the hand road beside them, so a seat that must answer with something its message did not
/// declare is not left thinking the framework forbids it.
#[test]
fn a_declaration_that_mints_no_message_is_refused_by_name() {
  for kind in [
    NodeKind::Operator,
    NodeKind::Fulltext,
    NodeKind::Vector,
    NodeKind::Blob,
  ] {
    let refused = refusals_at(
      kind,
      syn::parse_quote! {
        #[ingraph(receipt = "media::SceneId")]
        struct Scene {
          #[ingraph(id)]
          id: u64,
        }
      },
    );
    assert!(
      names(&refused, "mints no MESSAGE"),
      "{kind:?} took a word it mints no subject for: {refused:?}"
    );
    assert!(
      names(
        &refused,
        "an `entity` (the message node), a `record`, or an `aggregate`"
      ),
      "{kind:?}'s refusal does not say where the word went: {refused:?}"
    );
    assert!(
      names(&refused, "`Emitter::receipt`"),
      "{kind:?}'s refusal closes the hand road it must leave open: {refused:?}"
    );
  }
}

/// **AND THE THREE THAT MINT ONE TAKE IT**, which is the other half of the same gate.
///
/// Asserted over the roster rather than at one kind, so a kind added to
/// [`NodeKind::mints_a_message`] without a home here is a test that fails rather than a silence.
#[test]
fn every_message_rung_takes_the_word() {
  for kind in [NodeKind::Entity, NodeKind::Record, NodeKind::Aggregate] {
    assert!(
      kind.mints_a_message(),
      "the roster and the predicate disagree about {kind:?}"
    );
  }
}