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 VOCABULARY LAW AT THE ELECTION BASE** — `rename` names ONE thing verbatim, `prefix`
//! re-stems a FAMILY, and the skin's base is pure stem.
//!
//! ```text
//! graphql(async_graphql(
//!   prefix = "clips",              a STEM  — mints `ClipsFuseOptions` and its siblings
//!   options(rename = "ClipKnobs"), a NAME  — one type, verbatim
//! ))
//! ```
//!
//! Beside [`skins`](super::skins) rather than inside it, on that file's own precedent: that one is
//! about WHICH BACKEND dresses a declaration, and this is about the WORD its base is written under.
//!
//! # The measurement that decided the word
//!
//! `prefix = "clips"` publishes `ClipsFuseOptions`, `ClipsFuseQuestion`, `ClipsEngineInput` and
//! their siblings, and NOTHING in the emission is called `clips`. A seat whose value is never a name
//! anywhere is not a rename, and calling it one taught every author the wrong rule about the four
//! seats beside it — which do name one type each and are carried verbatim.
//!
//! # What is asserted here and what is asserted elsewhere
//!
//! Here: the GRAMMAR and the SENTENCE. What the word does to an EMISSION is `ingraph-codegen`'s
//! `fuse/skins.rs`, and the rendered caret is `tests/refusals/`.

use ingraph_codegen::{model::GraphqlKind, prelude::NodeKind};

use super::skins::dressed;

/// Every refusal a head block earns, joined.
fn refusals(inside: proc_macro2::TokenStream) -> String {
  dressed(inside)
    .expect_err("the block is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// **THE BASE IS WRITTEN `prefix`, AND IT REACHES THE SEAT.**
///
/// The EXCLUSION is what makes this more than *the block parsed*: a base that also filled a face
/// would re-stem the family and name one of its members at once, which is the confusion the two
/// words exist to keep apart.
#[test]
fn the_base_is_written_prefix_and_fills_the_base_seat_alone() {
  let model = dressed(quote::quote! { prefix = "clips" }).expect("`prefix` is the base's word");
  let skin = model
    .graphql
    .as_ref()
    .expect("the declaration named a backend")
    .skin(GraphqlKind::AsyncGraphql)
    .expect("that backend's block");
  assert_eq!(
    skin.prefix.as_ref().map(syn::LitStr::value).as_deref(),
    Some("clips"),
  );
  assert!(skin.options.is_none(), "the stem filled a face's seat");
  assert!(skin.question.is_none(), "the stem filled a face's seat");
  assert!(skin.engine.is_none(), "the stem filled a face's seat");
}

/// **THE OLD WORD IS A REFUSAL AND NOT A SILENCE.**
///
/// `rename = "…"` parsed at this position until the correction, so the sentence has to be reachable:
/// a grammar that still took it would re-stem the family and this test would be the only thing that
/// noticed.
#[test]
fn the_old_word_at_the_base_is_refused_with_the_law_and_the_successor() {
  let refused = refusals(quote::quote! { rename = "clips" });
  assert!(
    refused.contains("names ONE thing verbatim and this position re-stems a FAMILY"),
    "the sentence carries the law rather than the substitution alone: {refused}",
  );
  assert!(
    refused.contains("no emitted type is called `clips` at all"),
    "and the MEASUREMENT the law rests on: {refused}",
  );
  assert!(
    refused.contains("did you mean `prefix`?"),
    "and the near-miss family's own clause, which the edit distance could not produce: {refused}",
  );
}

/// **THE FACES KEEP `rename`**, which is the other half of one law: each names a real type outright.
///
/// Asserted in the same file as the refusal above, because either half alone would pass for a
/// grammar that had renamed the word at every seat rather than at the one that is a stem.
#[test]
fn a_face_still_names_its_one_type_with_a_rename_row() {
  let model = dressed(quote::quote! {
    prefix = "clips",
    options(rename = "ClipKnobs"),
  })
  .expect("the stem and a face's name are one legal block");
  let skin = model
    .graphql
    .as_ref()
    .expect("the declaration named a backend")
    .skin(GraphqlKind::AsyncGraphql)
    .expect("that backend's block");
  assert_eq!(
    skin.prefix.as_ref().map(syn::LitStr::value).as_deref(),
    Some("clips"),
  );
  assert_eq!(
    skin.options.as_ref().map(syn::LitStr::value).as_deref(),
    Some("ClipKnobs"),
  );

  // …and `prefix` INSIDE a face is not the face's word: a face names one type, so there is no
  // family under it for a stem to re-base.
  let refused = refusals(quote::quote! { options(prefix = "ClipKnobs") });
  assert!(
    refused.contains("it takes `rename = \"…\"` and nothing else"),
    "{refused}",
  );
}

/// **THE STEM IS STILL HELD TO GRAPHQL'S OWN NAME GRAMMAR** — the correction moved the word and not
/// the fence.
#[test]
fn the_stem_is_held_to_the_wire_alphabet_under_its_new_word() {
  let refused = refusals(quote::quote! { prefix = "9lives" });
  assert!(refused.contains("is not a GraphQL name"), "{refused}");
}

/// **ONE STEM, ONE VALUE** — the repeat every seat in this grammar refuses, under the new word.
#[test]
fn the_stem_is_written_once() {
  let refused = refusals(quote::quote! { prefix = "a", prefix = "b" });
  assert!(
    refused.contains("`prefix` is declared more than once"),
    "{refused}",
  );
}

/// **THE UNKNOWN-ENTRY SENTENCE OFFERS THE NEW WORD**, so a near miss of the stem lands on the stem
/// rather than on a word this position no longer takes.
#[test]
fn the_vocabulary_the_position_offers_is_the_corrected_one() {
  let refused = refusals(quote::quote! { prefiix = "clips" });
  assert!(refused.contains("unknown entry"), "{refused}");
  assert!(
    refused.contains("did you mean `prefix`?"),
    "the near-miss engine measures against the corrected vocabulary: {refused}",
  );
  assert!(
    refused.contains("`prefix = \"…\"` re-stems the whole SDL family"),
    "and the teaching half says what the word does: {refused}",
  );
}

/// **THE BONES BASE IS UNTOUCHED**, which is the ruling's own exclusion: the top-level `rename` sits
/// under a documented value-XOR-block law, and this correction is a word at the SKIN and nowhere
/// else.
#[test]
fn the_top_level_rename_keeps_its_word_and_both_of_its_forms() {
  for form in [
    quote::quote! { rename = "ShotSearch" },
    quote::quote! { rename(fulltext_options = "MyKnobs") },
  ] {
    let (host, arguments) = crate::host::split(&syn::parse_quote! {
      #[ingraph(upstream(node = "a::A", body = "text"), dedup(hash), #form)]
      struct D { #[ingraph(searchable)] body: String }
    });
    super::super::index::parse(&host, &arguments, NodeKind::Fulltext)
      .unwrap_or_else(|e| panic!("the bones axis keeps `rename`: {e}"));
  }
}