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
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
    let mut out = String::new();
15
    (|| {
16
        write!(out, "netdoc {ttype} parse: {msg}")?;
17
        for val in vals {
18
            write!(out, ", {val:?}")?;
19
        }
20
        writeln!(out)
21
    })()
22
    .expect("write to string failed");
23

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

            
27
define_derive_deftly_module! {
28
    /// Common definitions for `NetdocParseable`, `NetdocParseableFields`,
29
    /// and `NetdocParseableSignatures`
30
    ///
31
    /// The including macro is expected to define:
32
    ///
33
    ///  * **`THIS_ITEM`**: consumes the next item and evaluates to it as an `UnparsedItem`.
34
    ///    See the definition in `NetdocParseable`.
35
    ///
36
    ///  * **`F_ACCUMULATE_VAR`** the variable or field into which to accumulate
37
    ///    normal items for this field.  Must be of type `&mut $F_ACCUMULATE_TYPE`.
38
    ///
39
    /// Importer must also import `NetdocSomeItemsDeriveCommon` and `NetdocDeriveAnyCommon`.
40
    NetdocSomeItemsParseableCommon beta_deftly:
41

            
42
    // Convenience alias for our prelude
43
    ${define P { $crate::parse2::internal_prelude }}
44

            
45
    // Defines the `dtrace` macro.
46
    ${define DEFINE_DTRACE {
47
        #[allow(unused_macros)]
48
        macro_rules! dtrace { { $$msg:literal $$(, $$val:expr )* $$(,)? } => {
49
          ${if tmeta(netdoc(debug)) {
50
              $P::netdoc_parseable_derive_debug(
51
                  ${concat $ttype},
52
                  $$msg,
53
                  &[ $$( &&$$val as _, )* ],
54
              )
55
          }}
56
        }}
57
    }}
58

            
59
    // The effective field type for parsing.
60
    //
61
    // Handles #[deftly(netdoc(default))], in which case we parse as if the field was Option,
62
    // and substitute in the default at the end.
63
    //
64
    ${define F_EFFECTIVE_TYPE {
65
        ${if all(fmeta(netdoc(default))) {
66
            Option::<$ftype>
67
        } else {
68
            $ftype
69
        }}
70
    }}
71

            
72
    // Provide `$<selector_ $fname>` for every (suitable) field.
73
    ${define ITEM_SET_SELECTORS {
74
        $(
75
          ${when not(any(F_FLATTEN, F_SKIP))}
76

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

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

            
124
    // Convert the UnparsedItem (in `item` to the value (to accumulate).
125
    // Expands to an expression.
126
    ${define ITEM_VALUE_FROM_UNPARSED {
127
        ${if fmeta(netdoc(with)) {
128
          ${fmeta(netdoc(with)) as path}
129
              ::${paste_spanned $fname from_unparsed}
130
              (item)?
131
        } else if fmeta(netdoc(single_arg)) { {
132
          let item = ItemValueParseable::from_unparsed(item)?;
133
          let (item,) = item;
134
          item
135
        } } else {
136
          ItemValueParseable::from_unparsed(item)?
137
        }}
138
    }}
139

            
140
    // Type into which we accumulate value(s) of this field
141
    ${define F_ACCUMULATE_TYPE {
142
        ${if F_FLATTEN {
143
            <$ftype as $P::NetdocParseableFields>::Accumulator
144
        } else {
145
            Option::<$F_EFFECTIVE_TYPE>
146
        }
147
    }}}
148

            
149
    // Parse the intro item and bind `$fpatname` accumulator for each field.
150
    //
151
    // For the intro item, parse it and bind it to $fpatname.
152
    //
153
    // For other items, set up a mutable $fpatname, initialised to `default()` (normally None).
154
    ${define INIT_ACCUMULATE_VARS {
155
        $( ${select1 F_INTRO {
156

            
157
          let item = input.next_item()?.ok_or(EP::EmptyDocument)?;
158
          dtrace!("intro", item);
159
          if !Self::is_intro_item_keyword(item.keyword()) {
160
              Err(EP::WrongDocumentType)?;
161
          }
162
          let $fpatname: $ftype = $ITEM_VALUE_FROM_UNPARSED;
163

            
164
        } F_SKIP {
165

            
166
        } else {
167

            
168
          let mut $fpatname = $F_ACCUMULATE_TYPE::default();
169

            
170
        }})
171
    }}
172

            
173
    // Accumulates `item` (which must be `ItemSetMethods::Each`) into `$F_ACCUMULATE_VAR`
174
    ${define ACCUMULATE_ITEM_VALUE { {
175
        $F_SELECTOR.${paste_spanned $fname accumulate}($F_ACCUMULATE_VAR, item)?;
176
    } }}
177

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

            
191
          if kw == $F_KEYWORD {
192
            ${select1
193
              F_NORMAL {
194
                let item = $THIS_ITEM;
195
                dtrace!("is normal", item);
196
                let item = $ITEM_VALUE_FROM_UNPARSED;
197
                $ACCUMULATE_ITEM_VALUE
198
              }
199
              F_SIGNATURE {
200
                let hash_inputs = input
201
                      .peek_signature_hash_inputs(signed_doc_body)?
202
                      .expect("not eof, we peeked kw");
203

            
204
                let item = $THIS_ITEM;
205
                dtrace!("is signature", item);
206
                let item = SignatureItemParseable::from_unparsed_and_body(
207
                    item,
208
                    &hash_inputs,
209
                    AsMut::as_mut(sig_hashes),
210
                )?;
211
                $ACCUMULATE_ITEM_VALUE
212
              }
213
              F_INTRO {
214
                dtrace!("is intro", kw);
215
                break;
216
              } // start of next similar document
217
            }
218
          } else
219
        }}
220
        ${for fields {
221
          ${when F_FLATTEN}
222

            
223
          if $ftype::is_item_keyword(kw) {
224
              dtrace!(${concat "is flatten in " $fname}, kw);
225
              let item = $THIS_ITEM;
226
              <$ftype as NetdocParseableFields>::accumulate_item($F_ACCUMULATE_VAR, item)?;
227
          } else
228
        }}
229
    }}
