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 two rules this layer owns over a NON-`ingraph` field attribute, and the seat they fill.
//!
//! What the CONDITION does once it is filled is the emission's to prove, and it is proven where it
//! can only be proven: by a program that compiles the same declaration twice, once with the feature
//! and once without. What is here is the READING — which attribute is taken, which is refused, and
//! which is left for somebody else's macro.

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

/// The pass under test, over a declaration written the way the derive took one.
fn parse(input: &syn::DeriveInput, kind: NodeKind) -> syn::Result<RecordModel> {
  let (host, arguments) = crate::host::split(input);
  crate::parse::parse(&host, &arguments, kind)
}

/// Every refusal the pass reported, JOINED — one compile reports every rule it can still see, and a
/// `syn::Error` renders only the first of a combined one.
fn refusals(input: &syn::DeriveInput, kind: NodeKind) -> String {
  parse(input, kind)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// A `#[cfg]` on a DATA field reaches the model, which is the whole of what the seat is.
#[test]
fn a_data_fields_condition_takes_its_seat() {
  let model = parse(
    &syn::parse_quote! {
      pub struct Frame {
        #[ingraph(id, copy)]
        id: u64,
        #[cfg(feature = "x")]
        #[ingraph(copy)]
        width: u32,
        #[ingraph(copy)]
        height: u32,
      }
    },
    NodeKind::Entity,
  )
  .expect("a condition on a data field is served");
  assert!(
    model.conditional(),
    "the declaration knows it has a condition"
  );
  let width = syn::parse_quote!(width);
  let height = syn::parse_quote!(height);
  assert!(
    !model.gate(&width).is_empty(),
    "the conditional field carries its gate"
  );
  assert!(
    model.gate(&height).is_empty(),
    "an unconditional field costs not one token"
  );
}

/// SEVERAL `#[cfg]` on one field are Rust's own conjunction, and the presence term says so — the
/// `all(…)` is what keeps two conditions from becoming two expansions.
#[test]
fn two_conditions_are_one_conjunction() {
  let model = parse(
    &syn::parse_quote! {
      pub struct Frame {
        #[ingraph(id, copy)]
        id: u64,
        #[cfg(feature = "x")]
        #[cfg(unix)]
        #[ingraph(copy)]
        width: u32,
      }
    },
    NodeKind::Entity,
  )
  .expect("two conditions on one field are served");
  let width = syn::parse_quote!(width);
  let present = model.present(&width).to_string();
  assert!(
    present.contains("all") && present.contains("not"),
    "the presence term is the conjunction and its negation: {present}"
  );
}

/// A field with NO condition expands to exactly what it always did: no gate, and the constant `1`
/// wherever a presence term is summed.
#[test]
fn an_unconditional_field_pays_nothing() {
  let model = parse(
    &syn::parse_quote! {
      pub struct Frame {
        #[ingraph(id, copy)]
        id: u64,
        #[ingraph(copy)]
        width: u32,
      }
    },
    NodeKind::Entity,
  )
  .expect("a plain declaration is unchanged");
  assert!(!model.conditional());
  let width = syn::parse_quote!(width);
  assert!(model.gate(&width).is_empty());
  assert_eq!(model.present(&width).to_string(), "1usize");
}

/// IDENTITY IS NOT CONDITIONAL — the id and each lineage step name the row, and a build without one
/// leaves a record with no address rather than a smaller record.
#[test]
fn a_conditional_identity_is_refused_by_name() {
  let refused = refusals(
    &syn::parse_quote! {
      pub struct Frame {
        #[cfg(feature = "x")]
        #[ingraph(id, copy)]
        id: u64,
        #[ingraph(copy)]
        width: u32,
      }
    },
    NodeKind::Entity,
  );
  assert!(
    refused.contains("identity is not conditional"),
    "the refusal names the rule: {refused}"
  );
}

/// A `cfg_attr` carrying THIS grammar's own word is refused: the macro cannot evaluate the
/// predicate, so it cannot know whether the word was written — and the walk would drop it in
/// silence.
#[test]
fn a_conditional_ingraph_word_is_refused_by_name() {
  let refused = refusals(
    &syn::parse_quote! {
      pub struct Frame {
        #[ingraph(id, copy)]
        id: u64,
        #[cfg_attr(feature = "x", ingraph(copy))]
        width: u32,
      }
    },
    NodeKind::Entity,
  );
  assert!(
    refused.contains("cannot carry an `ingraph(…)` word"),
    "the refusal names what it cannot decide: {refused}"
  );
}

/// A `cfg_attr` carrying ANOTHER crate's word is somebody else's business, and passes through.
#[test]
fn a_conditional_foreign_word_passes_through() {
  parse(
    &syn::parse_quote! {
      pub struct Frame {
        #[ingraph(id, copy)]
        id: u64,
        #[cfg_attr(feature = "x", serde(rename = "w"))]
        #[ingraph(copy)]
        width: u32,
      }
    },
    NodeKind::Entity,
  )
  .expect("an attribute this grammar does not own is not this grammar's to refuse");
}