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
//! The construction grammar's PASS: every word reaching the model, and every rule about a
//! declaration's shape refusing.
//!
//! What is NOT here is anything about the remote's own API. A `finish` naming no method, an `entry`
//! of the wrong arity and a seat that will not take what it is handed are all discharged by the
//! compiler at the call the emission writes — see `tests/construction_protocols.rs`, which compiles
//! six real protocols, and `tests/refusals/`, which pins the sentences these rules carry when a real
//! declaration breaks them.

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

/// The pass under test, over a declaration written the way the derive took one.
fn parse(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
  let (host, arguments) = crate::host::split(input);
  crate::parse::parse(&host, &arguments, NodeKind::Entity)
}

/// The model of a declaration that is expected to be accepted.
fn model(input: &syn::DeriveInput) -> RecordModel {
  parse(input).expect("an accepted declaration")
}

/// Every refusal the pass reported, JOINED — one compile reports every rule it can still see.
fn refusals(input: &syn::DeriveInput) -> String {
  parse(input)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// The field named `name`, in the settled model.
fn field<'a>(model: &'a RecordModel, name: &str) -> &'a Field {
  model
    .fields
    .iter()
    .find(|field| field.column.source == name)
    .expect("a declared field")
}

/// A wrapping declaration whose `builder(…)` words are whatever a case writes.
///
/// One shape for every case, so a difference between two assertions is the words and never the
/// declaration around them.
macro_rules! wrapping {
  ($($container:tt)*) => {
    syn::parse_quote! {
      #[ingraph(remote = "upstream::Frame" $($container)*)]
      pub struct Frame {
        #[ingraph(id)]
        id: i64,
        #[ingraph(copy)]
        width: u32,
        #[ingraph(optional)]
        note: Option<u8>,
      }
    }
  };
}

/// **THE DEFAULT LAW IS THE ABSENT WORD'S MEANING.** A declaration that writes nothing carries the
/// protocol the rewrite mode always had — which is what makes every word below an override rather
/// than a mode.
#[test]
fn a_declaration_that_describes_nothing_carries_the_default_protocol() {
  let m = model(&wrapping!());
  assert_eq!(m.construction, Construction::default());
  assert_eq!(m.construction.entry, "new");
  assert!(m.construction.finish.is_none(), "and it is a constructor");
  assert!(!m.construction.fallible);
  for name in ["id", "width", "note"] {
    assert_eq!(
      field(&m, name).build,
      Build::default(),
      "`{name}` took the seat the law gives it"
    );
  }
}

#[test]
fn the_container_words_reach_the_model() {
  let m = model(&wrapping!(, builder(
    entry = "builder",
    finish = "build",
    fallible,
    on(u32, into),
    call(with = "with_unit", value = "Unit::Pixels"),
    call(with = "with_scale", value = "2"),
  )));
  assert_eq!(m.construction.entry, "builder");
  assert_eq!(
    m.construction.finish.as_ref().expect("a closing call"),
    "build"
  );
  assert!(m.construction.fallible);
  assert!(m.construction.blankets(&syn::parse_quote!(u32)));
  assert!(!m.construction.blankets(&syn::parse_quote!(u64)));
  let methods: Vec<String> = m
    .construction
    .calls
    .iter()
    .map(|call| call.method.to_string())
    .collect();
  assert_eq!(
    methods,
    ["with_unit", "with_scale"],
    "an injection is repeatable, and the order is the declaration's"
  );
}

/// A blanket is keyed by a TYPE and not by a path, which is the whole reason its entry is read from
/// raw tokens rather than as nested meta.
#[test]
fn a_blanket_is_keyed_by_a_type_a_meta_path_could_not_hold() {
  let m = model(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame", builder(on(Option<u8>, into)))]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(optional)]
      note: Option<u8>,
    }
  });
  assert!(m.construction.blankets(&syn::parse_quote!(Option<u8>)));
}