230

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

            
268
//==================== Main whole document parsing impl ====================
269
//
270
// deftly module ` NetdocParseable`:
271
//
272
//   * IMPL_NETDOC_PARSEABLE expanding to `impl NetdocParseable { ... }`
273
//
274
// Much of the heavy lifting is done in the NetdocSomeItemsParseableCommon deftly module.
275

            
276
define_derive_deftly_module! {
277
    /// Provides `IMPL_NETDOC_PARSEABLE` which impls `NetdocParseable`
278
    ///
279
    /// Used by the `NetdocParseable` and `NetdocParseableUnverified` derives.
280
    NetdocParseable beta_deftly:
281

            
282
    use NetdocDeriveAnyCommon;
283
    use NetdocEntireDeriveCommon;
284
    use NetdocSomeItemsDeriveCommon;
285
    use NetdocSomeItemsParseableCommon;
286

            
287
    ${define F_ACCUMULATE_VAR { (&mut $fpatname) }}
288

            
289
  ${define IMPL_NETDOC_PARSEABLE {
290
    impl<$tgens> $P::NetdocParseable for $NETDOC_PARSEABLE_TTYPE {
291
588
        fn doctype_for_error() -> &'static str {
292
            ${tmeta(netdoc(doctype_for_error)) as expr,
293
              default ${concat ${for fields { ${when F_INTRO} $F_KEYWORD_STR }}}}
294
        }
295

            
296
122593
        fn is_intro_item_keyword(kw: $P::KeywordRef<'_>) -> bool {
297
            use $P::*;
298

            
299
            ${for fields {
300
                ${when F_INTRO}
301
                kw == $F_KEYWORD
302
            }}
303
        }
304

            
305
304
        fn is_structural_keyword(kw: $P::KeywordRef<'_>) -> Option<$P::IsStructural> {
306
            #[allow(unused_imports)] // not used if there are no subdocs
307
            use $P::*;
308

            
309
            if Self::is_intro_item_keyword(kw) {
310
                return Some(IsStructural)
311
            }
312

            
313
            ${for fields {
314
                ${when F_SUBDOC}
315
                if let y @ Some(_) = $F_SELECTOR_VALUE.is_structural_keyword(kw) {
316
                    return y;
317
                }
318
            }}
319

            
320
            None
321
        }
322

            
323
        //##### main parsing function #####
324

            
325
        #[allow(clippy::redundant_locals)] // let item = $THIS_ITEM, which might be item
326
1319
        fn from_items<'s>(
327
1319
            input: &mut $P::ItemStream<'s>,
328
1319
            outer_stop: $P::stop_at!(),
329
1319
        ) -> $P::Result<Self, $P::ErrorProblem> {
330
            use $P::*;
331
            $DEFINE_DTRACE
332
            $FIELD_ORDERING_CHECK
333

            
334
            //----- prepare item set selectors for every field -----
335
            $ITEM_SET_SELECTORS
336
            $CHECK_FIELD_TYPES_PARSEABLE
337

            
338
            // Is this an intro item keyword ?
339
            //
340
            // Expands to an appropriate `is_intro_item_keyword` method invocation,
341
            // but *without arguments*.  So, something a bit like an expression of type
342
            //    fn(KeywordRef) -> bool
343
            ${define F_SUBDOC_IS_INTRO_ITEM_KEYWORD {
344
                ${if not(F_SUBDOC) { ${error "internal-error: subdoc kw, but not subdoc field"} }}
345
7364
                $F_SELECTOR.is_intro_item_keyword
346
            }}
347

            
348
            //----- Helper fragments for parsing individual pieces of the document -----
349

            
350
            // Peeks a keyword, and returns it but only if it's part of this (sub)doc.
351
            // Return `None` if it was in outer_stop
352
8347
            let peek_keyword = |input: &mut ItemStream<'s>| -> Result<Option<KeywordRef<'s>>, EP> {
353
                let Some(kw) = input.peek_keyword()? else {
354
                    dtrace!("stopping, because EOF");
355
                    return Ok(None)
356
                };
357
                if outer_stop.stop_at(kw) {
358
                    dtrace!("stopping, because peeked", kw);
359
                    return Ok(None)
360
                }
361
                Ok(Some(kw))
362
            };
363

            
364
            // Returns the actual item as an UnparsedItem, committing to consuming it.
365
            // Can panic if called without previous `peek_keyword`.
366
            ${define THIS_ITEM  {
367
                input.next_item()?.expect("peeked")
368
            }}
369

            
370
            //----- keyword classification closures -----
371

            
372
            // Is this a keyword for one of our sub-documents?
373
            let is_subdoc_kw = ${for fields {
374
                ${when F_SUBDOC}
375
                StopAt(|kw: KeywordRef<'_>| $F_SUBDOC_IS_INTRO_ITEM_KEYWORD(kw)) |
376
              }}
377
                StopAt(false)
378
            ;
379
            // Is this a keyword for one of our parents or sub-documents?
380
            let inner_stop = outer_stop | is_subdoc_kw;
381

            
382
            //========== actual parsing ==========
383

            
384
            // For each parsing loop/section, where we aren't looking for precisely one thing,
385
            // we should explicitly decide what to do with each of:
386
            //   - F_INTRO - intro item for this document (maybe next instance in parent)
387
            //   - F_NORMAL - normal items
388
            //   - subdocuments, is_subdoc_kw and F_SUBDOC
389
            //   - our parent's structural keywords, outer_stop
390
            //     (this includes signature items for the signed version of this doc)
391
            // 5 cases in all.
392

            
393
            //----- Parse the intro item, and introduce bindings for the other items. -----
394

            
395
            dtrace!("looking for intro item");
396
            $INIT_ACCUMULATE_VARS
397

            
398
            //----- Parse the normal items -----
399
            dtrace!("looking for normal items");
400

            
401
            while let Some(kw) = peek_keyword(input)? {
402
                dtrace!("for normal, peeked", kw);
403
                if inner_stop.stop_at(kw) {
404
                    dtrace!("is inner stop", kw);
405
                    break;
406
                };
407

            
408
                $NONSTRUCTURAL_ACCUMULATE_ELSE
409
                {
410
                    dtrace!("is unknown (in normal)");
411
                    let _: UnparsedItem = $THIS_ITEM;
412
                }
413
            }
414

            
415
            //----- Parse the subdocs, in order -----
416
            dtrace!("looking for subdocs");
417

            
418
          ${for fields {
419
            ${when F_SUBDOC}
420
            dtrace!("looking for subdoc", $F_KEYWORD_REPORT);
421

            
422
            loop {
423
                let Some(kw) = peek_keyword(input)? else { break };
424
                dtrace!("for subdoc, peek", kw);
425

            
426
                if !$F_SUBDOC_IS_INTRO_ITEM_KEYWORD(kw) {
427
                    dtrace!("is not this subdoc", kw);
428
                    break;
429
                };
430

            
431
                $F_SELECTOR.can_accumulate(&mut $fpatname)?;
432

            
433
                dtrace!("is this subdoc", kw);
434
                let item = NetdocParseable::from_items(input, inner_stop);
435
                dtrace!("parsed this subdoc", item.as_ref().map(|_| ()));
436
                let item = item?;
437

            
438
                $ACCUMULATE_ITEM_VALUE
439
            }
440
          }}
441

            
442
            // Resolve all the fields
443
            dtrace!("reached end, resolving");
444

            
445
            $FINISH_RESOLVE_PARSEABLE
446
        }
447
    }
448
  }}
