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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! THE THIRD IDENTITY, read — which mode a declaration took, what its slot table came out as, and
//! every refusal the composed side carries.
//!
//! `tests/refusals/` proves the messages REACH a user, spanned at the token that broke the rule;
//! these prove the reader decides correctly in the first place, which is the half a compile-fail
//! fixture cannot separate out — `element_tests`'s own division, one identity over.

use super::{Face, Mode};

/// EVERY refusal a declaration produced, one per line — `element_tests`'s reader, for its reason:
/// one compile reports the whole declaration's problems, and `syn::Error`'s `Display` renders only
/// the first of a sealed set.
fn refused(read: syn::Result<Mode>) -> String {
  match read {
    Ok(_) => panic!("the declaration is refused"),
    Err(errors) => errors
      .into_iter()
      .map(|error| error.to_string())
      .collect::<Vec<_>>()
      .join("\n"),
  }
}

/// The composed model a marked declaration reads as.
fn composed(read: syn::Result<Mode>) -> ingraph_codegen::composed::ComposedModel {
  match read.expect("a `composed` declaration reads as a composed filter") {
    Mode::Composed(model) => *model,
    Mode::Element(_) | Mode::Struct(_) => panic!("`composed` is this mode's watershed"),
  }
}

/// The wire names of a model's SLOTS, in declaration order.
///
/// A spliced field contributes none: what it publishes is its component's list, which is a fact
/// about the component rather than about this declaration.
fn slots(model: &ingraph_codegen::composed::ComposedModel) -> Vec<&str> {
  model
    .fields
    .iter()
    .filter_map(|field| match &field.kind {
      ingraph_codegen::composed::ComposedKind::Slot(wire) => Some(wire.as_str()),
      ingraph_codegen::composed::ComposedKind::Spliced => None,
    })
    .collect::<Vec<_>>()
}

/// The members a model SPLICES, in declaration order.
fn spliced(model: &ingraph_codegen::composed::ComposedModel) -> Vec<String> {
  model
    .fields
    .iter()
    .filter(|field| matches!(field.kind, ingraph_codegen::composed::ComposedKind::Spliced))
    .map(|field| field.member.to_string())
    .collect::<Vec<_>>()
}

/// **THE MARKER IS THE WATERSHED**, and it is the same watershed the element mode has: one shape,
/// three readings, and the word an author writes is what tells them apart.
#[test]
fn the_marker_decides_the_identity() {
  let unmarked: syn::DeriveInput = syn::parse_quote! {
    pub struct Where {
      eq: Option<i64>,
    }
  };
  assert!(matches!(
    super::parse(&unmarked, Face::Filter).expect("an unmarked struct is the struct mode"),
    Mode::Struct(_)
  ));

  let marked: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  assert_eq!(
    slots(&composed(super::parse(&marked, Face::Filter))),
    ["eq"]
  );
}

/// **THE SLOT TABLE IS THE STRUCT BODY**, in declaration order and folded the way every other wire
/// member is: a raw prefix dropped, a trailing underscore eaten, the rest lowerCamel.
#[test]
fn the_slot_table_is_the_fields_in_order() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct RateFilter {
      eq: Option<Rate>,
      in_: Option<Box<[Rate]>>,
      not_in: Option<Box<[Rate]>>,
      starts_with: Option<Utf8Bytes>,
      numerator: Option<ScalarFilter<i32>>,
    }
  };
  let model = composed(super::parse(&input, Face::Filter));

  assert_eq!(
    slots(&model),
    ["eq", "in", "notIn", "startsWith", "numerator"]
  );
  // The SUBJECT is the filter, so the schema name is its own — no `Filter` suffix bolted on.
  assert_eq!(model.sdl.external(), "RateFilter");
}

/// The RAW spelling of the keyword slot folds the way the underscore one does.
#[test]
fn a_raw_slot_name_folds_to_the_word_it_escapes() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      r#in: Option<Box<[i64]>>,
    }
  };
  assert_eq!(slots(&composed(super::parse(&input, Face::Filter))), ["in"]);
}

