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 `members(<Variant>(field = "…"))` — one of the three positions the
//! raw-ident tolerance was built for and had not reached.
//!
//! The string-kind schema-words law says a Rust KEYWORD is an ordinary schema name, which is why a
//! column's `rename` is carried as a literal and never parsed. This position is the other side of
//! it: what `field = "…"` names is a RUST FIELD — on the member's own struct and on its row, both
//! reached by field access — so the value has to become an ident, and `syn`'s own `Ident` reader
//! refuses `type` exactly as rustc's grammar does. A member whose column is legally called
//! `r#type`, and whose schema and database both know it as `type`, was therefore unnameable here.
//!
//! Both spellings are the field and both read as the RAW identifier, which is what the emission has
//! to spell. The FENCE is unmoved: what is an identifier under neither spelling is still refused,
//! with this position's own sentence.

use syn::ext::IdentExt as _;

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

/// One variant name, for the lookup the model answers a column by.
fn ident(name: &str) -> syn::Ident {
  syn::Ident::new(name, proc_macro2::Span::call_site())
}

/// **A MEMBER'S COLUMN MAY BE A KEYWORD**, written either way, and both are the same field.
///
/// 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`,
/// which is the word an author reaches for.
#[test]
fn a_members_column_may_be_a_keyword_under_both_spellings() {
  for written in ["type", "r#type"] {
    let entry: proc_macro2::TokenStream = format!(r#"field = "{written}""#)
      .parse()
      .expect("a member block entry");
    let model = parse(&syn::parse_quote! {
      #[ingraph(fields(kind(type = "String", members(Audio(#entry)))))]
      pub enum Media {
        Video(Video),
        Audio(Audio),
      }
    })
    .expect("a keyword names a member's column");

    let column = model.fields[0].column(&ident("Audio"));
    assert_eq!(
      *column, *"r#type",
      "written as {written:?}, the column reads as the raw identifier the emission spells"
    );
    assert_eq!(column.unraw(), ident("type"), "written as {written:?}");
  }
}

/// An ordinary column is untouched by the tolerance — it is not read as raw, so the field access an
/// emission writes for it is the plain one.
#[test]
fn an_ordinary_members_column_stays_an_ordinary_identifier() {
  let model = parse(&syn::parse_quote! {
    #[ingraph(fields(created_at(type = "Timestamp", members(Audio(field = "imported_at")))))]
    pub enum Media {
      Video(Video),
      Audio(Audio),
    }
  })
  .expect("an ordinary name names a member's column");

  assert_eq!(*model.fields[0].column(&ident("Audio")), *"imported_at");
}

/// **THE FENCE, UNMOVED.** A value no spelling could make an identifier is still refused, and with
/// the position's own sentence rather than the reader's.
#[test]
fn a_value_that_names_no_field_is_still_refused() {
  for written in ["not an ident", "", "9lives", "a::b", "crate"] {
    let entry: proc_macro2::TokenStream = format!(r#"field = "{written}""#)
      .parse()
      .expect("a member block entry");
    let refused = refusal(&syn::parse_quote! {
      #[ingraph(fields(title(type = "String", members(Video(#entry)))))]
      pub enum Media {
        Video(Video),
      }
    });

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