#[test]
fn the_seat_words_reach_the_field() {
  let m = model(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame", builder(finish = "build"))]
    pub struct Frame {
      #[ingraph(id, builder(arg))]
      id: i64,
      #[ingraph(copy, builder(finish_arg))]
      width: u32,
      #[ingraph(copy, builder(with))]
      weight: u8,
      #[ingraph(copy, builder(with = "maybe_depth"))]
      depth: u8,
      #[ingraph(copy, builder(set))]
      tint: u8,
      #[ingraph(copy, builder(set = "put_gamma"))]
      gamma: u8,
    }
  });
  assert_eq!(field(&m, "id").build.seat, Some(Seat::Arg));
  assert_eq!(field(&m, "width").build.seat, Some(Seat::FinishArg));
  assert_eq!(field(&m, "weight").build.seat, Some(Seat::With(None)));
  assert_eq!(
    field(&m, "depth").build.seat,
    Some(Seat::With(Some(syn::parse_quote!(maybe_depth))))
  );
  assert_eq!(field(&m, "tint").build.seat, Some(Seat::Set(None)));
  assert_eq!(
    field(&m, "gamma").build.seat,
    Some(Seat::Set(Some(syn::parse_quote!(put_gamma))))
  );
}

#[test]
fn the_crossing_words_reach_the_field() {
  let m = model(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(into))]
      width: u32,
      #[ingraph(copy, builder(map = "narrow::width"))]
      height: u32,
      #[ingraph(optional, builder(option))]
      note: Option<u8>,
      #[ingraph(copy, builder(skip))]
      cached: u8,
    }
  });
  assert_eq!(field(&m, "width").build.cross, Some(Cross::Into));
  assert_eq!(
    field(&m, "height").build.cross,
    Some(Cross::Map(syn::parse_quote!(narrow::width)))
  );
  assert!(field(&m, "note").build.option);
  assert!(field(&m, "cached").build.skip);
  assert!(!field(&m, "width").build.skip, "and nothing else moved");
}

/// **A WRAPPER'S WORD.** Both seats are refused where nothing is wrapped, and the sentence names
/// the mode that serves them rather than the word that was wrong.
#[test]
fn a_construction_word_is_refused_where_nothing_is_wrapped() {
  let reported = refusals(&syn::parse_quote! {
    #[ingraph(builder(entry = "from_parts"))]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(arg))]
      width: u32,
    }
  });
  assert_eq!(
    reported
      .matches("describes how a type SOMEBODY ELSE declared is built")
      .count(),
    2,
    "the declaration's word and the field's word are each refused: {reported}"
  );
  assert!(reported.contains("`remote = \"…\"`"));
}

/// **A `finish_arg` NEEDS A `finish`** — checked at the assembly, because neither half sees the
/// other where it is written.
#[test]
fn a_finish_argument_with_no_closing_call_is_refused_by_name() {
  let reported = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(finish_arg))]
      width: u32,
    }
  });
  assert!(
    reported.contains("`width` in the argument list of the protocol's CLOSING call"),
    "{reported}"
  );
  assert!(reported.contains("builder(finish = \"…\")"));
}

/// **ONE COLUMN FILLS ONE SEAT**, and the refusal names the pair rather than the second word alone.
#[test]
fn two_seat_words_on_one_field_are_refused_naming_both() {
  for (first, second) in [
    ("arg", "with"),
    ("with", "set"),
    ("arg", "set"),
    ("arg", "finish_arg"),
  ] {
    let first: syn::Ident = syn::parse_str(first).expect("a word");
    let second: syn::Ident = syn::parse_str(second).expect("a word");
    let reported = refusals(&syn::parse_quote! {
      #[ingraph(remote = "upstream::Frame", builder(finish = "build"))]
      pub struct Frame {
        #[ingraph(id)]
        id: i64,
        #[ingraph(copy, builder(#first, #second))]
        width: u32,
      }
    });
    assert!(
      reported.contains(&format!(
        "`{first}` and `{second}` are both declared on this field"
      )),
      "{reported}"
    );
  }
}

#[test]
fn a_skipped_column_may_not_also_name_a_seat() {
  let reported = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(skip, arg))]
      width: u32,
    }
  });
  assert!(
    reported.contains("`skip` says this column is NOT handed back"),
    "{reported}"
  );
}

#[test]
fn a_call_that_names_no_method_or_hands_over_nothing_is_refused() {
  let missing_value = refusals(&wrapping!(, builder(call(with = "with_unit"))));
  assert!(
    missing_value.contains("names a method and hands it nothing"),
    "{missing_value}"
  );

  let missing_method = refusals(&wrapping!(, builder(call(value = "1"))));
  assert!(
    missing_method.contains("supplies a value and names no method"),
    "{missing_method}"
  );

  let bare = refusals(&wrapping!(, builder(call)));
  assert!(
    bare.contains("`call(…)` is a link this declaration supplies"),
    "{bare}"
  );
}

