1
//! Common macro elements for deriving parsers and encoders
2

            
3
use derive_deftly::{define_derive_deftly, define_derive_deftly_module};
4

            
5
define_derive_deftly! {
6
    /// Defines a constructor struct and method
7
    //
8
    // TODO maybe move this out of tor-netdoc, to a lower-level dependency
9
    ///
10
    /// "Constructor" is a more lightweight alternative to the builder pattern.
11
    ///
12
    /// # Comparison to builders
13
    ///
14
    ///  * Suitable for transparent, rather than opaque, structs.
15
    ///  * Missing fields during construction are detected at compile-time.
16
    ///  * Construction is infallible at runtime.
17
    ///  * Making a previously-required field optional is an API break.
18
    ///
19
    /// # Input
20
    ///
21
    ///  * `struct Thing`.  (enums and unions are not supported.)
22
    ///
23
    ///  * Each field must impl `Default` or be annotated `#[deftly(constructor)]`
24
    ///
25
    ///  * `Thing` should contain `#[doc(hidden)] pub __non_exhaustive: ()`
26
    ///    rather than being `#[non_exhaustive]`.
27
    ///    (Because struct literal syntax is not available otherwise.)
28
    ///
29
    /// # Generated items
30
    ///
31
    ///  * **`pub struct ThingConstructor`**:
32
    ///    contains all the required (non-optional) fields from `Thing`.
33
    ///    `ThingConstructor` is `exhaustive`.
34
    ///
35
    ///  * **`fn ThingConstructor::construct(self) -> Thing`**:
36
    ///    fills in all the default values.
37
    ///
38
    ///  * `impl From<ThingConstructor> for Thing`
39
    ///
40
    /// # Attributes
41
    ///
42
    /// ## Field attributes
43
    ///
44
    ///  * **`#[deftly(constructor)]`**:
45
    ///    Include this field in `ThingConstructor`.
46
    ///    The caller must provide a value.
47
    ///
48
    ///  * **`#[deftly(constructor(default = EXPR))]`**:
49
    ///    Instead of `Default::default()`, the default value is EXPR.
50
    ///    EXPR cannot refer to anything in `ThingConstructor`.
51
    //     If we want that we would need to invent a feature for it.
52
    ///
53
    /// # Example
54
    ///
55
    /// ```
56
    /// use derive_deftly::Deftly;
57
    /// use tor_netdoc::derive_deftly_template_Constructor;
58
    ///
59
    /// #[derive(Deftly, PartialEq, Debug)]
60
    /// #[derive_deftly(Constructor)]
61
    /// #[allow(clippy::exhaustive_structs)]
62
    /// pub struct Thing {
63
    ///     /// Required field
64
    ///     #[deftly(constructor)]
65
    ///     pub required: i32,
66
    ///
67
    ///     /// Optional field
68
    ///     pub optional: Option<i32>,
69
    ///
70
    ///     /// Optional field with fixed default
71
    ///     #[deftly(constructor(default = 7))]
72
    ///     pub defaulted: i32,
73
    ///
74
    ///     #[doc(hidden)]
75
    ///     pub __non_exhaustive: (),
76
    /// }
77
    ///
78
    /// let thing = Thing {
79
    ///     optional: Some(23),
80
    ///     ..ThingConstructor {
81
    ///         required: 12,
82
    ///     }.construct()
83
    /// };
84
    ///
85
    /// assert_eq!(
86
    ///     thing,
87
    ///     Thing {
88
    ///         required: 12,
89
    ///         optional: Some(23),
90
    ///         defaulted: 7,
91
    ///         __non_exhaustive: (),
92
    ///     }
93
    /// );
94
    /// ```
95
    ///
96
    /// # Note
97
    export Constructor for struct, meta_quoted rigorous, beta_deftly:
98

            
99
    ${define CONSTRUCTOR_NAME $<$tname Constructor>}
100
    ${define CONSTRUCTOR $<$ttype Constructor>}
101

            
102
    ${defcond F_DEFAULT_EXPR fmeta(constructor(default))}
103
    ${defcond F_DEFAULT_TRAIT not(fmeta(constructor))}
104
    ${defcond F_REQUIRED not(any(F_DEFAULT_EXPR, F_DEFAULT_TRAIT))}
105

            
106
    $/// Constructor (required fields) for `$tname`
107
    $///
108
    $/// See [`$tname`].
109
    $///
110
    $/// This constructor struct contains precisely the required fields.
111
    $/// You can make a `$tname` out of it with [`.construct()`]($CONSTRUCTOR_NAME::construct),
112
    $/// or the `From` impl,
113
    $/// and use the result as a basis for further modifications.
114
    $///
115
    $/// # Example
116
    $///
117
    $/// ```rust,ignore
118
    $/// let ${snake_case $tname} = $tname {
119
  ${for fields { ${when any(fmeta(constructor(default)), not(fmeta(constructor)))}
120
    $///     $fname: /* optional field value */,
121
  }}
122
    $///     ..$CONSTRUCTOR_NAME {
123
  ${for fields { ${when not(any(fmeta(constructor(default)), not(fmeta(constructor))))}
124
    $///         $fname: /* required field value */,
125
  }}
126
    $///     }.construct()
127
    $/// };
128
    $/// ```
129
    #[allow(clippy::exhaustive_structs)]
130
    $tvis struct $CONSTRUCTOR_NAME<$tdefgens> where $twheres { $(
131
        ${when F_REQUIRED}
132

            
133
        ${fattrs doc}
134
        $fdefvis $fname: $ftype,
135
    ) }
136

            
137
    impl<$tgens> $CONSTRUCTOR where $twheres {
138
        $/// Construct a minimal `$tname`
139
        $///
140
        $/// In the returned [`$tname`],
141
        $/// optional fields all get the default values.
142
2
        $tvis fn construct(self) -> $ttype {
143
            $tname { $(
144
                $fname: ${select1
145
                    F_REQUIRED {
146
                        self.$fname
147
                    }
148
                    F_DEFAULT_TRAIT {
149
                        <$ftype as ::std::default::Default>::default()
150
                    }
151
                    F_DEFAULT_EXPR {
152
                        ${fmeta(constructor(default)) as expr}
153
                    }
154
                },
155
            ) }
156
        }
157
    }
158

            
159
    impl<$tgens> From<$CONSTRUCTOR> for $ttype where $twheres {
160
        fn from(constructor: $CONSTRUCTOR) -> $ttype {
161
            constructor.construct()
162
        }
163
    }
164
}
165

            
166
/// Macro to help check that netdoc items in a derive input are in the right order
167
///
168
/// Used only by the `NetdocParseable` derive-deftly macro.
169
#[doc(hidden)]
170
#[macro_export]
171
macro_rules! netdoc_ordering_check {
172
    { } => { compile_error!("netdoc must have an intro item so cannot be empty"); };
173

            
174
    // When we have   K0 P0 K1 P1 ...
175
    //   * Check that P0 and P1 have a consistent ordr
176
    //   * Continue with   K1 P1 ...
177
    // So we check each consecutive pair of fields.
178
    { $k0:ident $f0:ident $k1:ident $f1:ident $($rest:tt)* } => {
179
        $crate::netdoc_ordering_check! { <=? $k0 $k1 $f1 }
180
        $crate::netdoc_ordering_check! { $k1 $f1 $($rest)* }
181
    };
182
    { $k0:ident $f0:ident } => {}; // finished
183

            
184
    // Individual ordering checks for K0 <=? K1
185
    //
186
    // We write out each of the allowed this-kind next-kind combinations:
187
    { <=? intro     $any:ident $f1:ident } => {};
188
    { <=? normal    normal     $f1:ident } => {};
189
    { <=? normal    subdoc     $f1:ident } => {};
190
    { <=? subdoc    subdoc     $f1:ident } => {};
191
    // Not in the allowed list, must be an error:
192
    { <=? $k0:ident $k1:ident  $f1:ident } => {
193
        compile_error!(concat!(
194
            "in netdoc, ", stringify!($k1)," field ", stringify!($f1),
195
            " may not come after ", stringify!($k0),
196
        ));
197
    };
198
}
199

            
200
define_derive_deftly_module! {
201
    /// Common definitions for any netdoc derives
202
    NetdocDeriveAnyCommon beta_deftly:
203

            
204
    // Emit an eprintln with deftly(netdoc(debug)), just so that we don't get surprises
205
    // where someone leaves a (debug) in where it's not implemented, and we later implement it.
206
    ${define EMIT_DEBUG_PLACEHOLDER {
207
        ${if tmeta(netdoc(debug)) {
208
            // This messing about with std::io::stderr() mirrors netdoc_parseable_derive_debug.
209
            // (We could use eprintln! #[test] captures eprintln! but not io::stderr.)
210
            writeln!(
211
                std::io::stderr().lock(),
212
                ${concat "#[deftly(netdoc(debug))] applied to " $tname},
213
            ).expect("write to stderr failed");
214
        }}
215
    }}
216
    ${define DOC_DEBUG_PLACEHOLDER {
217
        /// * **`#[deftly(netdoc(debug))]`**:
218
        ///
219
        ///   Currently implemented only as a placeholder
220
        ///
221
        ///   The generated implementation may in future generate copious debug output
222
        ///   to the program's stderr when it is run.
223
        ///   Do not enable in production!
224
    }}
225
}
226

            
227
define_derive_deftly_module! {
228
    /// Common definitions for derives of structs containing items
229
    ///
230
    /// Used by `NetdocParseable`, `NetdocParseableFields`,
231
    /// `NetdocEncodable` and `NetdocEncodableFields`.
232
    ///
233
    /// Importing template must define these:
234
    ///
235
    ///  * **`F_INTRO`**, **`F_SUBDOC`**, **`F_SIGNATURE`**
236
    ///    conditions for the fundamental field kinds which aren't supported everywhere.
237
    ///
238
    ///    The `F_FLATTEN`, `F_SKIP`, `F_NORMAL` field type conditions are defined here.
239
    ///
240
    /// Importer must also import `NetdocDeriveAnyCommon`.
241
    //
242
    // We have the call sites import the other modules, rather than using them here, because:
243
    //  - This avoids the human reader having to chase breadcrumbs
244
    //    to find out what a particular template is using.
245
    //  - The dependency graph is not a tree, so some things would be included twice
246
    //    and derive-deftly cannot deduplicate them.
247
    NetdocSomeItemsDeriveCommon beta_deftly:
248

            
249
    // Is this field `flatten`?
250
    ${defcond F_FLATTEN fmeta(netdoc(flatten))}
251
    // Is this field `skip`?
252
    ${defcond F_SKIP fmeta(netdoc(skip))}
253
    // Is this field normal (non-structural)?
254
    ${defcond F_NORMAL not(any(F_SIGNATURE, F_INTRO, F_FLATTEN, F_SUBDOC, F_SKIP))}
255

            
256
    // Field keyword as `&str`
257
    ${define F_KEYWORD_STR { ${concat
258
        ${if any(F_FLATTEN, F_SUBDOC, F_SKIP) {
259
          ${if F_INTRO {
260
            ${error "#[deftly(netdoc(subdoc))] (flatten) and (skip) not supported for intro items"}
261
          } else {
262
            // Sub-documents and flattened fields have their keywords inside;
263
            // if we ask for the field-based keyword name for one of those then that's a bug.
264
            ${error "internal error, subdoc or skip KeywordRef"}
265
          }}
266
        }}
267
        ${fmeta(netdoc(keyword)) as str,
268
          default ${concat ${kebab_case $fname}}}
269
    }}}
270
    // Field keyword as `&str` for debugging and error reporting
271
    ${define F_KEYWORD_REPORT ${concat
272
        ${if any(F_FLATTEN, F_SUBDOC, F_SKIP) { $fname }
273
             else { $F_KEYWORD_STR }}
274
    }}
275
    // Field keyword as `KeywordRef`
276
    ${define F_KEYWORD { (KeywordRef::new_const($F_KEYWORD_STR)) }}
277
}
278

            
279
define_derive_deftly_module! {
280
    /// Common definitions for derives of whole network documents
281
    ///
282
    /// Used by `NetdocParseable` and `NetdocEncodable`.
283
    ///
284
    /// Importer must also import `NetdocSomeItemsDeriveCommon` and `NetdocDeriveAnyCommon`.
285
    NetdocEntireDeriveCommon beta_deftly:
286

            
287
    // Predicate for the toplevel
288
    ${defcond T_SIGNATURES false}
289

            
290
    // Predicates for the field kinds
291
    ${defcond F_INTRO all(not(T_SIGNATURES), approx_equal($findex, 0))}
292
    ${defcond F_SUBDOC fmeta(netdoc(subdoc))}
293
    ${defcond F_SIGNATURE T_SIGNATURES} // signatures section documents have only signature fields
294

            
295
    // compile-time check that fields are in the right order in the struct
296
    ${define FIELD_ORDERING_CHECK {
297
        ${if not(T_SIGNATURES) { // signatures structs have only signature fields
298
          netdoc_ordering_check! {
299
            $(
300
                ${when not(F_SKIP)}
301

            
302
                ${select1
303
                  F_INTRO     { intro     }
304
                  F_NORMAL    { normal    }
305
                  F_FLATTEN   { normal    }
306
                  F_SUBDOC    { subdoc    }
307
                }
308
                $fname
309
            )
310
          }
311
        }}
312
    }}
313
}
314

            
315
define_derive_deftly_module! {
316
    /// Common definitions for derives of flattenable network document fields structs
317
    ///
318
    /// Used by `NetdocParseableFields` and `NetdocEncodableFields`.
319
    ///
320
    /// Importer must also import `NetdocSomeItemsDeriveCommon` and `NetdocDeriveAnyCommon`.
321
    NetdocFieldsDeriveCommon beta_deftly:
322

            
323
    // Predicates for the field kinds, used by NetdocSomeItemsDeriveCommon etc.
324
    ${defcond F_INTRO false}
325
    ${defcond F_SUBDOC false}
326
    ${defcond F_SIGNATURE false}
327

            
328
    ${define DOC_NETDOC_FIELDS_DERIVE_SUPPORTED {
329
        ///  * The input struct can contain only normal non-structural items
330
        ///    (so it's not a sub-document with an intro item).
331
        ///  * The only attributes supported are the field attributes
332
        ///    `#[deftly(netdoc(keyword = STR))]`
333
        ///    `#[deftly(netdoc(default))]`
334
        ///    `#[deftly(netdoc(single_arg))]`
335
        ///    `#[deftly(netdoc(with = MODULE))]`
336
        ///    `#[deftly(netdoc(flatten))]`
337
        ///    `#[deftly(netdoc(skip))]`
338
    }}
339
}
340

            
341
define_derive_deftly_module! {
342
    /// Common definitions for derives of network document item value structs
343
    ///
344
    /// Used by `ItemValueParseable` and `ItemValueEncodable`.
345
    ///
346
    /// Importer must also import `NetdocDeriveAnyCommon`.
347
    NetdocItemDeriveCommon beta_deftly:
348

            
349
    ${defcond F_REST fmeta(netdoc(rest))}
350
    ${defcond F_OBJECT fmeta(netdoc(object))}
351
    ${defcond F_NORMAL not(any(F_REST, F_OBJECT))}
352

            
353
    ${defcond T_IS_SIGNATURE tmeta(netdoc(signature))}
354
}