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
//! What an emitted WIRE field may be called.
//!
//! One rule, and its authority is GraphQL's own grammar: a Name is `[_A-Za-z][_0-9A-Za-z]*`, so a
//! field named outside that produces a schema that is not GraphQL.
//!
//! Part of the layer every `ingraph` derive parses on; `Record` is its first consumer.

use crate::errors::Errors;

/// Whether `name` is a GraphQL Name: non-empty, `[A-Za-z_]` then `[A-Za-z0-9_]*`.
///
/// The SDL grammar's own production rather than a charset this crate picked, which is the whole of
/// why a name is held to it. Nothing this crate can call states that production, so it is written
/// out here — and what keeps the writing faithful is not a convention but a TEST:
/// `tests::the_wire_name_rule_is_graphqls_name_grammar` states the grammar a second time,
/// independently of this, and asks both of every case.
pub(crate) fn is_graphql_name(name: &str) -> bool {
  let mut chars = name.chars();
  chars
    .next()
    .is_some_and(|first| first.is_ascii_alphabetic() || first == '_')
    && chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// Refuse a wire field whose EFFECTIVE name — the field's own, or whatever renamed it — is not a
/// GraphQL Name.
///
/// Nothing downstream would. Measured against `async-graphql` 7.2.1 with `bit-rate`, `9lives`, `""`,
/// `bit rate`, `$where` and `débit`: every one builds its schema without an error and without a
/// panic, introspection reports the field under its bad name, and the SDL the schema exports is
/// rejected by `async-graphql`'s OWN parser. The field is also unreachable — a query selecting it
/// fails to PARSE, aliasing included, since the name itself is what the query grammar chokes on — and
/// the resulting error is a syntax error naming a client's document, mentioning neither the schema
/// nor the declaration. So this is a silent defect rather than a late one, and the refusal has to be
/// here because there is no later place that produces one.
///
/// `at` is whatever declared the name — the `rename` literal, or the field, whose own name is what
/// an unrenamed position exposes.
pub(crate) fn refuse_invalid_name(name: &str, at: impl quote::ToTokens, errors: &mut Errors) {
  if !is_graphql_name(name) {
    errors.push(syn::Error::new_spanned(
      at,
      format!(
        "`{name}` is not a GraphQL name — the SDL grammar requires `[_A-Za-z][_0-9A-Za-z]*`, and \
         nothing downstream refuses one: the schema builds, the SDL it exports is not parseable \
         GraphQL, and the field can never be selected because a query naming it fails to parse. \
         Refused here because there is nowhere later that would."
      ),
    ));
  }
}