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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! THE `skip` WORD, read at the parser — both axes, both seats, and every tooth.
//!
//! The ruling is the two-axis table on #112: `skip` is no column in any persisted database,
//! `<db>(skip)` is one database's own answer, `graphql(skip)` is absent from every wire backend, and
//! `graphql(<backend>(skip))` is absent from one. Beside it ride the laws — identity is unskippable
//! on both axes, stored-nowhere-yet-published is refused by name, and a repeated word is the
//! repeat-refusal family — and the hydration rider, which is `Default::default()` or the function
//! `skip(default = "path::to::fn")` names.
//!
//! What is pinned HERE is the front end: which spellings are read, which are refused, and what each
//! refusal SAYS. That the emission then omits the column, fills the field and sweeps the schema is
//! pinned where those are written — see `tests/skip_word.rs`, which builds and uses the expansion,
//! and `tests/refusals` / `tests/storability`, which prove the messages reach a real compile.
//!
//! Each refusal is asserted on a PHRASE the sentence exists to carry rather than on the whole
//! string, so a wording pass does not turn into a test edit — but the phrase is always the part
//! that names the rule, so a rule that stopped being enforced cannot pass.

use ingraph_codegen::prelude::NodeKind;

use crate::parse;

/// Every refusal a record declaration earned, joined.
fn refusals(input: syn::DeriveInput) -> String {
  let (host, arguments) = crate::host::split(&input);
  parse::parse(&host, &arguments, NodeKind::Record)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// The model a declaration that is NOT refused produces.
fn model(input: syn::DeriveInput) -> ingraph_codegen::prelude::RecordModel {
  let (host, arguments) = crate::host::split(&input);
  match parse::parse(&host, &arguments, NodeKind::Record) {
    Ok(model) => model,
    Err(errors) => panic!(
      "the declaration is accepted: {}",
      errors
        .into_iter()
        .map(|e| e.to_string())
        .collect::<Vec<_>>()
        .join("\n")
    ),
  }
}

/// **THE BARE WORD.** A skipped field is a column in NO declared database, and it carries the fill a
/// raise supplies instead.
#[test]
fn the_bare_word_mints_no_column_anywhere_and_carries_its_fill() {
  let model = model(syn::parse_quote! {
    #[ingraph(sqlite, postgres)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar)]
      frame: u32,
      #[ingraph(skip)]
      clip: Vec<f32>,
    }
  });

  let persist = model.persist.as_ref().expect("the record is stored");
  for database in &persist.backends {
    assert!(
      !database
        .columns()
        .iter()
        .any(|column| column.source == "clip"),
      "`{}` minted a column for a skipped field",
      database.kind().as_str()
    );
  }
  let skipped = model
    .fields
    .iter()
    .find(|field| field.column.source == "clip")
    .expect("a skipped field is still a field of the record");
  assert!(
    skipped
      .skip
      .as_ref()
      .expect("the word reaches the model")
      .named
      .is_none(),
    "a bare word names no filler, and the fill is `Default::default()`"
  );
}

/// **THE FILL IS NAMEABLE**, in serde's own spelling, and it travels as a PATH.
#[test]
fn the_word_takes_the_fill_serde_spells() {
  let model = model(syn::parse_quote! {
    #[ingraph(sqlite)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(skip(default = "crate::fixtures::no_clip"))]
      clip: Vec<f32>,
    }
  });

  let named = model
    .fields
    .iter()
    .find(|field| field.column.source == "clip")
    .and_then(|field| field.skip.as_ref())
    .and_then(|fill| fill.named.as_ref())
    .expect("the named filler reaches the model");
  assert_eq!(
    quote::quote!(#named).to_string().replace(' ', ""),
    "crate::fixtures::no_clip"
  );
}

/// **IDENTITY IS UNSKIPPABLE** on the storage axis, at the `id` and at every lineage step.
#[test]
fn identity_is_unskippable_on_the_storage_axis() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, lineage(fields(media_id)))]
    struct Shot {
      #[ingraph(id, skip)]
      id: u32,
      #[ingraph(copy, skip)]
      media_id: u32,
    }
  });

  assert!(
    refused.contains("is the record's `id`"),
    "the id names what it bears: {refused}"
  );
  assert!(
    refused.contains("is a step of the record's LINEAGE"),
    "a lineage step names what it bears: {refused}"
  );
  assert_eq!(
    refused
      .matches("Identity is unskippable on both axes")
      .count(),
    2,
    "both identity-bearing fields are refused: {refused}"
  );
}

