miniconf_derive 0.15.0

Derive macros for `miniconf`
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use darling::{
    ast::{self, Data},
    util::Flag,
    Error, FromDeriveInput, FromVariant,
};
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::parse_quote;

use crate::field::TreeField;

#[derive(Debug, FromVariant, Clone)]
#[darling(attributes(tree), supports(newtype, tuple, unit), and_then=Self::parse)]
pub struct TreeVariant {
    ident: syn::Ident,
    rename: Option<syn::Ident>,
    skip: Flag,
    fields: ast::Fields<TreeField>,
}

impl TreeVariant {
    fn parse(mut self) -> darling::Result<Self> {
        assert!(!self.fields.is_struct());
        // unnamed fields can only be skipped if they are terminal
        while self
            .fields
            .fields
            .last()
            .map(|f| f.skip.is_present())
            .unwrap_or_default()
        {
            self.fields.fields.pop();
        }
        if let Some(f) = self.fields.iter().find(|f| f.skip.is_present()) {
            return Err(
                Error::custom("Can only `skip` terminal tuple struct fields")
                    .with_span(&f.skip.span()),
            );
        }
        Ok(self)
    }

    fn field(&self) -> &TreeField {
        // assert!(self.fields.is_newtype()); // Don't do this since we modified it with skip
        assert!(self.fields.len() == 1); // Only newtypes currently
        self.fields.fields.first().unwrap()
    }

    fn name(&self) -> &syn::Ident {
        self.rename.as_ref().unwrap_or(&self.ident)
    }
}

#[derive(Debug, FromDeriveInput, Clone)]
#[darling(attributes(tree), supports(any), and_then=Self::parse)]
#[darling()]
pub struct Tree {
    ident: syn::Ident,
    generics: syn::Generics,
    flatten: Flag,
    data: Data<TreeVariant, TreeField>,
}

impl Tree {
    fn parse(mut self) -> darling::Result<Self> {
        match &mut self.data {
            Data::Struct(fields) => {
                // unnamed fields can only be skipped if they are terminal
                while fields
                    .fields
                    .last()
                    .map(|f| f.ident.is_none() && f.skip.is_present())
                    .unwrap_or_default()
                {
                    fields.fields.pop();
                }
                fields
                    .fields
                    .retain(|f| f.ident.is_none() || !f.skip.is_present());
                if let Some(f) = fields.fields.iter().find(|f| f.skip.is_present()) {
                    return Err(
                        // Note(design) If non-terminal fields are skipped, there is a gap in the indices.
                        // This could be lifted with a index map.
                        Error::custom("Can only `skip` terminal tuple struct fields")
                            .with_span(&f.skip.span()),
                    );
                }
            }
            Data::Enum(variants) => {
                variants.retain(|v| !(v.skip.is_present() || v.fields.is_empty()));
                for v in variants.iter() {
                    if v.fields.len() != 1 {
                        return Err(Error::custom(
                            "Only newtype (single field tuple) and unit enum variants are supported.",
                        )
                        .with_span(&v.ident.span()));
                    }
                }
            }
        }
        if self.flatten.is_present() && self.fields().len() != 1 {
            return Err(Error::custom("Can't flatten multiple fields/variants")
                .with_span(&self.flatten.span()));
        }
        Ok(self)
    }

    fn level(&self) -> usize {
        if self.flatten.is_present() {
            0
        } else {
            1
        }
    }

    fn depth(&self) -> usize {
        let inner = self.fields().iter().fold(0, |d, field| d.max(field.depth));
        (self.level() + inner).max(1) // We need to eat at least one level. C.f. impl TreeKey for Option.
    }

    fn fields(&self) -> Vec<&TreeField> {
        match &self.data {
            Data::Struct(fields) => fields.iter().collect(),
            Data::Enum(variants) => variants.iter().map(|v| v.field()).collect(),
        }
    }

