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
//! **B-27 D-6 — the NAMING TOWER's grammar on an interface**: the three spellings it takes, and the
//! ones it refuses.
//!
//! Beside [`super::tests`] rather than inside it, which is this crate's shape for a word that
//! arrives after a grammar has been proven.

use super::tests::{parse, refusal};

/// **THE THREE TIERS PARSE**, and each lands in its own cell — one word for the plural, one block
/// per page name, exactly as a record's.
#[test]
fn the_tower_takes_its_three_words() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(
      persist(sqlite),
      graphql(
        plural = "corpora",
        connection(rename = "CorpusPage"),
        edge(rename = "CorpusEdge"),
      ),
      fields(slate: String),
    )]
    pub enum Media {
      Reel(Reel),
      Tape(Tape),
    }
  })
  .expect("the tower's three words are a declaration");

  assert_eq!(model.plural.as_deref(), Some("corpora"));
  assert_eq!(model.connection.as_deref(), Some("CorpusPage"));
  assert_eq!(model.edge.as_deref(), Some("CorpusEdge"));
}

/// **AND ALL THREE ARE OPTIONAL** — a declaration that writes none is the tower's BASE, which is the
/// pluraliser's answer over the interface's own name.
#[test]
fn a_declaration_that_writes_none_is_the_base() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(persist(sqlite), fields(slate: String))]
    pub enum Media {
      Reel(Reel),
      Tape(Tape),
    }
  })
  .expect("a declaration that writes none");

  assert!(model.plural.is_none());
  assert!(model.connection.is_none());
  assert!(model.edge.is_none());
  assert_eq!(
    model.published_plural(),
    "Media",
    "the worked example: the pluraliser's answer EQUALS the singular, and an interface has no by-id \
     field for it to collide with",
  );
}

/// **THE PLURAL IS HELD TO THE WIRE FLOOR**, at the literal's own span — a word that reaches a
/// schema and nothing downstream would refuse.
#[test]
fn a_plural_that_is_not_a_graphql_name_is_refused() {
  let refusal = refusal(&syn::parse_quote! {
    #[ingraph(persist(sqlite), graphql(plural = "9lives"), fields(slate: String))]
    pub enum Media {
      Reel(Reel),
      Tape(Tape),
    }
  });

  assert!(refusal.contains("not a GraphQL name"), "{refusal}");
}

/// **A PAGE NAME IS STRING-KIND**, so GraphQL's own keywords are ordinary words here — the #250
/// classification, applied to the second front end that has a page.
#[test]
fn a_page_name_may_be_a_rust_keyword() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(
      persist(sqlite),
      graphql(connection(rename = "type")),
      fields(slate: String),
    )]
    pub enum Media {
      Reel(Reel),
      Tape(Tape),
    }
  })
  .expect("a schema word is not a Rust one");

  assert_eq!(model.connection.as_deref(), Some("type"));
}

/// **EACH WORD IS WRITTEN ONCE**, for the reason every set-once entry in this grammar is.
#[test]
fn the_plural_is_declared_once() {
  let refusal = refusal(&syn::parse_quote! {
    #[ingraph(
      persist(sqlite),
      graphql(plural = "corpora", plural = "corpuses"),
      fields(slate: String),
    )]
    pub enum Media {
      Reel(Reel),
      Tape(Tape),
    }
  });

  assert!(refusal.contains("more than once"), "{refusal}");
}

/// The UNKNOWN sentence names the three new words, so a reader who misspells one is told the
/// spelling that exists.
#[test]
fn the_unknown_refusal_names_the_tower() {
  let refusal = refusal(&syn::parse_quote! {
    #[ingraph(persist(sqlite), graphql(plurals = "corpora"), fields(slate: String))]
    pub enum Media {
      Reel(Reel),
      Tape(Tape),
    }
  });

  assert!(refusal.contains("plural"), "{refusal}");
  assert!(refusal.contains("connection"), "{refusal}");
  assert!(refusal.contains("edge"), "{refusal}");
}