/// `option` is a sentence about a column with an empty state, and `optional` is what declares one —
/// never the field's spelled type, per the standing rule about what a macro can see.
#[test]
fn the_whole_option_word_is_refused_on_a_column_with_no_empty_state() {
  let reported = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(option))]
      width: Option<u32>,
    }
  });
  assert!(
    reported.contains("this column has no empty state to take"),
    "{reported}"
  );
}

#[test]
fn a_column_crosses_one_way() {
  let reported = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(into, map = "narrow::width"))]
      width: u32,
    }
  });
  assert!(reported.contains("a column crosses ONE way"), "{reported}");
}

#[test]
fn a_pinned_target_and_a_crossing_inside_an_option_are_refused_together() {
  let reported = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(optional, builder(into, option))]
      note: Option<u8>,
    }
  });
  assert!(
    reported.contains("`into` pins the crossing's target"),
    "{reported}"
  );
}

/// `arg` and `finish_arg` name a POSITION, and a position has no name to be given.
#[test]
fn a_position_word_takes_no_name() {
  let named_position = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(arg = "first"))]
      width: u32,
    }
  });
  assert!(
    named_position.contains("names a POSITION in an argument list"),
    "{named_position}"
  );
}

#[test]
fn every_word_is_declared_once() {
  for (words, sentence) in [
    (
      "entry = \"a\", entry = \"b\"",
      "`entry` is declared more than once",
    ),
    (
      "finish = \"a\", finish = \"b\"",
      "`finish` is declared more than once",
    ),
    (
      "fallible, fallible",
      "`fallible` is declared more than once",
    ),
  ] {
    let words: proc_macro2::TokenStream = syn::parse_str(words).expect("the words");
    let reported = refusals(&syn::parse_quote! {
      #[ingraph(remote = "upstream::Frame", builder(#words))]
      pub struct Frame {
        #[ingraph(id)]
        id: i64,
      }
    });
    assert!(reported.contains(sentence), "{reported}");
  }
}

/// The block is declared once at either seat, and the two sentences say which seat they are about.
#[test]
fn the_block_itself_is_declared_once_per_seat() {
  let container = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame", builder(entry = "a"), builder(entry = "b"))]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
    }
  });
  assert!(
    container.contains("one declaration describes one protocol"),
    "{container}"
  );

  let seat = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(arg), builder(with))]
      width: u32,
    }
  });
  assert!(seat.contains("one column fills one seat"), "{seat}");
}

/// An unknown entry is refused at BOTH seats, and each sentence points at the other seat rather
/// than leaving an author to guess which level their word belongs to.
#[test]
fn an_unknown_entry_names_the_seat_its_word_belongs_to() {
  let container = refusals(&wrapping!(, builder(arg)));
  assert!(
    container.contains(
      "The per-column words — `arg`, `with`, `set`, `map` and the rest — are \
                        written in a `builder(…)` on the FIELD"
    ),
    "{container}"
  );

  let seat = refusals(&syn::parse_quote! {
    #[ingraph(remote = "upstream::Frame")]
    pub struct Frame {
      #[ingraph(id)]
      id: i64,
      #[ingraph(copy, builder(entry = "new"))]
      width: u32,
    }
  });
  assert!(
    seat.contains("are facts about the whole protocol and are written on the DECLARATION"),
    "{seat}"
  );
}

/// A blanket states a CROSSING, and `into` is the only one a per-type rule can carry.
#[test]
fn a_blanket_takes_a_type_and_the_one_word() {
  for written in ["on(u32)", "on(u32, map)", "on"] {
    let words: proc_macro2::TokenStream = syn::parse_str(written).expect("the words");
    let reported = refusals(&syn::parse_quote! {
      #[ingraph(remote = "upstream::Frame", builder(#words))]
      pub struct Frame {
        #[ingraph(id)]
        id: i64,
      }
    });
    assert!(
      reported.contains("is written `on(<Type>, into)`"),
      "`{written}`: {reported}"
    );
  }
}