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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! **THE UNIFIED RENAME SYSTEM, READ OFF AN ELECTION** — the block form every name seat now takes,
//! the four type seats, the five road seats, and the framework vocabulary none of them reaches.
//!
//! ```text
//! #[ingraph(embedding, knn, lance(resource = "…"), graphql(async_graphql(
//!   first = 20,                              a COUNT — the one flat word left
//!   question(rename = "ClipAsk"),             a TYPE  — the @oneOf a point is named in
//!   engines(rename = "ClipLegs"),             a TYPE  — the per-leg product
//!   sighting(rename = "ClipAgreement"),       a TYPE  — the fused answer's evidence union
//!   fused_key_match(rename = "ClipLeader"),   a TYPE  — the row that carries it
//!   keys(rename = "clipNeighbours"),          a FIELD — one road, one word
//!   similar_hydrated(rename = "moreLikeThis"),
//! )))]
//! ```
//!
//! The two ANSWER-side seats are the 2026-08-18 ruling's, and they are the pair the family's own
//! audit had recorded as a gap: a client reads `<Index><Field>Sighting` and
//! `<Index><Field>FusedKeyMatch` by name, and until that ruling a declaration could re-base the
//! whole family or nothing at all. The union's ARMS take no seat — see
//! [`the_answer_seats_are_two_and_the_arms_are_neither`] for the exclusion and
//! `ingraph-codegen`'s `index/answer_seats.rs` for what they do to an emission.
//!
//! Beside [`skins`](super::skins) rather than inside it, on that file's own precedent: that one is
//! about WHICH BACKEND dresses a declaration and this one is about WHAT IT CALLS THINGS. A reader
//! looking for either should find it whole.
//!
//! # What is asserted here and what is asserted elsewhere
//!
//! Here: the GRAMMAR and the SENTENCE — which spellings parse, which are refused, and what a refusal
//! says. What the seats do to an EMISSION is `ingraph-codegen`'s `index/rename_resolution.rs`, where
//! the resolved names can be read off the token stream; that file is also where the claim that a
//! MEMBER BLOCK IS STANDALONE-LEGAL is spent rather than merely parsed.
//!
//! # Every accepting test states an exclusion
//!
//! *The block parsed* would pass for a grammar that swallowed a word and produced nothing, so each
//! acceptance names the seat that was filled AND at least one that was not: five roads and two types
//! in one block is exactly the shape where a word reaching its neighbour's seat would go unnoticed.

use ingraph_codegen::{
  index::{ElectionBackend, Field, IndexModel},
  model::GraphqlKind,
  prelude::NodeKind,
};

use super::tests::{fulltext, refused};

/// A VECTOR declaration whose ONE election wears whatever block a case hands it.
fn elected(block: proc_macro2::TokenStream) -> syn::Result<IndexModel> {
  let (host, arguments) = crate::host::split(&syn::parse_quote! {
    #[ingraph(upstream(node = "a::A", clip = "clip"), dedup(hash))]
    struct D {
      #[ingraph(embedding, knn, lance(resource = "crate::Lance"), graphql(async_graphql(#block)))]
      clip: ::ingraph::Embedding<f32, 4, Clip>,
    }
  });
  super::super::index::parse(&host, &arguments, NodeKind::Vector)
}

/// That election's `async_graphql` block, as the model holds it.
fn skin(model: &IndexModel) -> &ElectionBackend {
  model
    .numbers_of(&syn::Ident::new("clip", proc_macro2::Span::call_site()))
    .expect("the field wrote a block")
    .skin(GraphqlKind::AsyncGraphql)
    .expect("the block named `async_graphql`")
}