/// The same law on the WIRE axis, at both of that axis's seats.
#[test]
fn identity_is_unskippable_on_the_wire_axis() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql), lineage(fields(media_id)))]
    struct Shot {
      #[ingraph(id, graphql(skip))]
      id: u32,
      #[ingraph(copy, graphql(async_graphql(skip)))]
      media_id: u32,
    }
  });

  assert!(
    refused.contains("the address is what a hit answers with"),
    "the wire half names its own reason: {refused}"
  );
  assert_eq!(
    refused
      .matches("Identity is unskippable on both axes")
      .count(),
    2,
    "both seats are refused: {refused}"
  );
}

/// **THE TWO STORAGE WORDS CONTRADICT.** One says the column is nowhere and the other refines the
/// one it has.
///
/// The pair used to be `skip` beside the BARE `persist`, which said the field was a column in every
/// declared database. The bare word went with the participation inversion — silence says it now —
/// so what contradicts `skip` is the block that survived it: a refinement is a statement about a
/// column, and this field has said it has none.
#[test]
fn a_field_cannot_both_skip_and_persist() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, skip, rename = "frame_number")]
      frame: u32,
    }
  });

  assert!(
    refused.contains("carries `skip` and `rename`"),
    "the pair is named, and the sentence names the WORD that contradicts: {refused}"
  );
  assert!(
    refused.contains("absent from SOME of the record's databases"),
    "the near miss is offered by name — the partial absence has a spelling now: {refused}"
  );
}

/// **STORED NOWHERE AND YET PUBLISHED**, refused by name, with the census pin the ruling asked for.
#[test]
fn stored_nowhere_and_published_is_refused_by_name() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, skip)]
      frame: u32,
    }
  });

  assert!(
    refused.contains(
      "wears `skip`, so it is a column in no database, and the wire face publishes every \
                 column"
    ),
    "the shape is named: {refused}"
  );
  assert!(
    refused.contains("resolver-computed field"),
    "the census pin rides the sentence: {refused}"
  );
}

/// A field withheld from the WIRE needs no stored source — which is the L12 shape, and the one
/// combination the rule above must not catch.
#[test]
fn a_wire_skipped_field_needs_no_stored_source() {
  let model = model(syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(skip(default = "crate::fixtures::no_clip"), graphql(skip))]
      clip: Vec<f32>,
    }
  });

  let graphql = model.graphql.as_ref().expect("the record has a wire face");
  assert!(
    !graphql.publishes(&syn::parse_quote!(clip)),
    "the word withholds the field from every vocabulary"
  );
  assert!(
    graphql
      .backends
      .iter()
      .all(|backend| !backend.fields().iter().any(|field| field.source == "clip")),
    "a withheld field never becomes a wire field"
  );
}

/// **THE REPEAT FAMILY**, at each of the word's four seats.
#[test]
fn a_repeated_skip_word_is_the_repeat_refusal_family() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, skip, skip)]
      frame: u32,
      #[ingraph(scalar, graphql(skip, skip))]
      title: u32,
    }
  });

  assert!(
    refused.contains("`skip` is declared more than once on this field — one word, one field"),
    "the storage seat: {refused}"
  );
  assert!(
    refused.contains(
      "`skip` is declared more than once in `graphql(…)` on this field — one word, one field"
    ),
    "the wire seat: {refused}"
  );
}

/// The WIRE word takes no fill, and the refusal says whose the fill is.
#[test]
fn the_wire_word_is_bare() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, graphql(skip(default = "crate::fixtures::zero")))]
      frame: u32,
    }
  });

  assert!(
    refused.contains("`skip` on the wire is a BARE word"),
    "the shape is named: {refused}"
  );
  assert!(
    refused.contains("`#[ingraph(skip(default = \"…\"))]`"),
    "the fill's own seat is shown: {refused}"
  );
}

/// A wire word beside what it makes unreachable is a rule with nothing to apply it to.
#[test]
fn the_wire_word_refuses_the_entries_it_makes_unreachable() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, graphql(skip, rename = "frameNumber"))]
      frame: u32,
    }
  });

  assert!(
    refused.contains("has no name, no form and no crossing to declare"),
    "the reason is stated: {refused}"
  );
}

