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 `r#type` ROW at `graphql(<backend>(output(field = "…")))` — one of the three positions the
//! raw-ident tolerance was built for and had not reached.
//!
//! What this word names is a RUST ACCESSOR: the emitted output object reads the field through a
//! method whose name is built from it by suffix, so the value has to become an ident and `syn`'s own
//! `Ident` reader refuses `type` exactly as rustc's grammar does. A schema whose column is called
//! `type` — a legal name, and the one the database uses — could therefore not move its accessor onto
//! the field that carries it.
//!
//! Both spellings are the accessor, and both read as the RAW identifier, which is what the emission
//! has to spell. The FENCE is unmoved: a value that is an identifier under neither spelling is still
//! refused with this position's own sentence.

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

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

/// The accessor name one field's output face carries, or `None` where the word was not written.
///
/// The backend match is `let … else` rather than exhaustive: [`GraphqlBackend`] is
/// `#[non_exhaustive]` and this crate reads it from outside, so a second rendering arriving is a
/// panic here rather than a compile error — and this suite's subject is the READER, which is one
/// pass whichever library serves the schema.
fn output_field(model: &RecordModel, at: usize) -> Option<String> {
  let GraphqlBackend::AsyncGraphql(backend) =
    &model.graphql.as_ref().expect("a wire face").backends[0]
  else {
    panic!("the declaration names `async_graphql`")
  };
  backend.fields[at]
    .output_field
    .as_ref()
    .map(ToString::to_string)
}

/// Every refusal one declaration collects, as one string.
fn refusal(input: syn::DeriveInput) -> String {
  record(&input)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join(" | ")
}

/// **AN OUTPUT ACCESSOR MAY BE A KEYWORD**, written either way, and both name one accessor.
///
/// The `r#type` half already parsed before the tolerance — `syn` compares the ESCAPED spelling
/// against the keyword list and `r#type` is not on it — so the row that moved is the bare `type`,
/// the word an author reaches for because the schema and the database both call the column that.
#[test]
fn an_output_accessor_may_be_a_keyword_under_both_spellings() {
  for written in ["type", "r#type"] {
    let entry: proc_macro2::TokenStream = format!(r#"field = "{written}""#)
      .parse()
      .expect("an output block entry");
    let model = record(&syn::parse_quote! {
      #[ingraph(sqlite, graphql(async_graphql))]
      struct Track {
        #[ingraph(id)]
        id: u64,
        #[ingraph(string, graphql(async_graphql(output(#entry))))]
        kind: String,
      }
    })
    .expect("a keyword names an output accessor");

    assert_eq!(
      output_field(&model, 1).as_deref(),
      Some("r#type"),
      "written as {written:?}, the accessor reads as the raw identifier the emission spells"
    );
  }
}

/// An ordinary accessor is untouched by the tolerance — it is not read as raw, so the method an
/// emission mints from it is the plain one.
#[test]
fn an_ordinary_output_accessor_stays_an_ordinary_identifier() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct Track {
      #[ingraph(id)]
      id: u64,
      #[ingraph(string, graphql(async_graphql(output(field = "flavour"))))]
      kind: String,
    }
  })
  .expect("an ordinary name names an output accessor");

  assert_eq!(output_field(&model, 1).as_deref(), Some("flavour"));
}

/// **THE FENCE, UNMOVED.** A value no spelling could make an identifier is still refused, with the
/// position's own sentence rather than the reader's.
#[test]
fn a_value_that_names_no_accessor_is_still_refused() {
  for written in ["not an ident", "", "9lives", "a::b", "crate"] {
    let entry: proc_macro2::TokenStream = format!(r#"field = "{written}""#)
      .parse()
      .expect("an output block entry");
    let refused = refusal(syn::parse_quote! {
      #[ingraph(sqlite, graphql(async_graphql))]
      struct Track {
        #[ingraph(id)]
        id: u64,
        #[ingraph(string, graphql(async_graphql(output(#entry))))]
        kind: String,
      }
    });

    assert!(
      refused.contains("as an identifier in a string literal"),
      "written as {written:?}: {refused}"
    );
  }
}