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
//! `graphql(connection(rename = "…"), edge(rename = "…"))` — the two entries, parsed, refused four
//! ways, and reaching the model.
//!
//! The 2026-08-09 ruling asked for *the alias-knob family's sibling … mirroring the block's existing
//! rename entries*, so the entries write the same BLOCK the four type renames do, through the same
//! reader one layer down — [`attribute::named_block`](crate::attribute) — with only the word's KIND
//! passed in. What this file holds is that the sharing is real: every refusal the block owns fires
//! for these two words, in the block's own sentences, without either entry having written one.
//!
//! The KIND is `page_kind_tests.rs`'s subject, beside this file: these words name schema types and
//! not Rust ones, so they are strings and a Rust keyword is an ordinary word to them.

use ingraph_codegen::prelude::*;

use crate::host;

/// A record with a `graphql(…)` block spelled however a case wants it.
///
/// The declaration is the shape `tests.rs`'s own suites use — the derive-attribute form, which is
/// what [`host::split`](crate::host::split) takes — so a case here reads beside them.
fn model(block: &str) -> Result<RecordModel, Vec<String>> {
  let input: syn::DeriveInput = syn::parse_str(&format!(
    "#[ingraph(sqlite, graphql({block}))]
     struct Reel {{
       #[ingraph(id)] id: i64,
       #[ingraph(string)] title: String,
     }}"
  ))
  .expect("a parseable declaration");
  let (host, options) = host::split(&input);
  crate::parse::parse(&host, &options, NodeKind::Record)
    .map_err(|e| e.into_iter().map(|e| e.to_string()).collect())
}

/// The two page words of a settled model.
fn words(block: &str) -> (Option<String>, Option<String>) {
  let model = model(block).expect("the declaration settles");
  let graphql = model.graphql.expect("the wire face");
  (graphql.connection, graphql.edge)
}

/// Every refusal a declaration earns, as one string to look inside.
fn refusal(block: &str) -> String {
  model(block)
    .expect_err("the declaration is refused")
    .join(" | ")
}

/// **BOTH WORDS REACH THE MODEL**, verbatim and separately.
#[test]
fn the_two_entries_reach_the_model_as_written() {
  assert_eq!(
    words(r#"async_graphql, connection(rename = "SpliceReel"), edge(rename = "SpliceReelItem")"#),
    (
      Some("SpliceReel".to_owned()),
      Some("SpliceReelItem".to_owned())
    )
  );
}

/// **EITHER ALONE IS LEGAL** — the hooks are independent in the library, so they are independent in
/// the grammar.
#[test]
fn either_word_may_be_written_without_the_other() {
  assert_eq!(
    words(r#"async_graphql, connection(rename = "Page")"#),
    (Some("Page".to_owned()), None)
  );
  assert_eq!(
    words(r#"async_graphql, edge(rename = "Item")"#),
    (None, Some("Item".to_owned()))
  );
}

/// **AN UNTOUCHED DECLARATION CARRIES NEITHER**, which is what the emission reads as *take the
/// specification's own words*.
#[test]
fn a_declaration_that_writes_neither_carries_neither() {
  assert_eq!(words("async_graphql"), (None, None));
}

/// **THE WORD IS NOT THE RECORD'S RENAME** — `graphql(rename = "…")` moves the Rust type base and
/// has nothing to do with what the SDL calls the page.
#[test]
fn the_records_rust_rename_does_not_move_the_page_words() {
  assert_eq!(
    words(r#"async_graphql, rename = "Spool", connection(rename = "Page")"#),
    (Some("Page".to_owned()), None),
    "the page's word is its own"
  );
}

/// **A REPEATED ENTRY IS REFUSED IN THE READER'S OWN SENTENCE** — the shared `set_once` rule,
/// reached without either entry writing a rule of its own.
#[test]
fn a_repeated_word_is_refused_once_per_entry() {
  let repeated = refusal(r#"async_graphql, connection(rename = "A", rename = "B")"#);
  assert!(
    repeated.contains("more than once") && repeated.contains("connection(…)"),
    "{repeated}"
  );

  let edge = refusal(r#"async_graphql, edge(rename = "A", rename = "B")"#);
  assert!(
    edge.contains("more than once") && edge.contains("edge(…)"),
    "{edge}"
  );
}

/// **AN EMPTY OR BARE BLOCK IS REFUSED**, again in the shared reader's sentence: naming the type is
/// the only thing the block is for.
#[test]
fn a_block_with_nothing_in_it_is_refused() {
  for block in [
    "async_graphql, connection",
    "async_graphql, connection()",
    "async_graphql, edge",
    "async_graphql, edge()",
  ] {
    let refused = refusal(block);
    assert!(
      refused.contains("rename = ") && refused.contains("naming the type"),
      "{block}: {refused}"
    );
  }
}

/// **AN UNKNOWN ENTRY INSIDE EITHER BLOCK IS REFUSED**, and the sentence names the block.
#[test]
fn an_unknown_entry_inside_the_block_is_refused() {
  let refused = refusal(r#"async_graphql, connection(name = "Page")"#);
  assert!(
    refused.contains("unknown entry in `connection(…)`"),
    "{refused}"
  );
}

/// **A WORD THAT IS NOT A GraphQL NAME IS REFUSED** — the check that is not `syn::Ident`'s.
///
/// Rust's identifier alphabet is WIDER than the SDL's: a non-ASCII ident parses fine and would reach
/// a schema that async-graphql's own parser cannot read back. `wire::refuse_invalid_name` is what
/// stops it, and its note carries the measurement of what happens without it.
#[test]
fn a_word_outside_graphqls_name_grammar_is_refused() {
  let refused = refusal(r#"async_graphql, connection(rename = "débit")"#);
  assert!(refused.contains("is not a GraphQL name"), "{refused}");
}

/// **THE BLOCK'S UNKNOWN-ENTRY SENTENCE NAMES THEM**, so a declaration that misspells one is told
/// the two words exist.
#[test]
fn the_blocks_own_sentence_lists_the_two_new_entries() {
  let refused = refusal(r#"async_graphql, connexion(rename = "Page")"#);
  assert!(
    refused.contains("`connection`") && refused.contains("`edge`"),
    "{refused}"
  );
}