449
}
450

            
451
//==================== NetdocParseable user-facing derive macro ====================
452
//
453
// deftly template `NetdocParseable`:
454
//
455
//  * main entrypoint for deriving the `NetdocParseable` trait
456
//  * docs for the meta attributes we support during document parsing
457
//
458
// The actual implementation is in  the `NetdocParseable` deftly module, above.
459

            
460
define_derive_deftly! {
461
    use NetdocParseable;
462

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

            
652
    ${define NETDOC_PARSEABLE_TTYPE { $ttype }}
653
    ${define FINISH_RESOLVE_PARSEABLE $FINISH_RESOLVE}
654

            
655
    $IMPL_NETDOC_PARSEABLE
656
}
657

            
658
//==================== NetdocParseableSignatures user-facing derive macro ====================
659
//
660
// deftly template `NetdocParseableSignatures`:
661
//
662
//  * entrypoint for deriving the `NetdocParseableSignatures` trait
663
//  * docs for the signatures-section-specific attributes
664
//  * implementation of that derive
665
//
666
// Much of the heavy lifting is done in the NetdocSomeItemsParseableCommon deftly module.
667

            
668
define_derive_deftly! {
669
    use NetdocDeriveAnyCommon;
670
    use NetdocSomeItemsDeriveCommon;
671
    use NetdocSomeItemsParseableCommon;
672

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

            
754
    ${defcond F_INTRO false}
755
    ${defcond F_SUBDOC false}
756
    ${defcond F_SIGNATURE true}
757

            
758
    // NetdocParseableSignatures::HashesAccu
759
    ${define SIGS_HASHES_ACCU_TYPE { ${tmeta(netdoc(signatures(hashes_accu))) as ty} }}
760

            
761
    ${define THIS_ITEM { input.next_item()?.expect("peeked") }}
762
    ${define F_ACCUMULATE_VAR { (&mut $fpatname) }}
763

            
764
    impl<$tgens> $P::NetdocParseableSignatures for $ttype {
765
        type HashesAccu = $SIGS_HASHES_ACCU_TYPE;
766

            
767
38807
        fn is_item_keyword(kw: $P::KeywordRef<'_>) -> bool {
768
            use $P::*;
769
            ${for fields {
770
38807
                kw == $F_KEYWORD ||
771
            }}
772
37450
                false
773
38807
        }
774

            
775
        #[allow(clippy::redundant_locals)] // let item = $THIS_ITEM, which might be item
776
935
        fn from_items<'s>(
777
935
            input: &mut $P::ItemStream<'s>,
778
935
            signed_doc_body: $P::SignedDocumentBody<'s>,
779
935
            sig_hashes: &mut $SIGS_HASHES_ACCU_TYPE,
780
935
            outer_stop: $P::stop_at!(),
781
935
        ) -> $P::Result<$ttype, $P::ErrorProblem> {
782
            use $P::*;
783
            $DEFINE_DTRACE
784

            
785
            //----- prepare item set selectors for every field -----
786
            $ITEM_SET_SELECTORS
787
            $CHECK_FIELD_TYPES_PARSEABLE
788
            $INIT_ACCUMULATE_VARS
789

            
790
            //----- parse the items -----
791
            dtrace!("looking for signature items");
792

            
793
1946
            while let Some(kw) = input.peek_keyword()? {
794
                dtrace!("for signatures, peeked", kw);
795
1865
                if outer_stop.stop_at(kw) {
796
                    dtrace!("is outer stop", kw);
797
854
                    break;
798
1011
                };
799

            
800
                $NONSTRUCTURAL_ACCUMULATE_ELSE
801
                {
802
                    dtrace!("is unknown (in signatures)");
803
                    let _: UnparsedItem = $THIS_ITEM;
804
                }
805
            }
806

            
807
            // Resolve all the fields
808
            dtrace!("reached end, resolving");
809

            
810
            $FINISH_RESOLVE
811
935
        }
812
    }