/// **THE PER-DATABASE ABSENCE, WITH ITS FILL** — the column leaves ONE database's list and stays in
/// the others, and that database's raise supplies the value.
#[test]
fn a_per_database_skip_with_a_fill_masks_one_database_and_no_other() {
  let model = model(syn::parse_quote! {
    #[ingraph(sqlite, postgres)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, sqlite(skip(default = "no_frame")))]
      frame: u32,
    }
  });

  let persist = model.persist.as_ref().expect("a record stores rows");
  let named = |kind: ingraph_codegen::prelude::DatabaseKind| {
    persist
      .backends
      .iter()
      .find(|backend| backend.kind() == kind)
      .expect("the database is declared")
  };
  let sqlite = named(ingraph_codegen::prelude::DatabaseKind::Sqlite);
  let postgres = named(ingraph_codegen::prelude::DatabaseKind::Postgres);

  assert!(
    !sqlite
      .columns()
      .iter()
      .any(|column| column.source == "frame"),
    "the masked database mints no column for it"
  );
  assert!(
    postgres
      .columns()
      .iter()
      .any(|column| column.source == "frame"),
    "and the other one still does"
  );
  assert_eq!(
    sqlite.storage().fills.len(),
    1,
    "the masked database carries the fill"
  );
  assert!(
    postgres.storage().fills.is_empty(),
    "and the database that has the column carries none"
  );
  // That a column one database lacks is also out of the SHARED query vocabulary is the emitter's
  // half of the law, and it is pinned where that list is computed — see
  // `ingraph_codegen::tests::a_column_one_database_narrows_away_leaves_the_shared_vocabulary`.
}

/// **THE BARE `<db>(skip)` KEEPS A REFUSAL**, and it is the fill it asks for rather than the
/// capability it denies.
#[test]
fn a_per_database_skip_with_no_fill_is_refused_for_the_fill() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, postgres)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, sqlite(skip))]
      frame: u32,
    }
  });

  assert!(
    refused.contains("a read of a `sqlite` row has to hand something back"),
    "the demand is the READ's, said at the word: {refused}"
  );
  assert!(
    refused.contains(r#"#[ingraph(sqlite(skip(default = "…")))]"#),
    "and the spelling that answers it is written out: {refused}"
  );
}

/// **THE OTHER SPELLING**: a bare `<db>` narrowing the field to SOME of the declared databases is
/// the same absence read from its other end, and it earns the same demand for a fill.
#[test]
fn a_field_narrowed_by_a_presence_list_is_asked_for_the_fill() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, postgres)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, sqlite)]
      frame: u32,
    }
  });

  assert!(
    refused.contains("has no `frame` to fetch and nothing to hand back instead"),
    "the refusal names the READ that has no answer: {refused}"
  );
  assert!(
    refused.contains("postgres(skip(default"),
    "and names the spelling that supplies one, at the database that needs it: {refused}"
  );
}

/// The presence list and the fills TOGETHER: the narrowing states which databases keep the column
/// and the fills answer the ones it left out, and the pair is accepted.
#[test]
fn a_presence_list_beside_its_fills_is_accepted() {
  let model = model(syn::parse_quote! {
    #[ingraph(sqlite, postgres)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, sqlite, postgres(skip(default = "no_frame")))]
      frame: u32,
    }
  });

  let persist = model.persist.as_ref().expect("a record stores rows");
  let postgres = persist
    .backends
    .iter()
    .find(|backend| backend.kind() == ingraph_codegen::prelude::DatabaseKind::Postgres)
    .expect("the database is declared");
  assert_eq!(
    postgres.storage().fills.len(),
    1,
    "the database the list left out is filled"
  );
}

/// **AN ADDRESS STEP IS EVERY DATABASE'S**, so a narrowing word on the id is refused by name.
#[test]
fn a_narrowed_address_step_is_refused() {
  let refused = refusals(syn::parse_quote! {
    #[ingraph(sqlite, postgres)]
    struct Shot {
      #[ingraph(id, sqlite)]
      id: u32,
      #[ingraph(scalar)]
      frame: u32,
    }
  });

  assert!(
    refused.contains("An address step is a column of every database the record declares"),
    "the all-databases-present law is named: {refused}"
  );
  assert!(
    refused.contains("a fill there would INVENT an identity"),
    "and the reason is identity rather than layout: {refused}"
  );
}

/// Naming EVERY database the record declares takes nothing away, so it is the restatement it is.
#[test]
fn a_presence_list_naming_every_declared_database_is_a_restatement() {
  model(syn::parse_quote! {
    #[ingraph(sqlite, postgres)]
    struct Shot {
      #[ingraph(id)]
      id: u32,
      #[ingraph(scalar, sqlite, postgres)]
      frame: u32,
    }
  });
}