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 `required` SIGNATURE AT AN ARTIFACT POSITION** — the word the address takes now, read as
//! the grammar reads it and refused as the grammar refuses it.
//!
//! The word was REFUSED here until this round, on a premise the ruling corrected rather than obeyed:
//! the design took the address to be non-null already, so the word would be a spelled-out no-op. It
//! is not. The address is nullable BY ARCHITECTURE — an artifact is written by the indexing pipeline
//! asynchronously after the entity exists, so *not yet generated* is a normal state — and the
//! emission's `Option` was telling the truth about it. So the word opens with the meaning it has one
//! field over: the AUTHOR'S INFORMED SIGNATURE, promoting the published field to non-null.
//!
//! # Why a suite of its own, beside [`crate::locate_full_form_tests`]
//!
//! That file holds every claim about the full form as the round that MINTED the form left it, and
//! one of its tests was the refusal this round retires. Holding the flip apart is what makes the
//! reversal legible: the retired golden is gone from next door, and everything the word means now is
//! here, in one file a reader can read start to finish.
//!
//! # What is measured here and what is not
//!
//! GRAMMAR only — that the word reaches the model, that it is per-position, and that its two
//! refusals are the ones its sibling raises. What the word DOES to a published schema is
//! `ingraph/tests/artifact_address_signature.rs`, where the SDL itself is read: a `bool` in a model
//! is not a `!` in a schema, and only one of those is what an author signed for.

use ingraph_codegen::prelude::*;

use crate::host;

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

/// What each position SIGNED FOR: whether its address field is non-null.
fn signatures(model: &RecordModel) -> Vec<bool> {
  model
    .artifacts
    .iter()
    .map(|artifact| artifact.required)
    .collect()
}

/// EVERY refusal a declaration raised, as sentences.
fn refusals(input: &syn::DeriveInput) -> Vec<String> {
  record(input)
    .expect_err("this declaration is refused")
    .into_iter()
    .map(|error| error.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 REACHES THE MODEL**, off the full form and beside the family it signs for.
///
/// THE EXCLUSION, and it is the one that matters here: the `data` ask is untouched. The two words
/// are one spelling at two grains, so the failure mode a model assertion about `required` alone
/// cannot see is one of them being read as the other.
#[test]
fn the_position_carries_its_own_signature() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite,
      lineage(fields(media)),
      graphql(async_graphql),
      locate(cover(family = "app::Cover", required)),
    )]
    struct Frame { #[ingraph(id)] at: i64, media: String }
  })
  .expect("the ruling's own declaration");

  assert_eq!(signatures(&model), [true]);
  assert!(
    model.artifacts[0].data.is_none(),
    "the position signed for its ADDRESS and asked for no bytes"
  );
}

/// **BARE IS THE DEFAULT AND THE DEFAULT IS NULLABLE** — in both spellings of a position.
///
/// The truth clause of the ruling, pinned at the grain a grammar can pin it: nothing this framework
/// does on its own puts a signature on an address, so an author reading `BlobLocation` in a schema
/// wrote nothing to get it.
#[test]
fn a_position_that_signs_nothing_is_nullable() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite,
      lineage(fields(media)),
      graphql(async_graphql),
      locate(card = "app::Card", poster(family = "app::Poster", data)),
    )]
    struct Frame { #[ingraph(id)] at: i64, media: String }
  })
  .expect("the shorthand and a full form that signs nothing");

  assert_eq!(signatures(&model), [false, false]);
}

/// **THE SIGNATURE BELONGS TO THE POSITION THAT WROTE IT.** One block, three positions, one word —
/// so a reader can see that the promotion is per-position rather than per-block.
#[test]
fn the_signature_belongs_to_the_position_that_wrote_it() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite,
      lineage(fields(media)),
      graphql(async_graphql),
      locate(
        card = "app::Card",
        cover(family = "app::Cover", required),
        poster(family = "app::Poster", data),
      ),
    )]
    struct Frame { #[ingraph(id)] at: i64, media: String }
  })
  .expect("three positions, one signature");

  assert_eq!(signatures(&model), [false, true, false]);
}