/// Every refusal a block earns, joined.
fn refusals(block: proc_macro2::TokenStream) -> String {
  elected(block)
    .expect_err("the block is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// **EVERY ROAD WORD REACHES ITS OWN SEAT AND NO OTHER.**
///
/// The claim the five-word vocabulary rests on, and it cannot be made one road at a time: the
/// grammar fills seats through a table of `&mut`s and the model reads them back through
/// `ElectionBackend::road`, so a road wired to its neighbour's slot would show as two roads holding
/// one word and one holding none. Written as a walk over `Field::ALL` so that a sixth road cannot be
/// added to the vocabulary without being wired.
#[test]
fn every_road_word_reaches_its_own_seat() {
  for road in Field::ALL {
    let word = road.as_str();
    let written = format!("{word}(rename = \"aWord\")");
    let block: proc_macro2::TokenStream = written.parse().expect("a block entry");
    let model = elected(block).unwrap_or_else(|_| panic!("`{written}` is legal"));
    let skin = skin(&model);
    assert_eq!(
      skin.road(road).map(syn::LitStr::value),
      Some("aWord".to_owned()),
      "`{word}` did not fill its own seat",
    );
    for other in Field::ALL.into_iter().filter(|other| *other != road) {
      assert!(
        skin.road(other).is_none(),
        "`{word}` also filled `{}`",
        other.as_str(),
      );
    }
  }
}

/// **THE FOUR TYPE SEATS ARE READ, AND THEY ARE NOT THE ROADS.**
#[test]
fn the_type_seats_are_read_and_reach_neither_road_nor_number() {
  let model = elected(quote::quote! {
    question(rename = "ClipAsk"),
    engines(rename = "ClipLegs"),
    sighting(rename = "ClipAgreement"),
    fused_key_match(rename = "ClipLeader")
  })
  .expect("the four type seats are legal");
  let skin = skin(&model);
  for (read, spelled) in [
    (skin.question.as_ref(), "ClipAsk"),
    (skin.engines.as_ref(), "ClipLegs"),
    (skin.sighting.as_ref(), "ClipAgreement"),
    (skin.fused_key_match.as_ref(), "ClipLeader"),
  ] {
    assert_eq!(read.map(syn::LitStr::value), Some(spelled.to_owned()));
  }
  // THE EXCLUSIONS: a type word that reached a road would publish a query field called `ClipAsk`,
  // and one that reached the number would be a count parsed out of a name.
  assert!(Field::ALL.into_iter().all(|road| skin.road(road).is_none()));
  assert!(skin.first.is_none());
}

/// **THE ANSWER SIDE'S TWO SEATS ARE TWO, AND THE ARMS ARE NEITHER OF THEM.**
///
/// Written one word at a time rather than both together, because the claim is that each fills ITS
/// OWN slot: the four-seat test above passes for a grammar whose second reader wrote into the
/// first's slot as long as both words are present, and this is what does not.
///
/// The ARM exclusion is a grammar claim and not an emission one — there is no word for an arm, so
/// the near-miss list is where an author who reached for one lands. What the arms actually do to an
/// expansion is `ingraph-codegen`'s `index/answer_seats.rs`.
#[test]
fn the_answer_seats_are_two_and_the_arms_are_neither() {
  let union =
    elected(quote::quote! { sighting(rename = "ClipAgreement") }).expect("`sighting` is a seat");
  assert_eq!(
    skin(&union).sighting.as_ref().map(syn::LitStr::value),
    Some("ClipAgreement".to_owned()),
  );
  assert!(
    skin(&union).fused_key_match.is_none(),
    "the union's word filled the row's seat as well",
  );

  let row = elected(quote::quote! { fused_key_match(rename = "ClipLeader") })
    .expect("`fused_key_match` is a seat");
  assert_eq!(
    skin(&row).fused_key_match.as_ref().map(syn::LitStr::value),
    Some("ClipLeader".to_owned()),
  );
  assert!(
    skin(&row).sighting.is_none(),
    "the row's word filled the union's seat as well",
  );

  // NO ARM WORD: an arm is `<Index><Field><Engine>Sighting`, derived from the leg's own word, so a
  // seat here would be one word over a set of types. The author lands on the vocabulary list.
  let refused = refusals(quote::quote! { sightings(rename = "aWord") });
  assert!(
    refused.contains("unknown entry in a backend block on `clip`"),
    "{refused}",
  );
  assert!(
    refused.contains("did you mean `sighting`"),
    "the near miss is measured against the seat that does exist: {refused}",
  );
}

/// **EACH ANSWER SEAT IS NAMED ONCE, AND ITS NAME IS HELD TO THE WIRE ALPHABET** — the two refusals
/// every other seat in this block already earns, spent at the pair the ruling added.
#[test]
fn an_answer_seat_takes_one_name_and_that_name_is_a_wire_name() {
  for word in ["sighting", "fused_key_match"] {
    let twice: proc_macro2::TokenStream = format!("{word}(rename = \"A\", rename = \"B\")")
      .parse()
      .expect("a block entry");
    let refused = refusals(twice);
    assert!(
      refused.contains(&format!("declared more than once in `{word}(…)`")),
      "`{word}` took two names: {refused}",
    );

    let illegal: proc_macro2::TokenStream = format!("{word}(rename = \"9lives\")")
      .parse()
      .expect("a block entry");
    let refused = refusals(illegal);
    assert!(
      refused.contains("9lives"),
      "`{word}` published a name a schema cannot export: {refused}",
    );

    let empty: proc_macro2::TokenStream = format!("{word}(rename = \"\")")
      .parse()
      .expect("a block entry");
    let refused = refusals(empty);
    assert!(
      !refused.is_empty(),
      "`{word}` accepted the empty name: {refused}",
    );
  }
}

/// **`first` STAYED FLAT**, which is the one asymmetry in the block grammar and is deliberate: a
/// COUNT has no name for a `rename` row to carry.
///
/// Stated in BOTH directions, because either half alone would pass for a grammar that had quietly
/// accepted both spellings.
#[test]
fn the_count_is_written_flat_and_takes_no_rename_row() {
  let model = elected(quote::quote! { first = 7 }).expect("`first = N` is the spelling");
  assert_eq!(skin(&model).first.map(core::num::NonZeroU32::get), Some(7));

  let refused = refusals(quote::quote! { first(rename = "howMany") });
  assert!(
    refused.contains("is a COUNT"),
    "the refusal says what the word takes: {refused}",
  );
}

/// **THE FLAT ROAD SPELLING IS GONE**, and what an author who wrote the shipped one gets is the
/// block form by name rather than a silent acceptance.
///
/// `keys = "…"` parsed until the 2026-08-16 ruling, so the sentence has to be reachable: a grammar
/// that still took it would publish the field and this test would be the only thing that noticed.
#[test]
fn the_shipped_flat_spelling_is_refused_with_the_block_that_replaced_it() {
  for word in ["keys", "fused", "similar"] {
    let written = format!("{word} = \"aWord\"");
    let block: proc_macro2::TokenStream = written.parse().expect("a block entry");
    let refused = refusals(block);
    assert!(
      refused.contains(&format!("`{word}(…)` needs `rename = \"…\"`")),
      "`{written}` was not sent to its block: {refused}",
    );
    assert!(
      refused.contains("naming the field is the only thing it is for"),
      "and the sentence names the AXIS — a road names a field, not a type: {refused}",
    );
  }
}

/// **THE VOCABULARY IS CLOSED AND WIRED TO THE SUGGESTION ENGINE**, like every other block in this
/// grammar.
#[test]
fn an_unknown_word_is_measured_against_the_closed_set() {
  let refused = refusals(quote::quote! { hydrate(rename = "aWord") });
  assert!(
    refused.contains("unknown entry in a backend block on `clip`"),
    "{refused}",
  );
  assert!(
    refused.contains("did you mean `hydrated`"),
    "a near miss is measured against the road words: {refused}",
  );
  // And the sentence lists the roads as BLOCKS, which is the shape the author has to write.
  assert!(
    refused.contains("`similar_hydrated(rename = \"…\")`"),
    "the sentence spells the seats it offers: {refused}",
  );
}

/// **A ROAD NAME IS HELD TO GRAPHQL'S OWN NAME GRAMMAR**, at the literal that wrote it — the one
/// fence there is, because nothing below this layer refuses a field name a schema cannot export.
#[test]
fn a_road_name_is_held_to_the_wire_alphabet() {
  let refused = refusals(quote::quote! { keys(rename = "9lives") });
  assert!(refused.contains("9lives"), "{refused}");
}

/// **A RUST KEYWORD IS AN ORDINARY WORD HERE**, on the 2026-08-13 ruling every SDL rename in this
/// workspace carries: these name things in GraphQL's namespace and nothing in Rust.
#[test]
fn a_rust_keyword_is_a_legal_name_at_every_seat_in_this_block() {
  let model = elected(quote::quote! {
    question(rename = "type"), keys(rename = "where")
  })
  .expect("GraphQL's keywords are contextual and Rust's are not its business");
  assert_eq!(
    skin(&model).question.as_ref().map(syn::LitStr::value),
    Some("type".to_owned()),
  );
  assert_eq!(
    skin(&model).road(Field::Keys).map(syn::LitStr::value),
    Some("where".to_owned()),
  );
}

/// **ONE SEAT, ONE WORD** — a second `rename` inside one road block is the repeat every block in
/// this grammar refuses.
#[test]
fn a_seat_is_named_once() {
  let refused = refusals(quote::quote! { keys(rename = "a", rename = "b") });
  assert!(
    refused.contains("declared more than once in `keys(…)`"),
    "{refused}",
  );
}

/// **THE FRAMEWORK'S OWN VOCABULARY IS REFUSED BY NAME, WITH THE REASON** — law 3 as a surface
/// rather than a doc sentence.
///
/// By NAME and not as an unknown entry, because the author's idea is coherent: they wanted the
/// answer row called something else, and what they are owed is why one vocabulary across every
/// declaration is worth more than one schema's taste.
#[test]
fn the_frameworks_own_vocabulary_is_refused_at_an_election() {
  for word in [
    "score",
    "rank",
    "streams",
    "ordinal",
    "contribution",
    "node",
    "coordinates",
    "key",
  ] {
    let written = format!("{word}(rename = \"mine\")");
    let block: proc_macro2::TokenStream = written.parse().expect("a block entry");
    let refused = refusals(block);
    assert!(
      refused.contains(&format!("`{word}` is the FRAMEWORK's own word")),
      "`{written}` was not refused as the floor: {refused}",
    );
    assert!(
      refused.contains("a client written against one schema reads the next"),
      "and the sentence says what the floor buys: {refused}",
    );
    assert!(
      refused.contains("What IS nameable here is the TYPE behind an argument"),
      "and where the author's coherent idea DOES have a seat: {refused}",
    );
  }
}

/// **THE RETIRED SPELLING IS AN UNKNOWN WORD AND NO LONGER A FLOOR** — the 2026-08-19 change, read
/// as the behaviour it is.
///
/// `entity` was the Match family's tier-B row word until the 2026-08-18 alignment gave both tiers
/// `node`, and it stayed in the floor for one round as a reserved spelling. It is out, so an author
/// writing it is naming a word this grammar does not have rather than one it refuses to move — and
/// what they are owed is the list of seats that DO exist, which is exactly the unknown-entry
/// sentence. Both halves are asserted, because the failure this pins is the word going SILENT: a
/// spelling struck from the floor and wired to nothing would parse.
#[test]
fn the_retired_row_spelling_falls_to_the_unknown_entry_sentence() {
  let refused = refusals(quote::quote! { entity(rename = "mine") });
  assert!(
    !refused.contains("is the FRAMEWORK's own word"),
    "the floor still claims the retired spelling: {refused}",
  );
  assert!(
    refused.contains("unknown entry in a backend block on `clip`"),
    "{refused}",
  );
  // And the sentence is USEFUL rather than merely different: it names the seats the block has.
  assert!(
    refused.contains("question(rename = \"…\")"),
    "the unknown-entry sentence does not list the seats: {refused}",
  );
}

/// The same law at the HEAD, because a floor is not a floor at one position — and the head is where
/// an author who has just read the ruling's canonical example is most likely to write one.
#[test]
fn the_frameworks_own_vocabulary_is_refused_at_the_head() {
  let refused = refused(
    syn::parse_quote! {
      #[ingraph(
        upstream(node = "a::A", body = "text"),
        dedup(hash),
        graphql(async_graphql(score(rename = "points"))),
      )]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(
    refused.contains("`score` is the FRAMEWORK's own word"),
    "{refused}"
  );
  assert!(
    refused.contains("`options(rename = \"…\")`"),
    "and the head's own three seats are the ones it offers: {refused}",
  );
}

/// **`evidence` KEEPS ITS OWN SENTENCE**, and the exclusion is a measurement rather than an
/// oversight: it is the one floor row that is ALSO a Rust mint with a seat on the other axis, so the
/// head block's existing answer — naming that axis — is strictly more useful than the general one.
#[test]
fn the_one_floor_row_with_a_seat_elsewhere_keeps_the_more_specific_answer() {
  let refused = refused(
    syn::parse_quote! {
      #[ingraph(
        upstream(node = "a::A", body = "text"),
        dedup(hash),
        graphql(async_graphql(evidence(rename = "WhoAgreed"))),
      )]
      struct D { #[ingraph(searchable)] body: String }
    },
    NodeKind::Fulltext,
  );
  assert!(
    !refused.contains("is the FRAMEWORK's own word"),
    "the general sentence displaced the specific one: {refused}",
  );
  assert!(
    refused.contains("The RUST family is re-based by the top-level"),
    "{refused}",
  );
}

/// **THE FULL-TEXT KIND TAKES THE SAME BLOCK**, which is what makes this a grammar rather than one
/// kind's arrangement — the seats are read off the field wherever a field elects.
#[test]
fn the_block_reaches_a_full_text_election_by_the_same_words() {
  let model = fulltext(&syn::parse_quote! {
    #[ingraph(upstream(node = "a::A", body = "text"), dedup(hash))]
    struct D {
      #[ingraph(searchable, graphql(async_graphql(keys(rename = "bodyHits"))))]
      body: String,
    }
  })
  .expect("a full-text election dresses itself the same way");
  assert_eq!(
    model
      .numbers_of(&syn::Ident::new("body", proc_macro2::Span::call_site()))
      .and_then(|written| written.skin(GraphqlKind::AsyncGraphql))
      .and_then(|skin| skin.road(Field::Keys))
      .map(syn::LitStr::value),
    Some("bodyHits".to_owned()),
  );
}