1
//! Deriving `NetdocParseable`
2

            
3
use super::*;
4

            
5
//==================== Common definitions used by many of the macros ====================
6

            
7
/// Helper to implemnet `dtrace!` inside `NetdocParseable` derive-deftly macro.
8
#[doc(hidden)]
9
#[allow(clippy::print_stderr)]
10
108
pub fn netdoc_parseable_derive_debug(ttype: &str, msg: &str, vals: &[&dyn Debug]) {
11
    // We use `eprintln!` so that the output is captured as expected under cargo test.
12
    // We buffer the output into a string so that it a;ll appears at once,
13
    // rather than possibly being interleaved with similar output for other types.
14
108
    let mut out = String::new();
15
108
    (|| {
16
108
        write!(out, "netdoc {ttype} parse: {msg}")?;
17
198
        for val in vals {
18
198
            write!(out, ", {val:?}")?;
19
        }
20
108
        writeln!(out)
21
    })()
22
108
    .expect("write to string failed");
23

            
24
108
    eprint!("{out}");
25
108
}
26

            
27
define_derive_deftly_module! {
28
    /// Common definitions for all parsing macros.
29
    NetdocParseAnyCommon beta_deftly:
30

            
31
    // Convenience alias for our prelude
32
    ${define P { $crate::parse2::internal_prelude }}
33

            
34
    // Defines the `dtrace` macro.
35
    ${define DEFINE_DTRACE {
36
        #[allow(unused_macros)]
37
        macro_rules! dtrace { { $$msg:literal $$(, $$val:expr )* $$(,)? } => {
38
          ${if tmeta(netdoc(debug)) {
39
              $P::netdoc_parseable_derive_debug(
40
                  ${concat $ttype},
41
                  $$msg,
42
                  &[ $$( &&$$val as _, )* ],
43
              )
44
          }}
45
        }}
46
    }}
47
}
48

            
49
define_derive_deftly_module! {
50
    /// Common definitions for `NetdocParseable`, `NetdocParseableFields`,
51
    /// and `NetdocParseableSignatures`
52
    ///
53
    /// The including macro is expected to define:
54
    ///
55
    ///  * **`ITEM_STREAM`**: The input `ItemStream<'_>`.
56
    ///
57
    ///  * **`THIS_ITEM`**: consumes the next item and evaluates to it as an `UnparsedItem`.
58
    ///    See the definition in `NetdocParseable`.
59
    ///
60
    ///  * **`F_ACCUMULATE_VAR`** the variable or field into which to accumulate
61
    ///    normal items for this field.  Must be of type `&mut $F_ACCUMULATE_TYPE`.
62
    ///
63
    /// Importer must also import `NetdocSomeItemsDeriveCommon` and `NetdocDeriveAnyCommon`.
64
    NetdocSomeItemsParseableCommon beta_deftly:
65

            
66
    // The effective field type for parsing.
67
    //
68
    // Handles #[deftly(netdoc(default))], in which case we parse as if the field was Option,
69
    // and substitute in the default at the end.
70
    //
71
    ${define F_EFFECTIVE_TYPE {
72
        ${if all(fmeta(netdoc(default))) {
73
            Option::<$ftype>
74
        } else {
75
            $ftype
76
        }}
77
    }}
78

            
79
    // Provide `$<selector_ $fname>` for every (suitable) field.
80
    ${define ITEM_SET_SELECTORS {
81
        $(
82
          ${when not(any(F_FLATTEN, F_SKIP))}
83

            
84
          // See `mod multiplicity`.
85
        ${if not(all(F_INTRO, fmeta(netdoc(with)))) {
86
          // If the intro it has `with`, we don't check its trait impl, and this ends up unused
87
          let $<selector_ $fname> = $F_SELECTOR_VALUE;
88
        }}
89
        )
90
    }}
91
    // The item set selector for this field.
92
    // We must provide this, rather than expanding $<selector_ $fname> at the use sites,
93
    // because the identifier `selector_` has different macro_rules hygiene here vs there!
94
    // TODO derive-deftly#130
95
7256
    ${define F_SELECTOR $<selector_ $fname>}
96
    // The selector value for this field.  Used where we don't want to bind a selector
97
    // for every field with $ITEM_SET_SELECTORS (and within $ITEM_SET_SELECTORS).
98
    ${define F_SELECTOR_VALUE {( MultiplicitySelector::<$F_EFFECTIVE_TYPE>::default() )}}
99
    // Check that every field type implements the necessary trait.
100
    ${define CHECK_FIELD_TYPES_PARSEABLE {
101
        $(
102
          ${when not(any(F_FLATTEN, F_SKIP))}
103

            
104
          // Expands to `selector_FIELD.check_SOMETHING();`
105
          //
106
          // If the relevant trait isn't implemented, rustc reports the error by
107
          // pointing at the `check-something` call.  We re-span that identifier
108
          // to point to the field name, so that's where the error is reported.
109
          //
110
          // Without this, we just get a report that `item` doesn't implement the required
111
          // trait - but `item` is a local variable here, so the error points into the macro
112
        ${if not(all(any(F_INTRO, F_NORMAL), fmeta(netdoc(with)))) {
113
          $<selector_ $fname> . ${paste_spanned $fname ${select1
114
                  any(F_INTRO, F_NORMAL){
115
                      // For the intro item, this is not completely precise, because the
116
                      // it will allow Option<> and Vec<> which aren't allowed there.
117
                      ${if
118
                        fmeta(netdoc(single_arg)) { check_item_argument_parseable }
119
                        else { check_item_value_parseable }
120
                      }
121
                  }
122
                  F_SIGNATURE { check_signature_item_parseable }
123
                  F_SUBDOC    { check_subdoc_parseable         }
124
          }} (
125
              ${if F_SIGNATURE { sig_hashes, }}
126
          );
127
        }}
128
        )
129
    }}
130

            
131
    // Convert the UnparsedItem (in `item` to the value (to accumulate).
132
    // Expands to an expression.
133
    ${define ITEM_VALUE_FROM_UNPARSED {
134
        ${if fmeta(netdoc(with)) {
135
          ${fmeta(netdoc(with)) as path}
136
              ::${paste_spanned $fname from_unparsed}
137
              (item)?
138
        } else if fmeta(netdoc(single_arg)) { {
139
          let item = ItemValueParseable::from_unparsed(item)?;
140
          let (item,) = item;
141
          item
142
        } } else {
143
          ItemValueParseable::from_unparsed(item)?
144
        }}
145
    }}
146

            
147
    // Type into which we accumulate value(s) of this field
148
    ${define F_ACCUMULATE_TYPE {
149
        ${if F_FLATTEN {
150
            <$ftype as $P::NetdocParseableFields>::Accumulator
151
        } else {
152
            Option::<$F_EFFECTIVE_TYPE>
153
        }
154
    }}}
155

            
156
    // Parse the intro item and bind `$fpatname` accumulator for each field.
157
    //
158
    // For the intro item, parse it and bind it to $fpatname.
159
    //
160
    // For other items, set up a mutable $fpatname, initialised to `default()` (normally None).
161
    ${define INIT_ACCUMULATE_VARS {
162
        $( ${select1 F_INTRO {
163

            
164
          let item = input.next_item()?.ok_or(EP::EmptyDocument)?;
165
          dtrace!("intro", item);
166
          if !Self::is_intro_item_keyword(item.keyword()) {
167
              Err(EP::WrongDocumentType)?;
168
          }
169
          let $fpatname: $ftype = $ITEM_VALUE_FROM_UNPARSED;
170

            
171
        } F_SKIP {
172

            
173
        } else {
174

            
175
          let mut $fpatname = $F_ACCUMULATE_TYPE::default();
176

            
177
        }})
178
    }}
179

            
180
    // Accumulates `item` (which must be `ItemSetMethods::Each`) into `$F_ACCUMULATE_VAR`
181
    ${define ACCUMULATE_ITEM_VALUE { {
182
        dtrace!($"accumulating $fname", $F_SELECTOR.item_set_debug());
183
        $F_SELECTOR.${paste_spanned $fname accumulate}($F_ACCUMULATE_VAR, item)?;
184
    } }}
185

            
186
    // Handle a nonstructural field, parsing and accumulating its value
187
    //
188
    // Looks at `kw` for the keyword.
189
    //
190
    // Expands to a series of `if ... { ... } else`.
191
    // The use site must provide (maybe further arms) and a fallback block!
192
    //
193
    // If the item is the intro item for this document, evaluates `break` -
194
    // so if `f_INTRO` is not trivially false, must be expanded within a field loop.
195
    ${define NONSTRUCTURAL_ACCUMULATE_ELSE {
196
        ${for fields {
197
          ${when not(any(F_FLATTEN, F_SUBDOC, F_SKIP))}
198

            
199
          if kw == $F_KEYWORD {
200
            ${select1
201
              F_NORMAL {
202
                let item = $THIS_ITEM;
203
                dtrace!("is normal", item);
204
                let item = $ITEM_VALUE_FROM_UNPARSED;
205
                $ACCUMULATE_ITEM_VALUE
206
              }
207
              F_SIGNATURE {
208
                let hash_inputs = input
209
                      .peek_signature_hash_inputs(signed_doc_body)?
210
                      .expect("not eof, we peeked kw");
211

            
212
                let item = $THIS_ITEM;
213
                dtrace!("is signature", item);
214
                let item = SignatureItemParseable::from_unparsed_and_body(
215
                    item,
216
                    &hash_inputs,
217
                    AsMut::as_mut(sig_hashes),
218
                )?;
219
                $ACCUMULATE_ITEM_VALUE
220
              }
221
              F_INTRO {
222
                dtrace!("is intro", kw);
223
                break;
224
              } // start of next similar document
225
            }
226
          } else
227
        }}
228
        ${for fields {
229
          ${when F_FLATTEN}
230

            
231
          if <$ftype>::is_item_keyword(kw) {
232
              dtrace!(${concat "is flatten in " $fname}, kw);
233
              let item = $THIS_ITEM;
234
              <$ftype as NetdocParseableFields>::accumulate_item($F_ACCUMULATE_VAR, item)?;
235
          } else
236
        }}
237
    }}
238

            
239
    // Completes a document
240
    //
241
    // The fields accumulated so far must be in `$fpatname` (as a value, not a ref,
242
    // and therefore not in $F_ACCUMULATE_VAR).
243
    //
244
    // Expands to code which resolves the fields, and ends with `Ok(document value)`.
245
    ${define FINISH_RESOLVE {
246
        ${for fields {
247
            ${select1
248
              F_INTRO {}
249
              any(F_NORMAL, F_SIGNATURE) {
250
                  let $fpatname = $F_SELECTOR.finish($fpatname, $F_KEYWORD_REPORT)?;
251
              }
252
              F_FLATTEN {
253
                  let $fpatname = <$ftype as NetdocParseableFields>::finish(
254
                      $fpatname,
255
                      $ITEM_STREAM,
256
                  )?;
257
              }
258
              F_SUBDOC {
259
                  let $fpatname = $F_SELECTOR.finish_subdoc($fpatname)?;
260
              }
261
              F_SKIP {
262
                  #[allow(non_snake_case)]
263
                  let $fpatname = Default::default();
264
              }
265
            }
266
        }}
267
        $(
268
            ${when not(any(F_INTRO, F_SKIP))}
269
            // These conditions are mirrored in NetdocSomeItemsEncodableCommon,
270
            // which is supposed to recognise netdoc(default) precisely when we do.
271
          ${if fmeta(netdoc(default)) {
272
            let $fpatname = Option::unwrap_or_default($fpatname);
273
          }}
274
        )
275
        Ok($vpat)
276
    }}
277
}
278

            
279
//==================== Main whole document parsing impl ====================
280
//
281
// deftly module ` NetdocParseable`:
282
//
283
//   * IMPL_NETDOC_PARSEABLE expanding to `impl NetdocParseable { ... }`
284
//
285
// Much of the heavy lifting is done in the NetdocSomeItemsParseableCommon deftly module.
286

            
287
define_derive_deftly_module! {
288
    /// Provides `IMPL_NETDOC_PARSEABLE` which impls `NetdocParseable`
289
    ///
290
    /// Used by the `NetdocParseable` and `NetdocParseableUnverified` derives.
291
    NetdocParseable beta_deftly:
292

            
293
    use NetdocDeriveAnyCommon;
294
    use NetdocParseAnyCommon;
295
    use NetdocEntireDeriveCommon;
296
    use NetdocSomeItemsDeriveCommon;
297
    use NetdocSomeItemsParseableCommon;
298

            
299
    ${define ITEM_STREAM input}
300
    ${define F_ACCUMULATE_VAR { (&mut $fpatname) }}
301

            
302
  ${define IMPL_NETDOC_PARSEABLE {
303
    impl<$tgens> $P::NetdocParseable for $NETDOC_PARSEABLE_TTYPE {
304
636
        fn doctype_for_error() -> &'static str {
305
            ${tmeta(netdoc(doctype_for_error)) as expr,
306
              default ${concat ${for fields { ${when F_INTRO} $F_KEYWORD_STR }}}}
307
        }
308

            
309
82000
        fn is_intro_item_keyword(kw: $P::KeywordRef<'_>) -> bool {
310
            use $P::*;
311

            
312
            ${for fields {
313
                ${when F_INTRO}
314
                ${loop_exactly_1 "internal error, somehow not exactly one intro item!"}
315
                kw == $F_KEYWORD
316
            }}
317
        }
318

            
319
300
        fn is_structural_keyword(kw: $P::KeywordRef<'_>) -> Option<$P::IsStructural> {
320
            #[allow(unused_imports)] // not used if there are no subdocs
321
            use $P::*;
322

            
323
            if Self::is_intro_item_keyword(kw) {
324
                return Some(IsStructural)
325
            }
326

            
327
            ${for fields {
328
                ${when F_SUBDOC}
329
                if let y @ Some(_) = $F_SELECTOR_VALUE.is_structural_keyword(kw) {
330
                    return y;
331
                }
332
            }}
333

            
334
            None
335
        }
336

            
337
        //##### main parsing function #####
338

            
339
        #[allow(clippy::redundant_locals)] // let item = $THIS_ITEM, which might be item
340
1384
        fn from_items<'s>(
341
1384
            input: &mut $P::ItemStream<'s>,
342
1384
            outer_stop: $P::stop_at!(),
343
1384
        ) -> $P::Result<Self, $P::ErrorProblem> {
344
            use $P::*;
345
            $DEFINE_DTRACE
346
            $FIELD_ORDERING_CHECK
347

            
348
            //----- prepare item set selectors for every field -----
349
            $ITEM_SET_SELECTORS
350
            $CHECK_FIELD_TYPES_PARSEABLE
351

            
352
            // Is this an intro item keyword ?
353
            //
354
            // Expands to an appropriate `is_intro_item_keyword` method invocation,
355
            // but *without arguments*.  So, something a bit like an expression of type
356
            //    fn(KeywordRef) -> bool
357
            ${define F_SUBDOC_IS_INTRO_ITEM_KEYWORD {
358
                ${if not(F_SUBDOC) { ${error "internal-error: subdoc kw, but not subdoc field"} }}
359
7256
                $F_SELECTOR.is_intro_item_keyword
360
            }}
361

            
362
            //----- Helper fragments for parsing individual pieces of the document -----
363

            
364
            // Peeks a keyword, and returns it but only if it's part of this (sub)doc.
365
            // Return `None` if it was in outer_stop
366
9538
            let peek_keyword = |input: &mut ItemStream<'s>| -> Result<Option<KeywordRef<'s>>, EP> {
367
                let Some(kw) = input.peek_keyword()? else {
368
                    dtrace!("stopping, because EOF");
369
                    return Ok(None)
370
                };
371
                if outer_stop.stop_at(kw) {
372
                    dtrace!("stopping, because peeked", kw);
373
                    return Ok(None)
374
                }
375
                Ok(Some(kw))
376
            };
377

            
378
            // Returns the actual item as an UnparsedItem, committing to consuming it.
379
            // Can panic if called without previous `peek_keyword`.
380
            ${define THIS_ITEM  {
381
                input.next_item()?.expect("peeked")
382
            }}
383

            
384
            //----- keyword classification closures -----
385

            
386
            // Is this a keyword for one of our sub-documents?
387
            let is_subdoc_kw = ${for fields {
388
                ${when F_SUBDOC}
389
                StopAt(|kw: KeywordRef<'_>| $F_SUBDOC_IS_INTRO_ITEM_KEYWORD(kw)) |
390
              }}
391
                StopAt(false)
392
            ;
393
            // Is this a keyword for one of our parents or sub-documents?
394
            let inner_stop = outer_stop | is_subdoc_kw;
395

            
396
            //========== actual parsing ==========
397

            
398
            // For each parsing loop/section, where we aren't looking for precisely one thing,
399
            // we should explicitly decide what to do with each of:
400
            //   - F_INTRO - intro item for this document (maybe next instance in parent)
401
            //   - F_NORMAL - normal items
402
            //   - subdocuments, is_subdoc_kw and F_SUBDOC
403
            //   - our parent's structural keywords, outer_stop
404
            //     (this includes signature items for the signed version of this doc)
405
            // 5 cases in all.
406

            
407
            //----- Parse the intro item, and introduce bindings for the other items. -----
408

            
409
            dtrace!("looking for intro item");
410
            $INIT_ACCUMULATE_VARS
411

            
412
            //----- Parse the normal items -----
413
            dtrace!("looking for normal items");
414

            
415
            while let Some(kw) = peek_keyword(input)? {
416
                dtrace!("for normal, peeked", kw);
417
                if inner_stop.stop_at(kw) {
418
                    dtrace!("is inner stop", kw);
419
                    break;
420
                };
421

            
422
                $NONSTRUCTURAL_ACCUMULATE_ELSE
423
                {
424
                    dtrace!("is unknown (in normal)");
425
                    let _: UnparsedItem = $THIS_ITEM;
426
                }
427
            }
428

            
429
            //----- Parse the subdocs, in order -----
430
            dtrace!("looking for subdocs");
431

            
432
          ${for fields {
433
            ${when F_SUBDOC}
434
            dtrace!("looking for subdoc", $F_KEYWORD_REPORT);
435

            
436
            loop {
437
                let Some(kw) = peek_keyword(input)? else { break };
438
                dtrace!("for subdoc, peek", kw);
439

            
440
                if !$F_SUBDOC_IS_INTRO_ITEM_KEYWORD(kw) {
441
                    dtrace!("is not this subdoc", kw);
442
                    break;
443
                };
444

            
445
                $F_SELECTOR.can_accumulate(&mut $fpatname)?;
446

            
447
                dtrace!("is this subdoc", kw);
448
                let item = NetdocParseable::from_items(input, inner_stop);
449
                dtrace!("parsed this subdoc", item.as_ref().map(|_| ()));
450
                let item = item?;
451

            
452
                $ACCUMULATE_ITEM_VALUE
453
            }
454
          }}
455

            
456
            // Resolve all the fields
457
            dtrace!("reached end, resolving");
458

            
459
            $FINISH_RESOLVE_PARSEABLE
460
        }
461
    }
462
  }}