/// The TWO RENAME KNOBS: the container moves the input object's name, a field moves one slot's.
#[test]
fn the_rename_words_move_the_object_and_the_slot() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed, filter(rename = "ClipWhere"))]
    pub struct ClipCriteria {
      #[ingraph(rename = "atLeast")]
      gte: Option<Rate>,
    }
  };
  let model = composed(super::parse(&input, Face::Filter));

  assert_eq!(model.sdl.external(), "ClipWhere");
  assert_eq!(slots(&model), ["atLeast"]);
}

/// A slot's `rename` is STRING-kind, so it can name a slot Rust cannot spell — which is not an
/// edge case: `in` is an operator on four of this crate's own filter kinds.
#[test]
fn a_slot_may_be_renamed_onto_a_rust_keyword() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      #[ingraph(rename = "in")]
      members: Option<Box<[i64]>>,
    }
  };
  assert_eq!(slots(&composed(super::parse(&input, Face::Filter))), ["in"]);
}

/// **EVERY SLOT IS AN `Option`** — the absence is the meaning, so a bare `T` is refused by name at
/// the type that wrote it.
#[test]
fn a_slot_that_is_not_optional_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      eq: i64,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(refusal.contains("`eq` is an operator SLOT"), "{refusal}");
  assert!(refusal.contains("Option<i64>"), "{refusal}");
}

/// The TWO MARKERS are the two readings of one declaration and no declaration is both.
#[test]
fn the_two_markers_refuse_together() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed, operator(string))]
    pub struct Tag(String);
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`operator(…)` and `composed` are the two ELEMENT and FILTER readings"),
    "{refusal}"
  );
}

/// A filter with NO slot can ask nothing, and the input object it would publish has no field.
#[test]
fn an_empty_declaration_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {}
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("a COMPOSED filter's fields ARE its vocabulary"),
    "{refusal}"
  );
}

/// TWO FIELDS ON ONE WIRE NAME. The fold is not injective, so the collision is a declaration that
/// looks right and publishes one field where it declared two.
#[test]
fn two_fields_folding_onto_one_slot_are_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      not_in: Option<Box<[i64]>>,
      #[ingraph(rename = "notIn")]
      excluded: Option<Box<[i64]>>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`excluded` and `not_in` are both the slot `notIn`"),
    "{refusal}"
  );
}

/// A slot's `rename` is fenced by GraphQL's own Name grammar, for the reason every wire name is:
/// nothing downstream refuses one, the schema builds, and the field can never be selected.
#[test]
fn a_slot_renamed_outside_the_name_grammar_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      #[ingraph(rename = "not-in")]
      not_in: Option<Box<[i64]>>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`not-in` is not a GraphQL name"),
    "{refusal}"
  );
}

/// A field takes `rename` and `flatten` and nothing else — the interpretation, storage and
/// `graphql(…)` words are a COLUMN's, and a slot is not one.
#[test]
fn a_column_word_on_a_slot_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      #[ingraph(scalar)]
      eq: Option<i64>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("a COMPOSED filter's field takes `rename = \"…\"` and `flatten`"),
    "{refusal}"
  );
}

/// The marker is the FILTER door's word: the other two doors emit a face over a row's COLUMNS, and
/// a composed declaration's fields are operator slots.
#[test]
fn the_marker_is_the_filter_doors_word() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      eq: Option<i64>,
    }
  };

  for face in [Face::Comparison, Face::OrderBy] {
    let refusal = refused(super::parse(&input, face));
    assert!(
      refusal.contains("`composed` declares a FILTER and reaches no face"),
      "{refusal}"
    );
  }
}

/// The container words a composed declaration does not spend, each refused where it was written.
#[test]
fn the_words_a_composed_filter_does_not_spend_refuse_by_name() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(
      composed,
      sqlite,
      comparison(rename = "WhereRow"),
      order_by(rename = "WhereSort"),
      filter = "crate::Seat"
    )]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));

  for word in [
    "`order_by(rename = \"…\")` reaches no face the COMPOSED mode emits",
    "`comparison(rename = \"…\")` reaches no face the COMPOSED mode emits",
    "`filter = \"…\"` reaches no face the COMPOSED mode emits",
    "`sqlite` reaches no face the COMPOSED mode emits",
  ] {
    assert!(refusal.contains(word), "{word}\n{refusal}");
  }
}