    fn bound_generics<F>(&self, func: &mut F) -> syn::Generics
    where
        F: FnMut(usize) -> Option<syn::TraitBound>,
    {
        let mut generics = self.generics.clone();
        for f in self.fields() {
            walk_type_params(f.typ(), func, f.depth, &mut generics);
        }
        generics
    }

    pub fn tree_key(&self) -> TokenStream {
        let depth = self.depth();
        let level = self.level();
        let ident = &self.ident;
        let generics = self.bound_generics(&mut |depth| {
            (depth > 0).then_some(parse_quote!(::miniconf::TreeKey<#depth>))
        });
        let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
        let fields = self.fields();
        let fields_len = fields.len();
        let metadata_arms = fields.iter().enumerate().map(|(i, f)| f.metadata(i));
        let traverse_arms = fields
            .iter()
            .enumerate()
            .filter_map(|(i, f)| f.traverse_by_key(i));
        let defers = fields.iter().map(|field| field.depth > 0);
        let names: Option<Vec<_>> = match &self.data {
            Data::Struct(fields) if fields.style.is_struct() => Some(
                fields
                    .iter()
                    .map(|f| {
                        // ident is Some
                        let name = f.name().unwrap();
                        quote_spanned! { name.span()=> stringify!(#name) }
                    })
                    .collect(),
            ),
            Data::Enum(variants) => Some(
                variants
                    .iter()
                    .map(|v| {
                        let name = v.name();
                        quote_spanned! { name.span()=> stringify!(#name) }
                    })
                    .collect(),
            ),
            _ => None,
        };
        let (names, name_to_index, index_to_name, mut ident_len) = if let Some(names) = names {
            (
                Some(quote!(
                    const __MINICONF_NAMES: [&'static str; #fields_len] = [#(#names ,)*];
                )),
                quote!(Self::__MINICONF_NAMES.iter().position(|&n| n == value)),
                quote!(::core::option::Option::Some(
                    *Self::__MINICONF_NAMES
                        .get(index)
                        .ok_or(::miniconf::Traversal::NotFound(1))?
                )),
                quote!(Self::__MINICONF_NAMES[index].len()),
            )
        } else {
            (
                None,
                quote!(str::parse(value).ok()),
                quote!(if index >= #fields_len {
                    ::core::result::Result::Err(::miniconf::Traversal::NotFound(1))?
                } else {
                    ::core::option::Option::None
                }),
                quote!(index.checked_ilog10().unwrap_or_default() as usize + 1),
            )
        };

        let (index, traverse, increment, lookup) = if self.flatten.is_present() {
            ident_len = quote!(0);
            (quote!(0), None, None, None)
        } else {
            (
                quote!(::miniconf::Keys::next::<Self>(&mut keys)?),
                Some(quote! {
                    func(index, #index_to_name, #fields_len).map_err(|err| ::miniconf::Error::Inner(1, err))?;
                }),
                Some(quote!(::miniconf::Error::increment_result)),
                Some(quote! {
                    #[automatically_derived]
                    impl #impl_generics #ident #ty_generics #where_clause {
                        #names
                    }

                    #[automatically_derived]
                    impl #impl_generics ::miniconf::KeyLookup for #ident #ty_generics #where_clause {
                        const LEN: usize = #fields_len;

                        #[inline]
                        fn name_to_index(value: &str) -> ::core::option::Option<usize> {
                            #name_to_index
                        }
                    }
                }),
            )
        };

        quote! {
            #[automatically_derived]
            impl #impl_generics #ident #ty_generics #where_clause {
                // TODO: can these be hidden and disambiguated w.r.t. collision?
                fn __miniconf_lookup<K: ::miniconf::Keys>(mut keys: K) -> ::core::result::Result<usize, ::miniconf::Traversal> {
                    const DEFERS: [bool; #fields_len] = [#(#defers ,)*];
                    let index = #index;
                    let defer = DEFERS.get(index)
                        .ok_or(::miniconf::Traversal::NotFound(1))?;
                    if !defer && !keys.finalize() {
                        ::core::result::Result::Err(::miniconf::Traversal::TooLong(1))
                    } else {
                        ::core::result::Result::Ok(index)
                    }
                }
            }

            #lookup

            #[automatically_derived]
            impl #impl_generics ::miniconf::TreeKey<#depth> for #ident #ty_generics #where_clause {
                fn metadata() -> ::miniconf::Metadata {
                    let mut meta = ::miniconf::Metadata::default();
                    let ident_len = |index: usize| { #ident_len };
                    #(#metadata_arms)*
                    meta.max_depth += #level;
                    meta
                }

                fn traverse_by_key<K, F, E>(mut keys: K, mut func: F) -> ::core::result::Result<usize, ::miniconf::Error<E>>
                where
                    K: ::miniconf::Keys,
                    F: ::core::ops::FnMut(usize, ::core::option::Option<&'static str>, usize) -> ::core::result::Result<(), E>,
                {
                    let index = #index;
                    #traverse
                    #increment(match index {
                        #(#traverse_arms ,)*
                        _ => {
                            if !keys.finalize() {
                                ::core::result::Result::Err(::miniconf::Traversal::TooLong(0).into())
                            } else {
                                ::core::result::Result::Ok(0)
                            }
                        }
                    })
                }
            }
        }
    }

    pub fn tree_serialize(&self) -> TokenStream {
        let depth = self.depth();
        let ident = &self.ident;
        let generics = self.bound_generics(&mut |depth| {
            Some(if depth > 0 {
                parse_quote!(::miniconf::TreeSerialize<#depth>)
            } else {
                parse_quote!(::miniconf::Serialize)
            })
        });

        let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
        let (mat, arms, default) = match &self.data {
            Data::Struct(fields) => (
                quote!(index),
                fields
                    .iter()
                    .enumerate()
                    .map(|(i, f)| {
                        let rhs = f.serialize_by_key(Some(i));
                        quote!(#i => #rhs)
                    })
                    .collect::<Vec<_>>(),
                quote!(::core::unreachable!()),
            ),
            Data::Enum(variants) => (
                quote!((self, index)),
                variants
                    .iter()
                    .enumerate()
                    .map(|(i, v)| {
                        let ident = &v.ident;
                        let rhs = v.field().serialize_by_key(None);
                        quote!((Self::#ident(value, ..), #i) => #rhs)
                    })
                    .collect(),
                quote!(::core::result::Result::Err(
                    ::miniconf::Traversal::Absent(0).into()
                )),
            ),
        };

        let increment = if self.flatten.is_present() {
            quote!()
        } else {
            quote!(::miniconf::Error::increment_result)
        };

        quote! {
            #[automatically_derived]
            impl #impl_generics ::miniconf::TreeSerialize<#depth> for #ident #ty_generics #where_clause {
                fn serialize_by_key<K, S>(&self, mut keys: K, ser: S) -> ::core::result::Result<usize, ::miniconf::Error<S::Error>>
                where
                    K: ::miniconf::Keys,
                    S: ::miniconf::Serializer,
                {
                    let index = Self::__miniconf_lookup(&mut keys)?;
                    // Note(unreachable) empty structs have diverged by now
                    #[allow(unreachable_code)]
                    #increment(match #mat {
                        #(#arms ,)*
                        _ => #default
                    })
                }
            }
        }
    }

    pub fn tree_deserialize(&self) -> TokenStream {
        let mut generics = self.bound_generics(&mut |depth| {
            Some(if depth > 0 {
                parse_quote!(::miniconf::TreeDeserialize<'de, #depth>)
            } else {
                parse_quote!(::miniconf::Deserialize<'de>)
            })
        });

        let depth = self.depth();
        let ident = &self.ident;

        let orig_generics = generics.clone();
        let (_, ty_generics, where_clause) = orig_generics.split_for_impl();
        let lts: Vec<_> = generics.lifetimes().cloned().collect();
        generics.params.push(parse_quote!('de));
        if let Some(syn::GenericParam::Lifetime(de)) = generics.params.last_mut() {
            assert_eq!(de.lifetime.ident, "de");
            for l in lts {
                assert!(l.lifetime.ident != "de");
                de.bounds.push(l.lifetime);
            }
        }
        let (impl_generics, _, _) = generics.split_for_impl();

        let (mat, arms, default) = match &self.data {
            Data::Struct(fields) => (
                quote!(index),
                fields
                    .iter()
                    .enumerate()
                    .map(|(i, f)| {
                        let rhs = f.deserialize_by_key(Some(i));
                        quote!(#i => #rhs)
                    })
                    .collect::<Vec<_>>(),
                quote!(::core::unreachable!()),
            ),
            Data::Enum(variants) => (
                quote!((self, index)),
                variants
                    .iter()
                    .enumerate()
                    .map(|(i, v)| {
                        let ident = &v.ident;
                        let rhs = v.field().deserialize_by_key(None);
                        quote!((Self::#ident(value, ..), #i) => #rhs)
                    })
                    .collect(),
                quote!(::core::result::Result::Err(
                    ::miniconf::Traversal::Absent(0).into()
                )),
            ),
        };

        let increment = if self.flatten.is_present() {
            quote!()
        } else {
            quote!(::miniconf::Error::increment_result)
        };

        quote! {
            #[automatically_derived]
            impl #impl_generics ::miniconf::TreeDeserialize<'de, #depth> for #ident #ty_generics #where_clause {
                fn deserialize_by_key<K, D>(&mut self, mut keys: K, de: D) -> ::core::result::Result<usize, ::miniconf::Error<D::Error>>
                where
                    K: ::miniconf::Keys,
                    D: ::miniconf::Deserializer<'de>,
                {
                    let index = Self::__miniconf_lookup(&mut keys)?;
                    // Note(unreachable) empty structs have diverged by now
                    #[allow(unreachable_code)]
                    #increment(match #mat {
                        #(#arms ,)*
                        _ => #default
                    })
                }
            }
        }
    }

    pub fn tree_any(&self) -> TokenStream {
        let generics = self.bound_generics(&mut |depth| {
            Some(if depth > 0 {
                parse_quote!(::miniconf::TreeAny<#depth>)
            } else {
                parse_quote!(::core::any::Any)
            })
        });

        let depth = self.depth();
        let ident = &self.ident;
        let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

        let (mat, (ref_arms, mut_arms), default) = match &self.data {
            Data::Struct(fields) => (
                quote!(index),
                fields
                    .iter()
                    .enumerate()
                    .map(|(i, f)| {
                        let (ref_rhs, mut_rhs) =
                            (f.ref_any_by_key(Some(i)), f.mut_any_by_key(Some(i)));
                        (quote!(#i => #ref_rhs), quote!(#i => #mut_rhs))
                    })
                    .unzip::<_, _, Vec<_>, Vec<_>>(),
                quote!(::core::unreachable!()),
            ),
            Data::Enum(variants) => (
                quote!((self, index)),
                variants
                    .iter()
                    .enumerate()
                    .map(|(i, v)| {
                        let ident = &v.ident;
                        let (ref_rhs, mut_rhs) = (
                            v.field().ref_any_by_key(None),
                            v.field().mut_any_by_key(None),
                        );
                        (
                            quote!((Self::#ident(value, ..), #i) => #ref_rhs),
                            quote!((Self::#ident(value, ..), #i) => #mut_rhs),
                        )
                    })
                    .unzip(),
                quote!(::core::result::Result::Err(
                    ::miniconf::Traversal::Absent(0).into()
                )),
            ),
        };

        let increment = if self.flatten.is_present() {
            quote!(ret)
        } else {
            quote!(ret.map_err(::miniconf::Traversal::increment))
        };

        quote! {
            #[automatically_derived]
            impl #impl_generics ::miniconf::TreeAny<#depth> for #ident #ty_generics #where_clause {
                fn ref_any_by_key<K>(&self, mut keys: K) -> ::core::result::Result<&dyn ::core::any::Any, ::miniconf::Traversal>
                where
                    K: ::miniconf::Keys,
                {
                    let index = Self::__miniconf_lookup(&mut keys)?;
                    // Note(unreachable) empty structs have diverged by now
                    #[allow(unreachable_code)]
                    {
                        let ret: ::core::result::Result<_, _> = match #mat {
                            #(#ref_arms ,)*
                            _ => #default
                        };
                        #increment
                    }
                }

                fn mut_any_by_key<K>(&mut self, mut keys: K) -> ::core::result::Result<&mut dyn ::core::any::Any, ::miniconf::Traversal>
                where
                    K: ::miniconf::Keys,
                {
                    let index = Self::__miniconf_lookup(&mut keys)?;
                    // Note(unreachable) empty structs have diverged by now
                    #[allow(unreachable_code)]
                    {
                        let ret: ::core::result::Result<_, _> = match #mat {
                            #(#mut_arms ,)*
                            _ => #default
                        };
                        #increment
                    }
                }
            }
        }
    }
}

fn walk_type_params<F>(typ: &syn::Type, func: &mut F, depth: usize, generics: &mut syn::Generics)
where
    F: FnMut(usize) -> Option<syn::TraitBound>,
{
    match typ {
        syn::Type::Path(syn::TypePath { path, .. }) => {
            if let Some(ident) = path.get_ident() {
                // The type is a single ident (no other path segments, has no generics):
                // call back if it is a generic type for us
                for generic in &mut generics.params {
                    if let syn::GenericParam::Type(type_param) = generic {
                        if &type_param.ident == ident {
                            if let Some(bound) = func(depth) {
                                type_param.bounds.push(bound.into());
                            }
                        }
                    }
                }
            } else {
                // Analyze the type parameters of the type, as they may be generics for us as well
                // This tries to reproduce the bounds that field types place on
                // their generic types, directly or indirectly. For this the API depth (the const generic
                // param to `TreeKey<Y>` etc) is determined as follows:
                //
                // Assume that all types use their generic T at
                // relative depth 1, i.e.
                // * if `#[tree(depth(Y > 1))] a: S<T>` then `T: Tree{Key,Serialize,Deserialize}<Y - 1>`
                // * else (that is if `Y = 1` or `a: S<T>` without `#[tree]`) then
                //   `T: serde::{Serialize,Deserialize}`
                //
                // And analogously for nested types `S<T<U>>` and `[[T; ..]; ..]` etc.
                // This is correct for all types in this library (Option, array, structs with the derive macro).
                //
                // The bounds are conservative (might not be required) and
                // fragile (might apply the wrong bound).
                // This matches the standard derive behavior and its issues
                // https://github.com/rust-lang/rust/issues/26925
                //
                // To fix this, one would extend the attribute syntax to allow overriding bounds.
                for seg in path.segments.iter() {
                    if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
                        for arg in args.args.iter() {
                            if let syn::GenericArgument::Type(typ) = arg {
                                // Found type argument in field type: recurse
                                walk_type_params(typ, func, depth.saturating_sub(1), generics);
                            }
                        }
                    }
                }
            }
        }
        syn::Type::Array(syn::TypeArray { elem, .. })
        | syn::Type::Slice(syn::TypeSlice { elem, .. }) => {
            // An array or slice places the element exactly one level deeper: recurse.
            walk_type_params(elem, func, depth.saturating_sub(1), generics);
        }
        syn::Type::Reference(syn::TypeReference { elem, .. }) => {
            // A reference is transparent
            walk_type_params(elem, func, depth, generics);
        }
        other => panic!("Unsupported type: {:?}", other),
    };
}