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 `options` WORD's grammar, read straight off the pass.
//!
//! A suite of its own beside [`crate::parse::tests`] for the reason
//! [`crate::parse::channel_tests`] gives: 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 not a gap: a
//! node with no knobs is configured by `()`, and the emission says so.

use ingraph_codegen::prelude::*;

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

/// Every refusal one declaration collects, as text.
fn refusals(input: syn::DeriveInput) -> Vec<String> {
  operator(&input)
    .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 = operator(&syn::parse_quote! {
    #[ingraph(options = "decode::Options")]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  })
  .expect("one word, one type");
  let options = model.options.expect("the declaration named one");
  assert_eq!(quote::quote!(#options).to_string(), "decode :: Options");
}

/// **ABSENT IS `()`, and it is a statement**: the model carries no path, and the emission spells the
/// unit type — which is what a zero-knob node's constructor already says.
#[test]
fn a_declaration_with_no_word_names_no_type() {
  let model = operator(&syn::parse_quote! {
    struct Keep {
      #[ingraph(id)]
      id: u64,
    }
  })
  .expect("a node with no knobs is a node");
  assert!(model.options.is_none());
}

/// One node is configured by 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(options = "a::One", options = "b::Other")]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&refused, "one node is configured by 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 — `crate`'s 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(options = "not a path!")]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&refused, "must be a path to this node's options type"),
    "{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(options)]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&refused, "must be a path to this node's options type"),
    "{refused:?}"
  );
}