/// `composed` is a MARKER: it takes no value, because the field table is the struct body and there
/// is nothing for one to name.
#[test]
fn the_marker_takes_no_value() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed = "Where")]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`composed` is a MARKER and takes no value"),
    "{refusal}"
  );
}

/// The marker's BLOCK carries exactly one word, and every other is refused by name.
#[test]
fn the_marker_block_takes_one_word() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed(eq))]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`composed(…)` takes `shorthand = <bool>` and nothing else"),
    "{refusal}"
  );
}

/// **RULE ① IS TAKEN UNLESS THE DECLARATION DECLINES IT** — the shape every optional flag in this
/// grammar has: what a declaration did not say is what the framework does.
#[test]
fn the_shorthand_is_taken_by_silence_and_declined_by_word() {
  let bare: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  assert!(composed(super::parse(&bare, Face::Filter)).shorthand);

  let empty: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed())]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  assert!(composed(super::parse(&empty, Face::Filter)).shorthand);

  let taken: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed(shorthand = true))]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  assert!(composed(super::parse(&taken, Face::Filter)).shorthand);

  let declined: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed(shorthand = false))]
    pub struct Where {
      contains: Option<i64>,
    }
  };
  assert!(!composed(super::parse(&declined, Face::Filter)).shorthand);
}

/// The flag is a BOOLEAN, and a word that is not one is refused where it was written.
#[test]
fn the_shorthand_flag_is_a_boolean() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed(shorthand = "no"))]
    pub struct Where {
      eq: Option<i64>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`composed(shorthand = …)` must be `true` or `false`"),
    "{refusal}"
  );
}

/// **`flatten` IS THE OTHER KIND OF FIELD** — the member is spliced, so it carries no wire name of
/// its own and the slots around it keep theirs.
#[test]
fn a_flattened_field_is_spliced_rather_than_named() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct SoundEventDetectionFilter {
      #[ingraph(flatten)]
      code: CodeFilter,
      under: Option<Box<[SoundEventCode]>>,
      score: Option<ScalarFilter<Score>>,
    }
  };
  let model = composed(super::parse(&input, Face::Filter));

  assert_eq!(slots(&model), ["under", "score"]);
  assert_eq!(spliced(&model), ["code"]);
}

/// A SPLICED component publishes its own component's names, so there is nothing here for `rename` to
/// move.
#[test]
fn a_renamed_splice_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      #[ingraph(flatten, rename = "code")]
      code: CodeFilter,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`code` is SPLICED, so it has no name of its own for `rename` to move"),
    "{refusal}"
  );
}

/// A SPLICED component is written BARE: the `Option` on a slot is an operator the client did not
/// write, and a component has no such absence of its own.
#[test]
fn an_optional_splice_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      #[ingraph(flatten)]
      code: Option<CodeFilter>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(
    refusal.contains("`code` is SPLICED and a spliced component is written BARE"),
    "{refusal}"
  );
}

/// `flatten` is written BARE — the component decides what its fields are called.
#[test]
fn a_flatten_with_a_block_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where {
      #[ingraph(flatten(prefix = "code"))]
      code: CodeFilter,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(refusal.contains("`flatten` is written BARE"), "{refusal}");
}

/// A GENERIC composed declaration is refused, which is the family's own rule for the family's own
/// reason: every emitted face names this type, and a parameter would have to be threaded through
/// each one.
#[test]
fn a_generic_declaration_is_refused() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(composed)]
    pub struct Where<T> {
      eq: Option<T>,
    }
  };
  let refusal = refused(super::parse(&input, Face::Filter));
  assert!(refusal.contains("takes a CONCRETE struct"), "{refusal}");
}