813
}
814

            
815
//==================== NetdocParseableFields user-facing derive macro ====================
816
//
817
// deftly template `NetdocParseableFields`
818
//
819
//  * entrypoint for deriving the `NetdocParseableFields` trait
820
//  * docs and implementation for that derive
821
//
822
// Much of the heavy lifting is done in the NetdocSomeItemsParseableCommon deftly module.
823

            
824
define_derive_deftly! {
825
    use NetdocDeriveAnyCommon;
826
    use NetdocFieldsDeriveCommon;
827
    use NetdocSomeItemsDeriveCommon;
828
    use NetdocSomeItemsParseableCommon;
829

            
830
    /// Derive [`NetdocParseableFields`] for a struct with individual items
831
    ///
832
    /// Defines a struct `FooNetdocParseAccumulator` to be the
833
    /// `NetdocParseableFields::Accumulator`.
834
    ///
835
    /// Similar to
836
    /// [`#[derive_deftly(NetdocParseable)]`](derive_deftly_template_NetdocParseable),
837
    /// but:
838
    ///
839
    ///  * Derives [`NetdocParseableFields`]
840
    $DOC_NETDOC_FIELDS_DERIVE_SUPPORTED
841
    ///
842
    export NetdocParseableFields for struct , expect items, beta_deftly:
843

            
844
    ${define THIS_ITEM item}
845
    ${define F_ACCUMULATE_VAR { (&mut acc.$fname) }}
846

            
847
    #[doc = ${concat "Partially parsed `" $tname "`"}]
848
    ///
849
    /// Used for [`${concat $P::NetdocParseableFields::Accumulator}`].
850
    #[derive(Default, Debug)]
851
    $tvis struct $<$tname NetdocParseAccumulator><$tdefgens> { $(
852
        $fname: $F_ACCUMULATE_TYPE,
853
    ) }
854

            
855
    impl<$tgens> $P::NetdocParseableFields for $ttype {
856
        type Accumulator = $<$ttype NetdocParseAccumulator>;
857

            
858
168
        fn is_item_keyword(
859
168
            #[allow(unused_variables)] // If there are no fields, this is unused
860
168
            kw: $P::KeywordRef<'_>,
861
168
        ) -> bool {
862
            #[allow(unused_imports)] // false positives in some situations
863
            use $P::*;
864

            
865
          ${for fields {
866
            ${when not(F_FLATTEN)}
867
30
            kw == $F_KEYWORD ||
868
          }}
869
          ${for fields {
870
            ${when F_FLATTEN}
871
6
            <$ftype as NetdocParseableFields>::is_item_keyword(kw) ||
872
          }}
873
            false
874
168
        }
875

            
876
        #[allow(clippy::redundant_locals)] // let item = $THIS_ITEM, which might be item
877
162
        fn accumulate_item(
878
162
            #[allow(unused_variables)] // If there are no fields, this is unused
879
162
            acc: &mut Self::Accumulator,
880
162
            #[allow(unused_variables)] // If there are no fields, this is unused
881
162
            item: $P::UnparsedItem<'_>,
882
162
        ) -> $P::Result<(), $P::ErrorProblem> {
883
            #[allow(unused_imports)] // false positives in some situations
884
            use $P::*;
885
            $DEFINE_DTRACE
886

            
887
            $ITEM_SET_SELECTORS
888
            $CHECK_FIELD_TYPES_PARSEABLE
889

            
890
            #[allow(unused_variables)] // If there are no fields, this is unused
891
162
            let kw = item.keyword();
892

            
893
            $NONSTRUCTURAL_ACCUMULATE_ELSE
894
            {
895
                panic!("accumulate_item called though is_item_keyword returns false");
896
            }
897

            
898
            #[allow(unreachable_code)] // If there are no fields!
899
162
            Ok(())
900
162
        }
901

            
902
54
        fn finish(
903
54
            #[allow(unused_variables)] // If there are no fields, this is unused
904
54
            acc: Self::Accumulator
905
54
        ) -> $P::Result<Self, $P::ErrorProblem> {
906
            #[allow(unused_imports)] // false positives in some situations
907
            use $P::*;
908
            $DEFINE_DTRACE
909

            
910
            dtrace!("finish, resolving");
911

            
912
            $ITEM_SET_SELECTORS
913

            
914
54
         $( let $fpatname = acc.$fname; )
915
            $FINISH_RESOLVE
916
54
        }
917
    }
918
}
919

            
920
//==================== NetdocParseableUnverified user-facing derive macro ====================
921
//
922
// deftly template `NetdocParseableUnverified`
923
//
924
//  * entrypoint for deriving the `FooUnverified` struct implementing `NetdocParseable`
925
//    (and supporting items such as `FooUnverifiedParsedBody` structs and its impl).
926
//  * docs for that derive, including doc-level signatures-related attributes
927
//  * implementation glue for those derived impls
928
//
929
// The principal derived parsing impl on the body type `Foo` is expanded by this macro,
930
// but that is implemented via IMPL_NETDOC_PARSEABLE in the NetdocParseable deftly module.
931
//
932
// The substantive code to implement `NetdocParseable` for `FooUnverified` is
933
// in the `ItemStream::parse_signed` helper function; a call to that is expanded here.
934

            
935
define_derive_deftly! {
936
    use NetdocParseable;
937

            
938
    /// Derive `NetdocParseable` for a top-level signed document
939
    ///
940
    /// ### Expected input structure
941
    ///
942
    /// Apply this derive to the main body struct `Foo`,
943
    /// which should meet all the requirements to derive
944
    /// [`NetdocParseable`](derive_deftly_template_NetdocParseable).
945
    ///
946
    /// Usually, the caller will provide suitable ad-hoc `.verify_...` methods
947
    /// on `FooUnverified`.
948
    ///
949
    /// ### Generated code
950
    ///
951
    /// Supposing your input structure is `Foo`, this macro will
952
    /// generate a `**struct FooUnverified`**
953
    /// implementing [`NetdocParseable`] and [`NetdocUnverified`]:
954
    ///
955
    /// ```rust,ignore
956
    /// # struct Foo; struct FooSignatures;
957
    /// pub struct FooUnverified {
958
    ///     body: Foo,
959
    ///     pub sigs: SignaturesData<FooUnverified>,
960
    /// }
961
    /// ```
962
    ///
963
    /// Also generated is `FooUnverifiedParsedBody`
964
    /// and an impl of [`HasUnverifiedParsedBody`] on `Foo`.
965
    /// These allow the generated code to call [`ItemStream::parse_signed`]
966
    /// and it should not normally be necessary to use them elsewhere.
967
    ///
968
    /// ### Required top-level attributes:
969
    ///
970
    /// * **`#[deftly(netdoc(signature = "TYPE"))]`**:
971
    ///   Type of the signature(s) section.
972
    ///
973
    ///   TYPE must implement `NetdocParseable`,
974
    ///   with `is_intro_item_keyword` reporting *every* signature keyword.
975
    ///   Normally this is achieved with
976
    ///   `#[derive_deftly(NetdocParseable)] #[deftly(netdoc(signatures))]`.
977
    ///
978
    /// ### Optional attributes
979
    ///
980
    /// All the attributes supported by the `NetdocParseable` derive are supported.
981
    //
982
    // We don't make NetdocUnverified a generic struct because
983
    //  - the defining module (crate) will want to add verification methods,
984
    //    which means they must define the struct
985
    //  - that lets the actual `body` field be private to the defining module.
986
    export NetdocParseableUnverified for struct, expect items, beta_deftly:
987

            
988
    ${define NETDOC_PARSEABLE_TTYPE { $<$ttype UnverifiedParsedBody> }}
989
    ${define FINISH_RESOLVE_PARSEABLE {
990
        { $FINISH_RESOLVE }
991
935
        .map(|unverified| $<$tname UnverifiedParsedBody> { unverified })
992
    }}
993

            
994
    $IMPL_NETDOC_PARSEABLE
995

            
996
    // FooSignatures (type name)
997
    ${define SIGS_TYPE { $< ${tmeta(netdoc(signatures)) as ty, default $<$ttype Signatures>} > }}
998
    ${define SIGS_DATA_TYPE { $P::SignaturesData<$<$ttype Unverified>> }}
999
    ${define SIGS_HASHES_ACCU_TYPE { <$SIGS_TYPE as $P::NetdocParseableSignatures>::HashesAccu }}
    #[doc = ${concat "Signed (unverified) form of [`" $tname "`]"}]
    ///
    /// Embodies:
    ///
    #[doc = ${concat "  * **[`" $tname "`]**: document body"}]
    #[doc = ${concat "  * **[`" $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)
    ///
    #[doc = ${concat "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)
        }