463
}
464

            
465
//==================== NetdocParseable user-facing derive macro ====================
466
//
467
// deftly template `NetdocParseable`:
468
//
469
//  * main entrypoint for deriving the `NetdocParseable` trait
470
//  * docs for the meta attributes we support during document parsing
471
//
472
// The actual implementation is in  the `NetdocParseable` deftly module, above.
473

            
474
define_derive_deftly! {
475
    use NetdocParseable;
476

            
477
    /// Derive [`NetdocParseable`] for a document (or sub-document)
478
    ///
479
    // NB there is very similar wording in the NetdocEncodable derive docs.
480
    // If editing any of this derive's documentation, considering editing that too.
481
    //
482
    /// ### Expected input structure
483
    ///
484
    /// Should be applied named-field struct, where each field is
485
    /// an Item which may appear in the document,
486
    /// or a sub-document.
487
    ///
488
    /// The first field will be the document's intro Item.
489
    /// The expected Keyword for each Item will be kebab-case of the field name.
490
    ///
491
    /// ### Field type
492
    ///
493
    /// Each field must be
494
    ///  * `impl `[`ItemValueParseable`] for an "exactly once" field,
495
    ///  * `Vec<T: ItemValueParseable>` for "zero or more", or
496
    ///  * `BTreeSet<T: ItemValueParseable + Ord>`, or
497
    ///  * `Option<T: ItemValueParseable>` for "zero or one".
498
    ///
499
    /// We don't directly support "at least once":
500
    /// the parsed network document doesn't imply the invariant
501
    /// that at least one such item was present.
502
    // We could invent a `NonemptyVec` or something for this.
503
    ///
504
    /// (This is implemented via types in the [`multiplicity`] module,
505
    /// specifically [`ItemSetSelector`].)
506
    ///
507
    /// ### Signed documents
508
    ///
509
    /// To handle signed documents:
510
    ///
511
    ///  * Define `struct Foo`, containing only the content, not the signatures.
512
    ///    Derive [`NetdocParseableUnverified`](crate::derive_deftly_template_NetdocParseableUnverified).
513
    ///  * Define `struct FooSignatures`, containing only the signatures.
514
    ///    Derive `NetdocParseableSignatures`.
515
    ///
516
    ///  * Implement a suitable `FooUnverified::verify`.
517
    ///    See [`NetdocParseableUnverified`](crate::derive_deftly_template_NetdocParseableUnverified)
518
    ///    for guidance.
519
    ///
520
    /// Don't mix signature items with non-signature items in the same struct.
521
    /// (This wouldn't compile, because the field type would implement the wrong trait.)
522
    ///
523
    /// ### Top-level attributes:
524
    ///
525
    /// * **`#[deftly(netdoc(doctype_for_error = EXPRESSION))]`**:
526
    ///
527
    ///   Specifies the value to be returned from
528
    ///   [`NetdocParseable::doctype_for_error`].
529
    ///
530
    ///   Note, must be an expression, so for a literal, nested `""` are needed.
531
    ///
532
    ///   The default is the intro item keyword.
533
    ///
534
    /// * **`#[deftly(netdoc(debug))]`**:
535
    ///
536
    ///   The generated implementation will generate copious debug output
537
    ///   to the program's stderr when it is run.
538
    ///   Do not enable in production!
539
    ///
540
    /// ### Field-level attributes:
541
    ///
542
    /// * **`#[deftly(netdoc(keyword = STR))]`**:
543
    ///
544
    ///   Use `STR` as the Keyword for this Item.
545
    ///
546
    /// * **`#[deftly(netdoc(single_arg))]`**:
547
    ///
548
    ///   The field type implements `ItemArgumentParseable`,
549
    ///   instead of `ItemValueParseable`,
550
    ///   and is parsed as if `(FIELD_TYPE,)` had been written.
551
    ///
552
    /// * **`#[deftly(netdoc(with = MODULE))]`**:
553
    ///
554
    ///   Instead of `ItemValueParseable`, the item is parsed with `MODULE::from_unparsed`,
555
    ///   which must have the same signature as [`ItemValueParseable::from_unparsed`].
556
    ///
557
    ///   (Not supported for sub-documents, signature items, or field collections.)
558
    ///
559
    /// * **`#[deftly(netdoc(default))]`**:
560
    ///
561
    ///   This field is optional ("at most once");
562
    ///   if not present, `FIELD_TYPE::default()` will be used.
563
    ///
564
    ///   This is an alternative to declaring the field type as `Option`
565
    ///   With `netdoc(default)`, the field value doesn't need unwrapping.
566
    ///   With `Option` it is possible to see if the field was provided.
567
    ///
568
    /// * **`#[deftly(netdoc(flatten))]`**:
569
    ///
570
    ///   This field is a struct containing further individual normal fields.
571
    ///   The Items for those individual fields can appear in *this*
572
    ///   outer document in any order, interspersed with other normal fields.
573
    ///
574
    ///   The field type must implement [`NetdocParseableFields`].
575
    ///
576
    /// * **`#[deftly(netdoc(skip))]`**:
577
    ///
578
    ///   This field doesn't really appear in the network document.
579
    ///   It won't be recognised during parsing.
580
    ///   Instead, `Default::default()` will be used for the field value.
581
    ///
582
    /// * **`#[deftly(netdoc(subdoc))]`**:
583
    ///
584
    ///   This field is a sub-document.
585
    ///   The value type `T` must implement [`NetdocParseable`]
586
    ///   *instead of* `ItemValueParseable`.
587
    ///
588
    ///   The field name is not used for parsging;
589
    ///   the sub-document's intro keyword is used instead.
590
    ///
591
    ///   Sub-documents are expected to appear after all normal items,
592
    ///   in the order presented in the struct definition.
593
    ///
594
    /// # Example
595
    ///
596
    /// ```
597
    /// use derive_deftly::Deftly;
598
    /// use tor_checkable::{Timebound, timed::TimerangeBound};
599
    /// use tor_netdoc::derive_deftly_template_AsMutSelf;
600
    /// use tor_netdoc::derive_deftly_template_NetdocParseableSignatures;
601
    /// use tor_netdoc::derive_deftly_template_NetdocParseableUnverified;
602
    /// use tor_netdoc::derive_deftly_template_ItemValueParseable;
603
    /// use tor_netdoc::parse2::{
604
    ///     parse_netdoc, ErrorProblem, ParseInput, VerifyFailed,
605
    ///     SignatureItemParseable, SignatureHashesAccumulator, SignatureHashInputs,
606
    /// };
607
    ///
608
    /// #[derive(Deftly, Debug, Clone)]
609
    /// #[derive_deftly(NetdocParseableUnverified)]
610
    /// pub struct NdThing {
611
    ///     pub thing_start: (),
612
    ///     pub value: (String,),
613
    /// }
614
    ///
615
    /// #[derive(Deftly, Debug, Clone)]
616
    /// #[derive_deftly(NetdocParseableSignatures)]
617
    /// #[deftly(netdoc(signatures(hashes_accu = UseLengthAsFoolishHash)))]
618
    /// pub struct NdThingSignatures {
619
    ///     pub signature: FoolishSignature,
620
    /// }
621
    ///
622
    /// #[derive(Deftly, Debug, Clone)]
623
    /// #[derive_deftly(ItemValueParseable)]
624
    /// #[deftly(netdoc(signature(hash_accu = UseLengthAsFoolishHash)))]
625
    /// pub struct FoolishSignature {
626
    ///     pub doc_len: usize,
627
    /// }
628
    ///
629
    /// #[derive(Deftly, Debug, Default, Clone)]
630
    /// #[derive_deftly(AsMutSelf)]
631
    /// pub struct UseLengthAsFoolishHash {
632
    ///     pub doc_len_actual_pretending_to_be_hash: Option<usize>,
633
    /// }
634
    /// impl SignatureHashesAccumulator for UseLengthAsFoolishHash {
635
    ///     fn update_from_netdoc_body(
636
    ///         &mut self,
637
    ///         document_body: &SignatureHashInputs<'_>,
638
    ///     ) -> Result<(), ErrorProblem> {
639
    ///         self
640
    ///             .doc_len_actual_pretending_to_be_hash
641
    ///             .get_or_insert_with(|| document_body.body().body().len());
642
    ///         Ok(())
643
    ///     }
644
    /// }
645
    ///
646
    /// let doc_text =
647
    /// r#"thing-start
648
    /// value something
649
    /// signature 28
650
    /// "#;
651
    ///
652
    /// impl NdThingUnverified {
653
    ///     pub fn verify_foolish(self) -> Result<TimerangeBound<NdThing>, VerifyFailed> {
654
    ///         // See docs for derive_deftly_template_NetdocParseableUnverified
655
    ///         // for how to write a verify function.
656
    ///
657
    ///         let sig = &self.sigs.sigs.signature;
658
    ///         let hash = self.sigs.hashes.doc_len_actual_pretending_to_be_hash
659
    ///             .as_ref().ok_or(VerifyFailed::Bug)?;
660
    ///         if sig.doc_len != *hash {
661
    ///             return Err(VerifyFailed::VerifyFailed);
662
    ///         }
663
    ///         let foolish_lack_of_validity_time_info = ..;
664
    ///         let body = TimerangeBound::new(self.body, foolish_lack_of_validity_time_info);
665
    ///         Ok(body)
666
    ///     }
667
    /// }
668
    ///
669
    /// let input = ParseInput::new(&doc_text, "<input>");
670
    /// let doc: NdThingUnverified = parse_netdoc(&input).unwrap();
671
    /// let doc = doc.verify_foolish().unwrap().dangerously_assume_timely();
672
    /// assert_eq!(doc.value.0, "something");
673
    /// ```
674
    export NetdocParseable for struct, meta_quoted rigorous, expect items, beta_deftly:
675

            
676
    ${define NETDOC_PARSEABLE_TTYPE { $ttype }}
677
    ${define FINISH_RESOLVE_PARSEABLE $FINISH_RESOLVE}
678

            
679
    $IMPL_NETDOC_PARSEABLE
680
}
681

            
682
//==================== NetdocParseableSignatures user-facing derive macro ====================
683
//
684
// deftly template `NetdocParseableSignatures`:
685
//
686
//  * entrypoint for deriving the `NetdocParseableSignatures` trait
687
//  * docs for the signatures-section-specific attributes
688
//  * implementation of that derive
689
//
690
// Much of the heavy lifting is done in the NetdocSomeItemsParseableCommon deftly module.
691

            
692
define_derive_deftly! {
693
    use NetdocDeriveAnyCommon;
694
    use NetdocParseAnyCommon;
695
    use NetdocSomeItemsDeriveCommon;
696
    use NetdocSomeItemsParseableCommon;
697

            
698
    /// Derive [`NetdocParseable`] for the signatures section of a network document
699
    ///
700
    /// This type is the signatures section of another document.
701
    /// Signature sections have no separate intro keyword:
702
    /// every field is structural and they are recognised in any order.
703
    ///
704
    /// This signatures sub-document will typically be included in a
705
    /// `FooUnverified` struct derived with
706
    /// [`NetdocParseableUnverified`](crate::derive_deftly_template_NetdocParseableUnverified),
707
    /// rather than included anywhere manually.
708
    ///
709
    /// ### Expected input structure
710
    ///
711
    /// Should be applied named-field struct, where each field
712
    /// implements [`SignatureItemParseable`],
713
    /// or is a `SignatureItemParseable` in `Vec` or `BTreeSet` or `Option`.
714
    ///
715
    /// ### Attributes
716
    ///
717
    ///  * The following top-level attributes are supported:
718
    ///    `#[deftly(netdoc(debug))]`
719
    ///
720
    ///  * The following field-level attributes are supported:
721
    ///    `#[deftly(netdoc(keyword = STR))]`
722
    ///    `#[deftly(netdoc(default))]`
723
    ///    `#[deftly(netdoc(single_arg))]`
724
    ///    `#[deftly(netdoc(with = MODULE))]`
725
    ///    `#[deftly(netdoc(flatten))]`
726
    ///    `#[deftly(netdoc(skip))]`
727
    ///
728
    /// ### Signature item ordering, and signatures covering signatures
729
    ///
730
    /// The derived code does not impose any mutual ordering of signatures.
731
    /// If signatures are independent, hashing can be done with [`SignedDocumentBody`]
732
    /// (from [`SignatureHashInputs::body`]).
733
    ///
734
    /// In sane netdoc signature scheme, no signatures would cover other signatures,
735
    /// and there would be no ordering requirement on signatures on the same document.
736
    ///  A relying party would verify the signatures that they are proposing to rely on
737
    /// (which would generally include signatures for *one* algorithm, not several)
738
    /// and ignore the others.
739
    ///
740
    /// (Such a signature, which also does not include any of its own item encoding
741
    /// in its hash, is called Orderly.  See [SignedDocumentBody].)
742
    ///
743
    /// Unfortunately, many Tor netdocs have signature schemes
744
    /// which are not sane (by this definition).
745
    ///
746
    /// When signatures are specified to cover other signatures,
747
    /// the signature item implementation must contain ad-hoc code in
748
    /// [`SignatureItemParseable::from_unparsed_and_body`].
749
    /// to hash not only the body, but also the prior signatures.
750
    /// Methods on [`SignatureHashInputs`] are available to get
751
    /// the relevant parts of the input document text
752
    /// (eg, [`document_sofar`](SignatureHashInputs::document_sofar)).
753
    ///
754
    /// When the spec states a required ordering on signature items,
755
    /// this should be enforced by ad-hoc code in implementation(s) of
756
    /// `SignatureItemParseable`.
757
    /// The implementation should use
758
    /// [`HashAccu`](SignatureItemParseable::HashAccu)
759
    /// to store any necessary state.
760
    /// Usually, this can be achieved by using the same Rust struct for the
761
    /// `HashAccu` of each of the signature items:
762
    /// that will make the signature hashes computed so far, for items seen so far,
763
    /// visible to subsequent items;
764
    /// the subsequent items can check that the prior items filled in the hash,
765
    /// thus imposing an ordering.
766
    ///
767
    /// Alternatively, the ordering could be enforced in the user-supplied
768
    /// ad-hoc `verify` function(s) on `FooUUnverified`.
769
    ///
770
    /// Note that this enforcement should be done for protocol compliance
771
    /// and availability reasons, but is not a security issue.
772
    /// There is not a security risk from accepting documents some of whose signatures
773
    /// aren't covered by other signatures even though the protocol specifies they should be:
774
    /// relying parties *verify* the signatures but do not treat them as trusted data.
775
    /// So there is no engineered safeguard against failing to implement
776
    /// signature item ordering checks.
777
    export NetdocParseableSignatures for struct, meta_quoted rigorous, expect items, beta_deftly:
778

            
779
    ${defcond F_INTRO false}
780
    ${defcond F_SUBDOC false}
781
    ${defcond F_SIGNATURE true}
782

            
783
    // NetdocParseableSignatures::HashesAccu
784
    ${define SIGS_HASHES_ACCU_TYPE { ${tmeta(netdoc(signatures(hashes_accu))) as ty} }}
785

            
786
    ${define ITEM_STREAM input}
787
    ${define THIS_ITEM { input.next_item()?.expect("peeked") }}
788
    ${define F_ACCUMULATE_VAR { (&mut $fpatname) }}
789

            
790
    impl<$tgens> $P::NetdocParseableSignatures for $ttype {
791
        type HashesAccu = $SIGS_HASHES_ACCU_TYPE;
792

            
793
37642
        fn is_item_keyword(kw: $P::KeywordRef<'_>) -> bool {
794
            use $P::*;
795
            ${for fields {
796
37642
                kw == $F_KEYWORD ||
797
            }}
798
36254
                false
799
37642
        }
800

            
801
        #[allow(clippy::redundant_locals)] // let item = $THIS_ITEM, which might be item
802
958
        fn from_items<'s>(
803
958
            input: &mut $P::ItemStream<'s>,
804
958
            signed_doc_body: $P::SignedDocumentBody<'s>,
805
958
            sig_hashes: &mut $SIGS_HASHES_ACCU_TYPE,
806
958
            outer_stop: $P::stop_at!(),
807
958
        ) -> $P::Result<$ttype, $P::ErrorProblem> {
808
            use $P::*;
809
            $DEFINE_DTRACE
810

            
811
            //----- prepare item set selectors for every field -----
812
            $ITEM_SET_SELECTORS
813
            $CHECK_FIELD_TYPES_PARSEABLE
814
            $INIT_ACCUMULATE_VARS
815

            
816
            //----- parse the items -----
817
            dtrace!("looking for signature items");
818

            
819
1992
            while let Some(kw) = input.peek_keyword()? {
820
                dtrace!("for signatures, peeked", kw);
821
1904
                if outer_stop.stop_at(kw) {
822
                    dtrace!("is outer stop", kw);
823
870
                    break;
824
1034
                };
825

            
826
                $NONSTRUCTURAL_ACCUMULATE_ELSE
827
                {
828
                    dtrace!("is unknown (in signatures)");
829
                    let _: UnparsedItem = $THIS_ITEM;
830
                }
831
            }
832

            
833
            // Resolve all the fields
834
            dtrace!("reached end, resolving");
835

            
836
            $FINISH_RESOLVE
837
958
        }
838
    }