/// **THE TWO WORDS ARE INDEPENDENT**, which is the whole of what *two fields, two signatures* means:
/// a position may sign for the address, for the bytes, for both, or for neither.
#[test]
fn the_address_and_the_bytes_are_signed_for_separately() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite,
      lineage(fields(media)),
      graphql(async_graphql),
      locate(
        neither(family = "app::Neither", data),
        address(family = "app::Address", required, data),
        bytes(family = "app::Bytes", data(required)),
        both(family = "app::Both", required, data(required)),
      ),
    )]
    struct Frame { #[ingraph(id)] at: i64, media: String }
  })
  .expect("the four combinations of two independent words");

  assert_eq!(signatures(&model), [false, true, false, true]);
  assert_eq!(
    model
      .artifacts
      .iter()
      .map(|artifact| artifact
        .data
        .as_ref()
        .expect("every position here asked")
        .required())
      .collect::<Vec<_>>(),
    [false, false, true, true],
    "the ask's own signature follows the ask's own word"
  );
}

/// **THE WORD IS BARE AT THE POSITION TOO**, and the sentence is the ask's own — one word, one
/// refusal, because two voices for one mistake is how a grammar comes to mean two things by one
/// word.
#[test]
fn refuses_a_signature_that_carries_anything() {
  for written in [
    quote::quote!(locate(cover(family = "app::Cover", required = true))),
    quote::quote!(locate(cover(family = "app::Cover", required("yes")))),
  ] {
    let refused = refusals(&syn::parse_quote! {
      #[ingraph(#written)]
      struct Frame { #[ingraph(id)] at: i64 }
    });
    assert!(
      names(&refused, "`required` is a BARE word")
        && names(&refused, "there is no inverse word either"),
      "{written}: {refused:?}"
    );
  }
}

/// **ONE SIGNATURE PER POSITION**, for the reason PRESENCE always earns: the second one says exactly
/// what the first already said.
#[test]
fn refuses_a_repeated_signature() {
  let refused = refusals(&syn::parse_quote! {
    #[ingraph(locate(cover(family = "app::Cover", required, required)))]
    struct Frame { #[ingraph(id)] at: i64 }
  });
  assert!(
    names(
      &refused,
      "`required` is declared more than once in an artifact position"
    ),
    "{refused:?}"
  );
}

/// **THE NEAR-MISS LINE KNOWS THE NEW WORD**, which is this grammar's own rule read from the far
/// end: a word added to a position without being added to the vocabulary a typo is measured against
/// is a word an author cannot be helped towards.
#[test]
fn a_typo_of_the_signature_is_suggested() {
  let refused = refusals(&syn::parse_quote! {
    #[ingraph(locate(cover(family = "app::Cover", requird)))]
    struct Frame { #[ingraph(id)] at: i64 }
  });
  assert!(
    names(&refused, "unknown entry in an artifact position")
      && names(&refused, "did you mean `required`"),
    "{refused:?}"
  );
  assert!(
    names(&refused, "There is no word for the nullable form"),
    "and the one-way law is in the sentence: {refused:?}"
  );
}

/// **THE SHORTHAND HAS NOWHERE TO WRITE IT**, and that is the form rather than a rule: a position
/// spelled `<name> = "path::to::Family"` carries a value and no options, so an author who wants the
/// signature writes the full form — which is what the shape sentence already says.
#[test]
fn the_shorthand_carries_no_words_at_all() {
  let refused = refusals(&syn::parse_quote! {
    #[ingraph(locate(cover = "app::Cover", required))]
    struct Frame { #[ingraph(id)] at: i64 }
  });
  assert!(
    names(
      &refused,
      "an artifact position is `<name> = \"path::to::Family\"`"
    ),
    "a bare word in the BLOCK is an entry with no family, not a signature: {refused:?}"
  );
}