60
        fn is_structural_keyword(kw: $P::KeywordRef<'_>) -> Option<$P::IsStructural> {
60
            $NETDOC_PARSEABLE_TTYPE::is_structural_keyword(kw)
90
                .or_else(|| <$SIGS_TYPE as $P::NetdocParseableSignatures>::is_item_keyword(kw).then_some($P::IsStructural))
60
        }
935
        fn from_items<'s>(
935
            input: &mut $P::ItemStream<'s>,
935
            outer_stop: $P::stop_at!(),
935
        ) -> $P::Result<$<$ttype Unverified>, $P::ErrorProblem> {
            $EMIT_DEBUG_PLACEHOLDER
935
            input.parse_signed(outer_stop)
935
        }
    }
    impl<$tgens> $P::NetdocUnverified for $<$ttype Unverified> {
        type Body = $ttype;
        type Signatures = $SIGS_TYPE;
915
        fn inspect_unverified(&self) -> (&Self::Body, &$SIGS_DATA_TYPE) {
915
            (&self.body, &self.sigs)
915
        }
118
        fn unwrap_unverified(self) -> (Self::Body, $SIGS_DATA_TYPE) {
118
            (self.body, self.sigs)
118
        }
1139
        fn from_parts(body: Self::Body, sigs: $SIGS_DATA_TYPE) -> Self {
1139
            Self { body, sigs }
1139
        }
    }
    impl<$tgens> $P::HasUnverifiedParsedBody for $ttype {
        type UnverifiedParsedBody = $NETDOC_PARSEABLE_TTYPE;
1139
        fn unverified_into_inner_unchecked(unverified: Self::UnverifiedParsedBody) -> Self {
1139
            unverified.unverified
1139
        }
    }
}
//==================== 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 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`].
    export ItemValueParseable for struct, expect items, beta_deftly:
    ${define P { $crate::parse2::internal_prelude }}
    ${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}}
    impl<$tgens> $P::$TRAIT for $ttype {
      ${if T_IS_SIGNATURE {
        type HashAccu = $SIG_HASH_ACCU_TYPE;
      }}
6858
        fn $METHOD<'s>(
6858
            mut input: $P::UnparsedItem<'s>,
6858
          ${if T_IS_SIGNATURE {
6858
            document_body: &SignatureHashInputs<'_>,
6858
            hash_accu: &mut $SIG_HASH_ACCU_TYPE,
6858
          }}
6858
        ) -> $P::Result<Self, $P::EP>
        {
            #[allow(unused_imports)] // false positive when macro is used with prelude in scope
            use $P::*;
            $EMIT_DEBUG_PLACEHOLDER
            ${if T_IS_SIGNATURE {
921
                <$SIG_HASH_ACCU_TYPE as SignatureHashesAccumulator>::update_from_netdoc_body(
921
                    hash_accu,
921
                    document_body
                )?;
            }}
6858
            let object = input.object();
            #[allow(unused)]
6858
            let mut args = input.args_mut();
          $(
6618
            let $fpatname = ${select1
              F_NORMAL { {
2874
                  let selector = MultiplicitySelector::<$ftype>::default();
                ${if not(fmeta(netdoc(with))) {
2874
                  selector.${paste_spanned $fname check_argument_value_parseable}();
                }}
2874
                  selector.parse_with(
2874
                      &mut args,
                      ${fmeta(netdoc(with))
                        as path,
                        default { ItemArgumentParseable }}::${paste_spanned $fname from_args},
2874
                  ).map_err(args.error_handler(stringify!($fname)))?
              } }
              F_OBJECT { {
3766
                  let selector = MultiplicitySelector::<$ftype>::default();
3897
                  let object = object.map(|object| {
3730
                      let data = object.decode_data()?;
                      ${if fmeta(netdoc(object(label))) {
931
                          if object.label() != ${fmeta(netdoc(object(label))) as str} {
2
                              return Err(EP::ObjectIncorrectLabel)
929
                          }
                      } else {
2797
                          selector.check_label(object.label())?;
                      }}
                      ${if fmeta(netdoc(with)) {
                          ${fmeta(netdoc(with)) as path}::${paste_spanned $fname try_from}
929
                              (data)
                              .map_err(|_| EP::ObjectInvalidData)
                      } else {
2795
                          selector.${paste_spanned $fname check_object_parseable}();
2795
                          ItemObjectParseable::from_bytes(&data)
                      }}
3897
                  }).transpose()?;
3756
                  selector.resolve_option(object)?
              } }
              F_REST { {
                  // 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)))?
              } }
            };
          )
          ${if approx_equal({}, $( ${when F_OBJECT} $fname )) {
3092
            if object.is_some() {
                return Err(EP::ObjectUnexpected);
3092
            }
          }}
          ${if tmeta(netdoc(no_extra_args)) {
3726
            args.reject_extra_args()?;
          }}
6844
            Ok($tname { $( $fname: $fpatname, ) })
6858
        }
    }
}