839
}
840

            
841
//==================== NetdocParseableFields user-facing derive macro ====================
842
//
843
// deftly template `NetdocParseableFields`
844
//
845
//  * entrypoint for deriving the `NetdocParseableFields` trait
846
//  * docs and implementation for that derive
847
//
848
// Much of the heavy lifting is done in the NetdocSomeItemsParseableCommon deftly module.
849

            
850
define_derive_deftly! {
851
    use NetdocDeriveAnyCommon;
852
    use NetdocParseAnyCommon;
853
    use NetdocFieldsDeriveCommon;
854
    use NetdocSomeItemsDeriveCommon;
855
    use NetdocSomeItemsParseableCommon;
856

            
857
    /// Derive [`NetdocParseableFields`] for a struct with individual items
858
    ///
859
    /// Defines a struct `FooNetdocParseAccumulator` to be the
860
    /// `NetdocParseableFields::Accumulator`.
861
    ///
862
    /// Similar to
863
    /// [`#[derive_deftly(NetdocParseable)]`](derive_deftly_template_NetdocParseable),
864
    /// but:
865
    ///
866
    ///  * Derives [`NetdocParseableFields`]
867
    $DOC_NETDOC_FIELDS_DERIVE_SUPPORTED
868
    ///
869
    export NetdocParseableFields for struct , meta_quoted rigorous, expect items, beta_deftly:
870

            
871
    ${define ITEM_STREAM items}
872
    ${define THIS_ITEM item}
873
    ${define F_ACCUMULATE_VAR { (&mut acc.$fname) }}
874

            
875
    #[doc = ${concat "Partially parsed `" $tname "`"}]
876
    ///
877
    /// Used for [`${concat $P::NetdocParseableFields::Accumulator}`].
878
    #[derive(Default, Debug)]
879
    $tvis struct $<$tname NetdocParseAccumulator><$tdefgens> { $(
880
        $fname: $F_ACCUMULATE_TYPE,
881
    ) }
882

            
883
    impl<$tgens> $P::NetdocParseableFields for $ttype {
884
        type Accumulator = $<$ttype NetdocParseAccumulator>;
885

            
886
388
        fn is_item_keyword(
887
388
            #[allow(unused_variables)] // If there are no fields, this is unused
888
388
            kw: $P::KeywordRef<'_>,
889
388
        ) -> bool {
890
            #[allow(unused_imports)] // false positives in some situations
891
            use $P::*;
892

            
893
          ${for fields {
894
            ${when not(F_FLATTEN)}
895
            ${when not(F_SKIP)}
896
250
            kw == $F_KEYWORD ||
897
          }}
898
          ${for fields {
899
            ${when F_FLATTEN}
900
            ${when not(F_SKIP)}
901
6
            <$ftype as NetdocParseableFields>::is_item_keyword(kw) ||
902
          }}
903
            false
904
388
        }
905

            
906
        #[allow(clippy::redundant_locals)] // let item = $THIS_ITEM, which might be item
907
382
        fn accumulate_item(
908
382
            #[allow(unused_variables)] // If there are no fields, this is unused
909
382
            acc: &mut Self::Accumulator,
910
382
            #[allow(unused_variables)] // If there are no fields, this is unused
911
382
            item: $P::UnparsedItem<'_>,
912
382
        ) -> $P::Result<(), $P::ErrorProblem> {
913
            #[allow(unused_imports)] // false positives in some situations
914
            use $P::*;
915
            $DEFINE_DTRACE
916

            
917
            $ITEM_SET_SELECTORS
918
            $CHECK_FIELD_TYPES_PARSEABLE
919

            
920
            #[allow(unused_variables)] // If there are no fields, this is unused
921
382
            let kw = item.keyword();
922

            
923
            $NONSTRUCTURAL_ACCUMULATE_ELSE
924
            {
925
                panic!("accumulate_item called though is_item_keyword returns false");
926
            }
927

            
928
            #[allow(unreachable_code)] // If there are no fields!
929
382
            Ok(())
930
382
        }
931

            
932
278
        fn finish(
933
278
            #[allow(unused_variables)] // If there are no fields, this is unused
934
278
            acc: Self::Accumulator,
935
278
            #[allow(unused_variables)] // often unused
936
278
            items: &$P::ItemStream<'_>,
937
278
        ) -> $P::Result<Self, $P::ErrorProblem> {
938
            #[allow(unused_imports)] // false positives in some situations
939
            use $P::*;
940
            $DEFINE_DTRACE
941

            
942
            dtrace!("finish, resolving");
943

            
944
            $ITEM_SET_SELECTORS
945

            
946
         $(
947
            ${when not(F_SKIP)}
948
276
            let $fpatname = acc.$fname;
949
         )
950
            $FINISH_RESOLVE
951
278
        }
952
    }
953
}
954

            
955
//==================== NetdocParseableUnverified user-facing derive macro ====================
956
//
957
// deftly template `NetdocParseableUnverified`
958
//
959
//  * entrypoint for deriving the `FooUnverified` struct implementing `NetdocParseable`
960
//    (and supporting items such as `FooUnverifiedParsedBody` structs and its impl).
961
//  * docs for that derive, including doc-level signatures-related attributes
962
//  * implementation glue for those derived impls
963
//
964
// The principal derived parsing impl on the body type `Foo` is expanded by this macro,
965
// but that is implemented via IMPL_NETDOC_PARSEABLE in the NetdocParseable deftly module.
966
//
967
// The substantive code to implement `NetdocParseable` for `FooUnverified` is
968
// in the `ItemStream::parse_signed` helper function; a call to that is expanded here.
969

            
970
define_derive_deftly! {
971
    use NetdocParseable;
972

            
973
    /// Derive `NetdocParseable` for a top-level signed document
974
    ///
975
    /// ### Expected input structure
976
    ///
977
    /// Apply this derive to the main body struct `Foo`,
978
    /// which should meet all the requirements to derive
979
    /// [`NetdocParseable`](derive_deftly_template_NetdocParseable).
980
    ///
981
    /// Usually, the caller will provide suitable ad-hoc `.verify_...` methods
982
    /// on `FooUnverified`.
983
    ///
984
    /// The `verify` method should:
985
    ///
986
    ///   * Take as an argument(s) the expected (trustworthy) signer(s),
987
    ///     (if the document is not always just supposed to be self-signed).
988
    ///
989
    ///   * Work directly with the fields `body` and `sigs` in `FooUnverified`.
990
    ///
991
    ///   * Verify all the signatures, including any [`EmbeddedCert`]s in the body.
992
    ///
993
    ///   * Cross-check any information that is supposed to be duplicated.
994
    ///
995
    ///   * Determine the validity period, from the validity time
996
    ///     information contained in the document or signatures.
997
    ///
998
    ///   * Return `TimeRangebound<Foo>`.
999
    ///
    /// ### Generated code
    ///
    /// Supposing your input structure is `Foo`, this macro will
    /// generate a **`struct FooUnverified`**
    /// implementing [`NetdocParseable`] and [`NetdocParseableUnverified`]:
    ///
    /// ```rust,ignore
    /// # struct Foo; struct FooSignatures;
    /// pub struct FooUnverified {
    ///     body: Foo,
    ///     pub sigs: SignaturesData<FooUnverified>,
    /// }
    /// ```
    ///
    /// Also generated is `FooUnverifiedParsedBody`
    /// and an impl of [`HasUnverifiedParsedBody`] on `Foo`.
    /// These allow the generated code to call [`ItemStream::parse_signed`]
    /// and it should not normally be necessary to use them elsewhere.
    ///
    /// ### Required top-level attributes:
    ///
    /// * **`#[deftly(netdoc(signature = TYPE))]`**:
    ///   Type of the signature(s) section.
    ///
    ///   TYPE must implement `NetdocParseable`,
    ///   with `is_intro_item_keyword` reporting *every* signature keyword.
    ///   Normally this is achieved with
    ///   `#[derive_deftly(NetdocParseable)] #[deftly(netdoc(signatures))]`.
    ///
    /// ### Optional attributes
    ///
    /// All the attributes supported by the `NetdocParseable` derive are supported.
    //
    // We don't make NetdocParseableUnverified a generic struct because
    //  - the defining module (crate) will want to add verification methods,
    //    which means they must define the struct
    //  - that lets the actual `body` field be private to the defining module.
    export NetdocParseableUnverified for struct, meta_quoted rigorous, expect items, beta_deftly:
    ${define NETDOC_PARSEABLE_TTYPE { $<$ttype UnverifiedParsedBody> }}
    ${define FINISH_RESOLVE_PARSEABLE {
        { $FINISH_RESOLVE }
958
        .map(|unverified| $<$tname UnverifiedParsedBody> { unverified })
    }}
    $IMPL_NETDOC_PARSEABLE
    // FooSignatures (type name)
    ${define SIGS_TYPE { $< ${tmeta(netdoc(signatures)) as ty, default $<$ttype Signatures>} > }}
    ${define SIGS_DATA_TYPE { $P::SignaturesData<$<$ttype Unverified>> }}
    ${define SIGS_HASHES_ACCU_TYPE { <$SIGS_TYPE as $P::NetdocParseableSignatures>::HashesAccu }}
   $///Signed (unverified) form of [`$tname`]
    ///
    /// Embodies:
    ///
   $///  * **[`$tname`]**: document body
   $///  * **[`$SIGS_TYPE`]**: signatures
    ///
    /// If this type was parsed from a document text,
    /// the signatures have *not* yet been verified.
    ///
    /// Use a `.verify_...` method to obtain useable, verified, contents.
    #[derive(Debug, Clone)]
    $tvis struct $<$ttype Unverified> {
        /// The actual body
        //
        // Misuse is prevented by this field not being public.
        // It can be accessed only in this module, where the verification functions are.
        body: $ttype,
        /// Signatures
        $tvis sigs: $SIGS_DATA_TYPE,
    }
    /// The parsed but unverified body part of a signed network document (working type)
    ///
   $/// Contains a $tname which has been parsed
    /// as part of a signed document,
    /// but the signatures aren't embodied here, and have not been verified.
    ///
    /// Not very useful to callers, who should use the `BodyUnverified` type instead,
    /// and its implementation of `NetdocParseable`.
    //
    // We implement NetdocParseable on FooUnverified using ItemStream::parse_signed.
    // ItemStream::parse_signed is a fairly normal but ad-hoc
    // implementation of NetdocParseable which uses as subroutines implementations
    // of NetdocParseable for the body and NetdocParseableSignatures for the signatures.
    //
    // We need a newtype because we don't want to implement `NetdocParseable`
    // for a type which is just the body.  Such an impl would be usable by mistake,
    // via the top-level parse2 functions, and it would then simply discard the signatures
    // and return unverified data, bypassing our efforts to prevent such bugs.
    //
    // Ideally we would have a generic `UnverifiedParsedBody<B>` type or something
    // but then this macro, invoked in other crates, couldn't impl NetdocParseable for
    // UnverifiedParsedBody<TheirType>, due to trait coherence rules.
    //
    #[derive(derive_more::From)]
    pub struct $NETDOC_PARSEABLE_TTYPE<$tdefgens> {
        /// The unverified body
        unverified: $ttype,
    }
    impl<$tgens> $P::NetdocParseable for $<$ttype Unverified> {
        fn doctype_for_error() -> &'static str {
            $NETDOC_PARSEABLE_TTYPE::doctype_for_error()
        }
        fn is_intro_item_keyword(kw: $P::KeywordRef<'_>) -> bool {
            $NETDOC_PARSEABLE_TTYPE::is_intro_item_keyword(kw)
        }
        fn is_structural_keyword(kw: $P::KeywordRef<'_>) -> Option<$P::IsStructural> {
            $NETDOC_PARSEABLE_TTYPE::is_structural_keyword(kw).or_else(
                || <$SIGS_TYPE as $P::NetdocParseableSignatures>
                    ::is_item_keyword(kw).then_some($P::IsStructural)
            )
        }
958
        fn from_items<'s>(
958
            input: &mut $P::ItemStream<'s>,
958
            outer_stop: $P::stop_at!(),
958
        ) -> $P::Result<$<$ttype Unverified>, $P::ErrorProblem> {
            $EMIT_DEBUG_PLACEHOLDER
958
            input.parse_signed(outer_stop)
958
        }
    }
    impl<$tgens> $P::NetdocParseableUnverified for $<$ttype Unverified> {
        type Body = $ttype;
        type Signatures = $SIGS_TYPE;
932
        fn inspect_unverified(&self) -> (&Self::Body, &$SIGS_DATA_TYPE) {
932
            (&self.body, &self.sigs)
932
        }
6
        fn unwrap_unverified(self) -> (Self::Body, $SIGS_DATA_TYPE) {
6
            (self.body, self.sigs)
6
        }
958
        fn from_parts(body: Self::Body, sigs: $SIGS_DATA_TYPE) -> Self {
958
            Self { body, sigs }
958
        }
    }
    impl<$tgens> $P::HasUnverifiedParsedBody for $ttype {
        type UnverifiedParsedBody = $NETDOC_PARSEABLE_TTYPE;
958
        fn unverified_into_inner_unchecked(unverified: Self::UnverifiedParsedBody) -> Self {
958
            unverified.unverified
958
        }
    }
}
//==================== ItemValueParseable user-facing derive macro ====================
//
// deftly template `ItemValueParseable`
//
//  * entrypoint for deriving the `ItemValueParseable` and `SignatureItemParseable` traits
//  * docs for the meta attributes we support during *item* parsing
//  * implementation of those derives
define_derive_deftly! {
    use NetdocDeriveAnyCommon;
    use NetdocParseAnyCommon;
    use NetdocItemDeriveCommon;
    /// Derive `ItemValueParseable` (or `SignatureItemParseable`)
    ///
    // NB there is very similar wording in the ItemValueEncodable derive docs.
    // If editing any of this derive's documentation, considering editing that too.
    //
    /// Fields in the struct are parsed from the keyword line arguments,
    /// in the order they appear in the struct.
    ///
    /// ### Field type
    ///
    /// Each field should be:
    ///
    ///  * `impl `[`ItemArgumentParseable`] (one argument),
    ///  * `Option<impl ItemArgumentParseable>` (one optional argument),
    ///  * `Vec<impl ItemArgumentParseable>` (zero or more arguments), or
    ///  * `BTreeSet<impl ItemArgumentParseable + Ord>` (zero or more arguments).
    ///
    /// `ItemArgumentParseable` can be implemented via `impl FromStr`,
    /// by writing `impl NormalItemArgument`.
    ///
    /// For `Option` or `Vec`, we expect that *if* there are any further arguments,
    /// they are for this field.
    /// So absence of any optional argument means absence of following arguments,
    /// and no arguments can follow a `Vec`.
    ///
    /// Some Tor netdocs have optional arguments followed by other data,
    /// with unclear/ambiguous parsing rules.
    /// These cases typically require manual implementation of [`ItemValueParseable`].
    ///
    /// (Multiplicity is implemented via types in the [`multiplicity`] module,
    /// specifically [`ArgumentSetSelector`] and [`ArgumentSetMethods`].)
    ///
    /// ### Top-level attributes:
    ///
    ///  * **`#[deftly(netdoc(no_extra_args))]**:
    ///
    ///    Reject, rather than ignore, additional arguments found in the document
    ///    which aren't described by the struct.
    ///
    ///  * **`#[deftly(netdoc(signature(hash_accu = HASH_ACCU))]**:
    ///
    ///    This item is a signature item.
    ///    [`SignatureItemParseable`] will be implemented instead of [`ItemValueParseable`].
    ///
    ///    **`HASH_ACCU`** is the type in which the hash(es) for this item will be accumulated,
    ///    and must implement [`SignatureHashesAccumulator`].
    ///    It is used as [`SignatureItemParseable::HashAccu`].
    ///
    /// * **`#[deftly(netdoc(debug))]`**:
    ///
    ///   Currently implemented only as a placeholder
    ///
    ///   The generated implementation may in future generate copious debug output
    ///   to the program's stderr when it is run.
    ///   Do not enable in production!
    ///
    $DOC_DEBUG_PLACEHOLDER
    ///
    /// ### Field-level attributes:
    ///
    ///  * **`#[deftly(netdoc(rest))]**:
    ///
    ///    The field is the whole rest of the line.
    ///    Must come after any other normal argument fields.
    ///    Only allowed once.
    ///
    ///    The field type must implement `FromStr`.
    ///    (I.e. `Vec` , `Option` etc., are not allowed, and `ItemArgumentParseable` is not used.)
    ///
    ///  * **`#[deftly(netdoc(object))]**:
    ///
    ///    The field is the Object.
    ///    It must implement [`ItemObjectParseable`]
    ///    (or be `Option<impl ItemObjectParseable>`).
    ///
    ///    Only allowed once.
    ///    If omittted, any object is rejected.
    ///
    ///  * **`#[deftly(netdoc(object(label = "LABEL")))]**:
    ///
    ///    Sets the expected label for an Object.
    ///    If not supplied, uses [`ItemObjectParseable::check_label`].
    ///
    ///  * **`#[deftly(netdoc(with = MODULE)]**:
    ///
    ///    Instead of `ItemArgumentParseable`, the argument is parsed with `MODULE::from_args`,
    ///    which must have the same signature as [`ItemArgumentParseable::from_args`].
    ///
    ///    With `#[deftly(netdoc(rest))]`, the argument is parsed with `MODULE::from_args_rest`,
    ///    must have the signature
    ///    `fn from_args_rest(s: &str) -> Result<FIELD, _>`).
    ///    and replaces `<FIELD as FromStr>::from_str`.
    ///
    ///    With `#[deftly(netdoc(object))]`, uses `MODULE::try_from`
    ///    which must have the signature `fn(Vec<u8>) -> Result<OBJECT, _>;
    ///    like `TryFrom::<Vec<u8>>>::try_from`.
    ///    LABEL must also be specified
    ///    unless the object also implements `ItemObjectParseable`.
    ///    Errors from parsing will all be collapsed into
    ///    [`ErrorProblem::ObjectInvalidData`].
    ///
    ///  * **`#[deftly(netdoc(skip))]**:
    ///
    ///    Do not parse this field; fill it in with `Default::default()` instead.
    export ItemValueParseable for struct, meta_quoted rigorous, expect items, beta_deftly:
    ${define TRAIT ${if T_IS_SIGNATURE { SignatureItemParseable } else { ItemValueParseable }}}
    ${define METHOD ${if T_IS_SIGNATURE { from_unparsed_and_body } else { from_unparsed }}}
    // SignatureItemParseable::HashAccu
    ${define SIG_HASH_ACCU_TYPE ${tmeta(netdoc(signature(hash_accu))) as ty}}
    // Avoid that someone wanting to parse a signed netdoc
    //   - tries to use parse2 on Foo rather than FooUnverified
    //   - fails to notice the impl on FooUnverified
    //   - adds a derive of NetdocParseable
    //   - calls that parser
    // thereby completely ignoring the signatures.
    $P::assert_not_impl! {
        [signed_documents_should_be_parsed_only_as_foo_unverified <$tgens>]
        $ttype: $P::NetdocParseable
    }
    impl<$tgens> $P::$TRAIT for $ttype {
      ${if T_IS_SIGNATURE {
        type HashAccu = $SIG_HASH_ACCU_TYPE;
      }}
9426
        fn $METHOD<'s>(
9426
            mut input: $P::UnparsedItem<'s>,
9426
          ${if T_IS_SIGNATURE {
9426
            document_body: &SignatureHashInputs<'_>,
9426
            hash_accu: &mut $SIG_HASH_ACCU_TYPE,
9426
          }}
9426
        ) -> $P::Result<Self, $P::EP>
        {
            #[allow(unused_imports)] // false positive when macro is used with prelude in scope
            use $P::*;
            $DEFINE_DTRACE
18
            dtrace!(
18
                "item start",
18
                input.keyword().as_str(),
18
                input.args_copy().into_remaining(),
27
                input.object().map(|o| o.label()),
            );
            ${if T_IS_SIGNATURE {
942
                <$SIG_HASH_ACCU_TYPE as SignatureHashesAccumulator>::update_from_netdoc_body(
942
                    hash_accu,
942
                    document_body
                )?;
            }}
9426
            let object = input.object();
            #[allow(unused)]
9426
            let mut args = input.args_mut();
          $(
            #[allow(non_snake_case)]
9146
            let $fpatname = ${select1
              F_NORMAL { {
5312
                  let selector = MultiplicitySelector::<$ftype>::default();
18
                  dtrace!(
18
                      $"field $fname, normal",
18
                      selector.argument_set_debug(),
18
                      args.clone().into_remaining(),
                  );
                ${if not(fmeta(netdoc(with))) {
266
                  selector.${paste_spanned $fname check_argument_value_parseable}();
                }}
5312
                  selector.parse_with(
5312
                      &mut args,
                      ${fmeta(netdoc(with))
                        as path,
                        default { ItemArgumentParseable }}::${paste_spanned $fname from_args},
5312
                  ).map_err(args.error_handler(stringify!($fname)))?
              } }
              F_OBJECT { {
5868
                  let selector = MultiplicitySelector::<$ftype>::default();
18
                  dtrace!(
18
                      $"field $fname, object",
18
                      selector.object_set_debug(),
27
                      object.as_ref().map(|o| (o.label(), o.decode_data().map(|d| d.len()))),
                  );
6080
                  let object = object.map(|object| {
5832
                      let data = object.decode_data()?;
                      ${if fmeta(netdoc(object(label))) {
2914
                          if object.label() != ${fmeta(netdoc(object(label))) as str} {
2
                              return Err(EP::ObjectIncorrectLabel)
2912
                          }
                      } else {
2916
                          selector.${paste_spanned $fname check_label}(object.label())?;
                      }}
                      ${if fmeta(netdoc(with)) {
                          ${fmeta(netdoc(with)) as path}::${paste_spanned $fname try_from}
2912
                              (data)
                              .map_err(|_| EP::ObjectInvalidData)
                      } else {
138
                          selector.${paste_spanned $fname check_object_parseable}();
2912
                          ItemObjectParseable::from_bytes(&data)
                      }}
6080
                  }).transpose()?;
5858
                  selector.resolve_option(object)?
              } }
              F_REST { {
                  dtrace!($"field $fname, rest", args.clone().into_remaining());
                  // consumes `args`, leading to compile error if the rest field
                  // isn't last (or is combined with no_extra_args).
16
                  let args_consume = args;
                  ${if fmeta(netdoc(with)) {
                      ${fmeta(netdoc(with)) as path}::${paste_spanned $fname from_args_rest}
                  } else {
10
                      <$ftype as FromStr>::from_str
                  }}
10
                      (args_consume.into_remaining())
10
                      .map_err(|_| AE::Invalid)
10
                      .map_err(args_consume.error_handler(stringify!($fname)))?
              } }
              F_SKIP { {
2212
                  <$ftype as Default>::default()
              } }
            };
          )
          ${if approx_equal({}, $( ${when F_OBJECT} $fname )) {
3538
            if object.is_some() {
                return Err(EP::ObjectUnexpected);
3538
            }
          }}
          ${if tmeta(netdoc(no_extra_args)) {
3850
            args.reject_extra_args()?;
          }}
18
            dtrace!("item complete Ok");
9392
            Ok($tname { $( $fname: $fpatname, ) })
9426
        }
    }
}