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 PLURAL WORD**, parsed and refused — the entry a declaration corrects its own English with.
//!
//! A suite of its own beside [`crate::tests`], for the reason the sortable word's is: what is
//! asserted here is ONE entry — how it is read, what it reaches in the model, and every way it can
//! be written wrongly — plus the one rule that cannot be asked of a declaration at all, because it
//! is about the name the framework DERIVED.
//!
//! # Where each half is proved
//!
//! | half | here |
//! |---|---|
//! | the word reaches the model | [`a_declared_plural_reaches_the_model_verbatim`] |
//! | it is a written word, once, and a GraphQL name | the three refusals below |
//! | a derived plural equal to the singular is refused | [`a_derived_plural_equal_to_the_singular_is_refused`] |
//! | a DECLARED one equal to it is not | [`a_declared_plural_equal_to_the_singular_is_allowed`] |
//! | a record with no page is asked for no word | [`a_record_with_no_page_is_asked_for_no_plural`] |
//!
//! What the refusal READS LIKE through a real compile is `tests/refusals`, and what the emitted
//! field is CALLED is `ingraph/tests/wire_plural.rs`, over a schema. This file is the parser's half.

use ingraph_codegen::prelude::*;

use crate::host;

/// The pass under test — [`crate::tests`]'s own, spelled here so this suite stands alone.
fn parse(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
  let (host, options) = host::split(input);
  crate::parse::parse(&host, &options, NodeKind::Record)
}

/// Every refusal a declaration raised, as sentences.
fn refusals(input: &syn::DeriveInput) -> Vec<String> {
  parse(input)
    .expect_err("this declaration is refused")
    .into_iter()
    .map(|error| error.to_string())
    .collect()
}

/// **THE WORD REACHES THE MODEL, VERBATIM** — a plural is a fact about the entity rather than a
/// field name, so what is stored is what was written, and no casing act touches it on the way.
#[test]
fn a_declared_plural_reaches_the_model_verbatim() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(table = "corpora", sqlite, graphql(async_graphql, plural = "corpora"))]
    struct Corpus {
      #[ingraph(id, copy)] id: i64,
      #[ingraph(string)] name: String,
    }
  })
  .expect("a declared plural is a declaration this grammar takes");
  let graphql = model.graphql.expect("the record declares a wire face");

  assert_eq!(graphql.plural.as_deref(), Some("corpora"));
}

/// A record that wrote no word carries none — the model holds the DECLARATION, and the emitter is
/// where a missing one becomes English.
#[test]
fn a_record_that_wrote_no_word_carries_none() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(table = "reels", sqlite, graphql(async_graphql))]
    struct Reel {
      #[ingraph(id, copy)] id: i64,
      #[ingraph(string)] title: String,
    }
  })
  .expect("the ordinary declaration");

  assert_eq!(
    model
      .graphql
      .expect("the record declares a wire face")
      .plural,
    None
  );
}

/// One entry, one value — the repeated-option rule this grammar applies to every word, reaching the
/// newest one.
#[test]
fn a_plural_is_declared_once() {
  let messages = refusals(&syn::parse_quote! {
    #[ingraph(
      table = "corpora", sqlite,
      graphql(async_graphql, plural = "corpora", plural = "corpuses")
    )]
    struct Corpus {
      #[ingraph(id, copy)] id: i64,
    }
  });

  assert!(
    messages
      .iter()
      .any(|message| message.contains("`plural` is declared more than once in `graphql(…)`")),
    "{messages:?}"
  );
}

/// **A WORD IN A STRING**, and not an ident: a plural names something in a SCHEMA, so it goes to the
/// wire's floor rather than to Rust's — and the refusal spells the entry it is teaching.
#[test]
fn a_plural_is_a_written_word() {
  let messages = refusals(&syn::parse_quote! {
    #[ingraph(table = "corpora", sqlite, graphql(async_graphql, plural = corpora))]
    struct Corpus {
      #[ingraph(id, copy)] id: i64,
    }
  });

  assert!(
    messages
      .iter()
      .any(|message| message.contains("`plural` is the entity's plural as a WORD")),
    "{messages:?}"
  );
}

