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 `lineage(fields(<column>(source = "…")))` — the third of the positions the
//! raw-ident tolerance was built for and had not reached.
//!
//! What this word names is one FIELD of the designated parent's KEY, and the emitted equation
//! reaches it 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 parent whose key field is legally called
//! `r#type`, and whose schema and database both know it as `type`, could therefore not be mirrored.
//!
//! Both spellings are the field and both read as the RAW identifier, which is what the emission has
//! to spell. The reader RESPANS to the literal for the reason the position always kept the string:
//! every error the emitted equation reports about this mapping lands where the author wrote it. The
//! FENCE is unmoved, with this position's own sentence.

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

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

/// The mirrors a fusion declared, as `(column, parent field)` in declaration order.
fn mirrors(model: &RecordModel) -> Vec<(String, String)> {
  model
    .upstreams
    .as_ref()
    .and_then(Upstreams::fused)
    .expect("the designation reaches the model")
    .mirrors
    .iter()
    .map(|mirror| (mirror.column.to_string(), mirror.from.to_string()))
    .collect()
}

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

/// **A PARENT'S KEY FIELD MAY BE A KEYWORD**, written either way, and both name one 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`,
/// the word an author reaches for because the schema and the database both call the column that.
#[test]
fn a_mirrored_parent_field_may_be_a_keyword_under_both_spellings() {
  for written in ["type", "r#type"] {
    let entry: proc_macro2::TokenStream = format!(r#"source = "{written}""#)
      .parse()
      .expect("a lineage field entry");
    let model = operator(&syn::parse_quote! {
      #[ingraph(
        upstream(node = "media::Frame", key),
        upstream(node = "asr::Segment"),
        lineage(fields(kind(#entry)))
      )]
      struct Caption {
        #[ingraph(id)] id: Uuid7,
        kind: Uuid7,
      }
    })
    .expect("a keyword names a field of the parent's key");

    assert_eq!(
      mirrors(&model),
      [("kind".to_owned(), "r#type".to_owned())],
      "written as {written:?}, the mapping reads as the raw identifier the emission spells"
    );
  }
}

/// An ordinary mapping is untouched by the tolerance — it is not read as raw, so the field access
/// the emitted equation writes for it is the plain one.
#[test]
fn an_ordinary_mirrored_field_stays_an_ordinary_identifier() {
  let model = operator(&syn::parse_quote! {
    #[ingraph(
      upstream(node = "media::Frame", key),
      upstream(node = "asr::Segment"),
      lineage(fields(frame(source = "id")))
    )]
    struct Caption {
      #[ingraph(id)] id: Uuid7,
      frame: Uuid7,
    }
  })
  .expect("an ordinary name names a field of the parent's key");

  assert_eq!(mirrors(&model), [("frame".to_owned(), "id".to_owned())]);
}

/// **THE FENCE, UNMOVED.** A value no spelling could make an identifier is still refused, and the
/// sentence is the position's own — it says what the value has to BE rather than reporting a parse.
#[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#"source = "{written}""#)
      .parse()
      .expect("a lineage field entry");
    let refused = refusal(syn::parse_quote! {
      #[ingraph(
        upstream(node = "media::Frame", key),
        upstream(node = "asr::Segment"),
        lineage(fields(kind(#entry)))
      )]
      struct Caption {
        #[ingraph(id)] id: Uuid7,
        kind: Uuid7,
      }
    });

    assert!(
      refused.contains("names ONE field of the parent's key, so its value is a field name"),
      "written as {written:?}: {refused}"
    );
  }
}