/// **THE WIRE FLOOR APPLIES TO IT** — a plural becomes a field name, so it is held to GraphQL's own
/// Name grammar by the one authority that holds every other wire name to it. The empty string is
/// the same refusal for the same reason, and needs no rule of its own.
#[test]
fn a_plural_that_is_not_a_graphql_name_is_refused() {
  for word in ["", "9lives", "reel list"] {
    let attribute: syn::Attribute = syn::parse_quote! {
      #[ingraph(table = "reels", sqlite, graphql(async_graphql, plural = #word))]
    };
    let messages = refusals(&syn::parse_quote! {
      #attribute
      struct Reel {
        #[ingraph(id, copy)] id: i64,
      }
    });
    assert!(
      messages
        .iter()
        .any(|message| message.contains("is not a GraphQL name")),
      "{word:?}: {messages:?}"
    );
  }
}

/// The entry joins the block's own vocabulary, so an unknown one names it among what may be written.
#[test]
fn the_unknown_entry_refusal_names_the_plural() {
  let messages = refusals(&syn::parse_quote! {
    #[ingraph(table = "reels", sqlite, graphql(async_graphql, plurals = "reels"))]
    struct Reel {
      #[ingraph(id, copy)] id: i64,
    }
  });

  assert!(
    messages
      .iter()
      .any(|message| message.contains("unknown entry in `graphql(…)`")
        && message.contains("`plural = \"…\"`")),
    "{messages:?}"
  );
}

/// **THE SILENCE IS REFUSED** — `Media` pluralises to itself, so the page would take the single's own
/// name, and nothing downstream would say a word about it.
#[test]
fn a_derived_plural_equal_to_the_singular_is_refused() {
  let messages = refusals(&syn::parse_quote! {
    #[ingraph(table = "media", sqlite, graphql(async_graphql))]
    struct Media {
      #[ingraph(id, copy)] id: i64,
      #[ingraph(string)] label: String,
    }
  });

  assert_eq!(messages.len(), 1, "one silence, one refusal: {messages:?}");
  assert!(
    messages[0].contains("`Media`'s page would be published as `media`"),
    "the refusal quotes the name a client would have read: {messages:?}"
  );
  assert!(
    messages[0].contains("`graphql(plural = \"…\")`"),
    "…and teaches the entry that answers it: {messages:?}"
  );
}

/// **THE RULE GUARDS SILENCE, NOT CHOICE** — the same word, WRITTEN, is a declaration and is taken.
#[test]
fn a_declared_plural_equal_to_the_singular_is_allowed() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(table = "media", sqlite, graphql(async_graphql, plural = "media"))]
    struct Media {
      #[ingraph(id, copy)] id: i64,
      #[ingraph(string)] label: String,
    }
  })
  .expect("a written word is a choice, and a choice is not refused");

  assert_eq!(
    model
      .graphql
      .expect("the record declares a wire face")
      .plural
      .as_deref(),
    Some("media")
  );
}

/// **THE WORD IS ASKED FOR WHERE IT IS SPENT** — this `Media` has no stored id, so it answers no
/// page, so there is no name for a plural to collide with and none is demanded.
///
/// The gate is the LIST LEG's own condition rather than the presence of a wire face. What it leaves
/// open is a later reader of the same fact — a connection, an aggregate — which will ask this
/// question again under its own condition when it lands.
#[test]
fn a_record_with_no_page_is_asked_for_no_plural() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(table = "media", sqlite, graphql(async_graphql, plural = "many"))]
    struct Media {
      #[ingraph(id)] id: u128,
      #[ingraph(string)] label: String,
    }
  })
  .expect("a record with no page owes no plural");

  assert!(
    model.degenerate_plural().is_none(),
    "no page, nothing to refuse"
  );
}