deluxe 0.5.0

Procedural macro attribute parser
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
//! # Deluxe
//!
//! A procedural macro attribute parser.
//!
//! ### Abstract
//!
//! This crate offers attribute parsing closer to the design of attributes in C#. It has an
//! interface similar to [serde](https://serde.rs). Attributes are written as plain Rust structs or
//! enums, and then parsers for them are generated automatically. They can contain arbitrary
//! expressions and can inherit from other attributes using a flattening mechanism.
//!
//! The parsers in this crate directly parse token streams using [`syn`]. As a result, most
//! built-in Rust types and `syn` types can be used directly as fields.
//!
//! ### Usage
//!
//! Functionality in this crate is centered around three traits, and their respective derive macros:
//! - **[`ExtractAttributes`](macro@ExtractAttributes)**
//!
//!   Extracts attributes from an object containing a list of [`syn::Attribute`], and parses them
//!   into a Rust type. Should be implemented for top-level structures that will be parsed directly
//!   out of a set of matching attributes.
//! - **[`ParseAttributes`](macro@ParseAttributes)**
//!
//!   Parses a Rust type from any object containing a list of [`syn::Attribute`]. Should be used if
//!   the set of matching attributes can potentially be shared between this type and other types.
//! - **[`ParseMetaItem`](macro@ParseMetaItem)**
//!
//!   Parses a Rust type from a [`ParseStream`](syn::parse::ParseStream). Should be implemented for
//!   any types that can be nested inside an attribute.
//!
//! Basic usage of this crate in derive macros requires simply deriving one (or a few) of these
//! traits, and then calling [`extract_attributes`] or [`parse_attributes`]. For more advanced
//! functionality, several `#[deluxe(...)]` attributes are supported on structs, enums, variants
//! and fields. See the examples below, and the documentation for each derive macro for a complete
//! description of the supported attributes.
//!
//! A list of field types supported by default can be seen in the list of provided [`ParseMetaItem`
//! implementations](trait@ParseMetaItem#foreign-impls). For more complex usage, manual
//! implementations of these traits can be provided. See the documentation on individual traits in
//! [`deluxe_core`] for more details on how to manually implement your own parsers.
//!
//! ### Related Crates
//!
//! Deluxe takes inspiration from the [darling](https://docs.rs/darling) crate, but offers a few
//! enhancements over it. Darling is built around pre-parsed [`syn::Meta`] objects, and therefore
//! is restricted to the [meta
//! syntax](https://doc.rust-lang.org/stable/reference/attributes.html#meta-item-attribute-syntax).
//! Deluxe parses its types directly from [`TokenStream`](proc_macro2::TokenStream) objects in the
//! attributes and so is able to use any syntax that parses as a valid token tree. Deluxe also does
//! not provide extra traits for parsing special `syn` objects like
//! [`DeriveInput`](syn::DeriveInput) and [`Field`](syn::Field). Instead, Deluxe uses a generic
//! trait to parse from any type containing a <code>[Vec]&lt;[syn::Attribute]></code>.
//!
//! ### Examples
//!
//! #### Basic Derive Macro
//!
//! To create a derive macro that can add some simple metadata to a Rust type from an attribute,
//! start by defining a struct that derives [`ExtractAttributes`](macro@ExtractAttributes). Then,
//! call [`extract_attributes`] in your derive macro to create an instance of the struct:
//!
//! ```
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(my_desc))]
//! struct MyDescription {
//!     name: String,
//!     version: String,
//! }
//!
//! fn my_derive(item: proc_macro2::TokenStream) -> deluxe::Result<proc_macro2::TokenStream> {
//!     let mut input = syn::parse2::<syn::DeriveInput>(item)?;
//!
//!     // Extract the attributes!
//!     let MyDescription { name, version } = deluxe::extract_attributes(&mut input)?;
//!
//!     // Now get some info to generate an associated function...
//!     let ident = &input.ident;
//!     let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
//!
//!     Ok(quote::quote! {
//!         impl #impl_generics #ident #type_generics #where_clause {
//!             fn my_desc() -> &'static str {
//!                 concat!("Name: ", #name, ", Version: ", #version)
//!             }
//!         }
//!     })
//! }
//!
//! # let tokens = my_derive(quote::quote! {
//! #     #[my_desc(name = "hello world", version = "0.2")]
//! #     struct Hello;
//! # }).unwrap();
//! # let i: syn::ItemImpl = syn::parse_quote! { #tokens };
//! # assert_eq!(i, syn::parse_quote! {
//! #     impl Hello {
//! #         fn my_desc() -> &'static str {
//! #             concat!("Name: ", "hello world", ", Version: ", "0.2")
//! #         }
//! #     }
//! # });
//! ```
//!
//! Then, try adding the attribute in some code that uses your macro:
//!
//! ```ignore
//! // In your macros crate
//!
//! #[proc_macro_derive(MyDescription, attributes(my_desc))]
//! pub fn derive_my_description(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
//!     my_derive(item.into()).unwrap().into()
//! }
//! ```
//!
//! ```ignore
//! // In your normal code
//!
//! #[derive(MyDescription, Default)]
//! #[my_desc(name = "hello world", version = "0.2")]
//! struct Hello {
//!     a: i32,
//!     b: String
//! }
//!
//! let hello: Hello = Default::default();
//! assert_eq!(hello.my_desc(), "Name: hello world, Version: 0.2");
//! ```
//!
//! #### Basic Attribute Macro
//!
//! The `parse` and `parse2` functions included in this crate can also be used as simple helpers
//! for attribute macros:
//!
//! ```
//! #[derive(deluxe::ParseMetaItem)]
//! struct MyDescription {
//!     name: String,
//!     version: String,
//! }
//!
//! fn my_desc_attr(
//!     attr: proc_macro2::TokenStream,
//!     item: proc_macro2::TokenStream,
//! ) -> deluxe::Result<proc_macro2::TokenStream> {
//!     let MyDescription { name, version } = deluxe::parse2::<MyDescription>(attr)?;
//!
//!     Ok(quote::quote! {
//!         fn my_desc() -> &'static str {
//!             concat!("Name: ", #name, ", Version: ", #version)
//!         }
//!         #item
//!     })
//! }
//!
//! # let tokens = my_desc_attr(
//! #     quote::quote!(name = "hello world", version = "0.2"),
//! #     quote::quote!(),
//! # ).unwrap();
//! # let f: syn::ItemFn = syn::parse_quote! { #tokens };
//! # assert_eq!(f, syn::parse_quote! {
//! #     fn my_desc() -> &'static str {
//! #         concat!("Name: ", "hello world", ", Version: ", "0.2")
//! #     }
//! # });
//! ```
//!
//! ```ignore
//! // In your macros crate
//!
//! #[proc_macro_attribute]
//! pub fn my_desc(
//!     attr: proc_macro::TokenStream,
//!     item: proc_macro::TokenStream,
//! ) -> proc_macro::TokenStream {
//!     my_desc_attr(attr.into(), item.into()).unwrap().into()
//! }
//! ```
//!
//! ```ignore
//! // In your normal code
//!
//! #[my_desc(name = "hello world", version = "0.2")]
//! fn nothing() {}
//!
//! assert_eq!(my_desc(), "Name: hello world, Version: 0.2");
//! ```
//!
//! #### Field Attributes
//!
//! The attributes [`alias`](macro@ParseMetaItem#deluxealias--ident-1),
//! [`default`](macro@ParseMetaItem#deluxedefault-1),
//! [`rename`](macro@ParseMetaItem#deluxerename--ident-1), and
//! [`skip`](macro@ParseMetaItem#deluxeskip-1) are supported, and behave the same as in Serde. The
//! [`append`](macro@ParseMetaItem#deluxeappend) attribute can be used on [`Vec`] fields to
//! aggregate all duplicates of a key. The [`rest`](macro@ParseMetaItem#deluxerest) attribute can
//! be used to do custom processing on any unknown keys.
//!
//! ```
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(my_object))]
//! struct MyObject {
//!     // Can be specified with key `id` or `object_id`
//!     #[deluxe(alias = object_id)]
//!     id: u64,
//!
//!     // Field is optional, defaults to `Default::default` if not present
//!     #[deluxe(default)]
//!     count: u64,
//!
//!     // Defaults to "Empty" if not present
//!     #[deluxe(default = String::from("Empty"))]
//!     contents: String,
//!
//!     // Can be specified only with key `name`
//!     #[deluxe(rename = name)]
//!     s: String,
//!
//!     // Skipped during parsing entirely
//!     #[deluxe(skip)]
//!     internal_flag: bool,
//!
//!     // Appends any extra fields with the key `expr` to the Vec
//!     #[deluxe(append, rename = expr)]
//!     exprs: Vec<syn::Expr>,
//!
//!     // Adds any unknown keys to the hash map
//!     #[deluxe(rest)]
//!     rest: std::collections::HashMap<syn::Path, syn::Expr>,
//! }
//! ```
//!
//! ```ignore
//! // Omitted fields will be set to defaults
//! #[derive(MyObject)]
//! #[my_object(id = 1, name = "First", expr = 1 + 2, count = 3)]
//! struct FirstObject;
//!
//! // `expr` can be specified multiple times because of the `append` attribute
//! #[derive(MyObject)]
//! #[my_object(object_id = 2, name = "Second", expr = 1 + 2, expr = 3 + 4)]
//! struct SecondObject;
//!
//! // `unknown` and `extra` will be stored in the `rest` hashmap
//! #[derive(MyObject)]
//! #[my_object(id = 3, name = "Third", unknown = 1 + 2, extra = 3 + 4)]
//! struct ThirdObject;
//! ```
//!
//! #### Inheritance
//!
//! The [`flatten`](macro@ParseMetaItem#deluxeflatten-1) attribute can be used to parse keys from
//! one structure inside another:
//!
//! ```
//! #[derive(deluxe::ParseMetaItem)]
//! struct A {
//!     id: u64,
//! }
//!
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(b))]
//! struct B {
//!     #[deluxe(flatten)]
//!     a: A,
//!     name: String,
//! }
//! ```
//!
//! Then, fields from both `A` and `B` can be used when deriving `B`:
//!
//! ```ignore
//! #[derive(B)]
//! #[b(id = 123, name = "object")]
//! struct Object;
//! ```
//!
//! #### Attributes in Nested Code
//!
//! Extra attributes can be taken from within the code block attached to a macro. When used in an
//! attribute macro, the attributes should be consumed so as not to produce an "unknown attribute"
//! error when outputting tokens.
//!
//! ```
//! #[derive(deluxe::ParseMetaItem, deluxe::ExtractAttributes)]
//! struct MyDescription {
//!     name: String,
//!     version: String,
//! }
//!
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(author))]
//! struct Authors(#[deluxe(flatten)] Vec<String>);
//!
//! fn my_derive(item: proc_macro2::TokenStream) -> deluxe::Result<proc_macro2::TokenStream> {
//!     let mut input = syn::parse2::<syn::DeriveInput>(item)?;
//!     let MyDescription { name, version } = deluxe::extract_attributes(&mut input)?;
//!     let mut authors = Vec::new();
//!     if let syn::Data::Struct(s) = &mut input.data {
//!          // Look through all fields in the struct for `author` attributes
//!         for field in s.fields.iter_mut() {
//!             let Authors(a) = deluxe::extract_attributes(field)?;
//!             authors.extend(a);
//!         }
//!     }
//!
//!     let ident = &input.ident;
//!     let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
//!
//!     Ok(quote::quote! {
//!         impl #impl_generics #ident #type_generics #where_clause {
//!             fn my_desc() -> &'static str {
//!                 concat!("Name: ", #name, ", Version: ", #version #(, ", Author: ", #authors)*)
//!             }
//!         }
//!     })
//! }
//!
//! # let tokens = my_derive(quote::quote! {
//! #     #[my_desc(name = "hello world", version = "0.2")]
//! #     struct Hello(#[author("Alice")] String);
//! # }).unwrap();
//! # let i: syn::ItemImpl = syn::parse_quote! { #tokens };
//! # assert_eq!(i, syn::parse_quote! {
//! #     impl Hello {
//! #         fn my_desc() -> &'static str {
//! #             concat!("Name: ", "hello world", ", Version: ", "0.2", ", Author: ", "Alice")
//! #         }
//! #     }
//! # });
//!
//! fn my_desc_mod(
//!     attr: proc_macro2::TokenStream,
//!     item: proc_macro2::TokenStream,
//! ) -> deluxe::Result<proc_macro2::TokenStream> {
//!     let MyDescription { name, version } = deluxe::parse2::<MyDescription>(attr)?;
//!     let mut authors = Vec::new();
//!     let mut module = syn::parse2::<syn::ItemMod>(item)?;
//!
//!     let (_, items) = module.content.as_mut().unwrap();
//!
//!     // Look through all items in the module for `author` attributes
//!     for i in items.iter_mut() {
//!         // Extract the attributes to remove them from the final output
//!         let Authors(a) = deluxe::extract_attributes(i)?;
//!         authors.extend(a);
//!     }
//!
//!     // Place a new function inside the module
//!     items.push(syn::parse_quote! {
//!         fn my_desc() -> &'static str {
//!             concat!("Name: ", #name, ", Version: ", #version #(, ", Author: ", #authors)*)
//!         }
//!     });
//!
//!     Ok(quote::quote! { #module })
//! }
//!
//! # let tokens = my_desc_mod(
//! #     quote::quote!(name = "hello world", version = "0.2"),
//! #     quote::quote!(mod abc {
//! #         #[author("Alice", "Bob")]
//! #         fn func1() {}
//! #         #[author("Carol")]
//! #         #[author("Dave")]
//! #         fn func2() {}
//! #     }),
//! # ).unwrap();
//! # let m: syn::ItemMod = syn::parse_quote! { #tokens };
//! # assert_eq!(m, syn::parse_quote! {
//! #     mod abc {
//! #         fn func1() {}
//! #         fn func2() {}
//! #         fn my_desc() -> &'static str {
//! #             concat!(
//! #                 "Name: ", "hello world", ", Version: ", "0.2",
//! #                 ", Author: ", "Alice", ", Author: ", "Bob",
//! #                 ", Author: ", "Carol", ", Author: ", "Dave"
//! #             )
//! #         }
//! #     }
//! # });
//! ```
//!
//! ```ignore
//! // In your normal code
//!
//! #[derive(MyDescription, Default)]
//! #[my_desc(name = "hello world", version = "0.2")]
//! struct Hello {
//!     #[author("Alice")]
//!     a: i32,
//!     #[author("Bob")]
//!     b: String
//! }
//!
//! let hello: Hello = Default::default();
//! assert_eq!(hello.my_desc(), "Name: hello world, Version: 0.2, Author: Alice, Author: Bob");
//!
//! #[my_desc_mod(name = "hello world", version = "0.2")]
//! mod abc {
//!     #[author("Alice", "Bob")]
//!     fn func1() {}
//!
//!     #[author("Carol")]
//!     #[author("Dave")]
//!     fn func2() {}
//! }
//!
//! assert_eq!(
//!     abc::my_desc(),
//!     "Name: hello world, Version: 0.2, Author: Alice, Author: Bob, Author: Carol, Author: Dave"
//! );
//! ```
//!
//! #### Tuple Structs, Tuples and Vecs
//!
//! Deluxe also supports parsing into data structures with unnamed fields.
//!
//! ```
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(my_tuple))]
//! struct MyTuple(u64, String);
//!
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(my_idents))]
//! struct MyIdents {
//!     id: u64,
//!     names: (String, String),
//!     idents: Vec<syn::Ident>
//! }
//! ```
//!
//! The standard attribute syntax with parenthesis can be used when specifying a [`Vec`] type. The
//! alternative syntax `key = [...]` can also be used to have an appearance similar to an array
//! literal.
//!
//! ```ignore
//! #[derive(MyTuple)]
//! #[my_tuple(123, "object")]
//! struct Object;
//!
//! #[derive(MyIdents)]
//! #[my_idents(id = 7, names("hello", "world"), idents(a, b, c))]
//! struct ABC;
//!
//! // `idents` contains same values as above
//! #[derive(MyIdents)]
//! #[my_idents(id = 7, names("hello", "world"), idents = [a, b, c])]
//! struct ABC2;
//! ```
//!
//! #### C#-styled Attributes
//!
//! Attributes in C# can support positional arguments first with the named
//! arguments afterwards. This style can be emulated by using a tuple struct with a
//! normal struct flattened at the end. Placing
//! [`#[deluxe(default)]`](macro@ParseMetaItem#deluxedefault) on the struct behaves the same as
//! Serde, by filling in all fields with values from [`Default`], allowing every named argument to
//! be optional.
//!
//! ```
//! #[derive(deluxe::ParseMetaItem, Default)]
//! #[deluxe(default)]
//! struct Flags {
//!     native: bool,
//! }
//!
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(a))]
//! struct A(u64, String, #[deluxe(flatten)] Flags);
//! ```
//!
//! ```ignore
//! #[derive(A)]
//! #[a(123, "object")]
//! struct Object;
//!
//! #[derive(A)]
//! #[a(123, "native-object", native = true)]
//! struct NativeObject;
//! ```
//!
//! #### Enums
//!
//! Enums are supported by using the variant name as a single key, in snake-case. Variants can be
//! renamed, aliased and skipped in the same way as fields.
//!
//! ```
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(my_enum))]
//! enum MyEnum {
//!     A,
//!     B,
//!     C,
//!     #[deluxe(alias = d)]
//!     AnotherOne,
//!     #[deluxe(rename = e)]
//!     AnotherTwo,
//!     #[deluxe(skip)]
//!     SkipMe
//! }
//! ```
//!
//! ```ignore
//! #[derive(MyEnum)]
//! #[my_enum(b)]
//! struct ObjectB;
//!
//! #[derive(MyEnum)]
//! #[my_enum(another_one)]
//! struct ObjectD;
//! ```
//!
//! #### Complex Enums
//!
//! Enums with struct and tuple variants are also supported. The data inside is used as arguments
//! to the attribute. All field attributes from structs are also supported inside variants.
//!
//! Additionally, enum variants with named fields can be flattened. The behavior of a flattened
//! variant is similar to Serde's `untagged` mode. In a flattened variant, the name of the variant
//! will be ignored. Instead, Deluxe will attempt to use the unique keys in each variant to
//! determine if that variant was specified. A compile error will be thrown if it is not possible
//! to determine a unique, unambiguous key between two variants.
//!
//! ```
//! #[derive(deluxe::ExtractAttributes)]
//! #[deluxe(attributes(my_enum))]
//! enum MyEnum {
//!     A,
//!     B(u64, String),
//!     C { id: u64, name: String },
//!     #[deluxe(flatten)]
//!     D { d: u64, name: String },
//! }
//! ```
//!
//! ```ignore
//! #[derive(MyEnum)]
//! #[my_enum(a)]
//! struct ObjectA;
//!
//! #[derive(MyEnum)]
//! #[my_enum(b(1, "hello"))]
//! struct ObjectB;
//!
//! #[derive(MyEnum)]
//! #[my_enum(c(id = 2, name = "world"))]
//! struct ObjectC;
//!
//! // No inner parenthesis needed here due to flattening
//! #[derive(MyEnum)]
//! #[my_enum(d = 3, name = "moon")]
//! struct ObjectD;
//! ```
//!
//! #### Storing Containers
//!
//! During parsing, Deluxe can store references to the container type holding the attributes for
//! easier access. Container fields are skipped during attribute parsing.
//!
//! ```
//! #[derive(deluxe::ParseAttributes)]
//! #[deluxe(attributes(my_object))]
//! struct MyObject<'t> {
//!     id: u64,
//!     // Fill `container` in using the parsed type. Note this restricts the
//!     // derived `ParseAttributes` impl so it can only be used on `DeriveInput`.
//!     #[deluxe(container)]
//!     container: &'t syn::DeriveInput,
//! }
//!
//! fn my_object(item: proc_macro2::TokenStream) -> deluxe::Result<proc_macro2::TokenStream> {
//!     let input = syn::parse2::<syn::DeriveInput>(item)?;
//!
//!     // `obj.container` now holds a reference to `input`
//!     let obj: MyObject = deluxe::parse_attributes(&input)?;
//!
//!     Ok(quote::quote! { /* ... generate some code here ... */ })
//! }
//! ```
//!
//! To support both extracting and parsing, a container field can also be a value type. In that
//! case, the container will be cloned into the structure.

#![deny(missing_docs)]
#![deny(unsafe_code)]

#[doc(hidden)]
pub mod ____private {
    pub use deluxe_core::parse_helpers::{self, FieldStatus, SmallString};
    pub use once_cell::sync::OnceCell as SyncOnceCell;
    pub use proc_macro2::Span;
    pub use std::{
        borrow::{Borrow, Cow, ToOwned},
        clone::Clone,
        collections::HashMap,
        convert::{AsRef, From},
        default::Default,
        format, format_args,
        iter::IntoIterator,
        option::Option,
        primitive::{bool, str, usize},
        string::{String, ToString},
        unreachable,
        vec::Vec,
    };
    pub use syn::{
        parse::{ParseBuffer, ParseStream},
        Error, Ident, Path,
    };
}

pub use deluxe_core::{
    define_with_collection, define_with_map, define_with_optional, parse_named_meta_item_with,
    with, Error, Errors, ExtractAttributes, Flag, HasAttributes, ParseAttributes, ParseMetaItem,
    ParseMode, Result, SpannedValue,
};
#[doc(hidden)]
pub use deluxe_core::{
    ContainerFrom, ParseMetaAppend, ParseMetaFlatNamed, ParseMetaFlatUnnamed, ParseMetaRest,
    ToKeyString,
};
pub use deluxe_macros::*;

/// Additional helper functions for validating after parsing.
pub mod validations {
    pub use deluxe_core::validations::*;
    pub use deluxe_core::{all_or_none, only_one};
}

#[cfg(feature = "proc-macro")]
extern crate proc_macro;

/// Parses a required Rust type out of a token stream.
///
/// Intended for use with [attribute
/// macros](https://doc.rust-lang.org/stable/reference/procedural-macros.html#attribute-macros).
/// This is a small wrapper around [`syn::parse::Parser::parse`] and
/// [`ParseMetaItem::parse_meta_item_inline`].
#[cfg(feature = "proc-macro")]
#[inline]
pub fn parse<T: ParseMetaItem>(attr: proc_macro::TokenStream) -> Result<T> {
    syn::parse::Parser::parse(
        |stream: syn::parse::ParseStream<'_>| {
            T::parse_meta_item_inline(&[stream], ParseMode::Named(proc_macro2::Span::call_site()))
        },
        attr,
    )
}

/// Parses a required Rust type out of a token stream.
///
/// Intended for use with [attribute
/// macros](https://doc.rust-lang.org/stable/reference/procedural-macros.html#attribute-macros).
/// This is a small wrapper around [`syn::parse::Parser::parse2`] and
/// [`ParseMetaItem::parse_meta_item_inline`].
#[inline]
pub fn parse2<T: ParseMetaItem>(attr: proc_macro2::TokenStream) -> Result<T> {
    syn::parse::Parser::parse2(
        |stream: syn::parse::ParseStream<'_>| {
            T::parse_meta_item_inline(&[stream], ParseMode::Named(proc_macro2::Span::call_site()))
        },
        attr,
    )
}

/// Parses a required Rust type out of another type holding a list of [`syn::Attribute`].
///
/// Intended for use with [derive
/// macros](https://doc.rust-lang.org/stable/reference/procedural-macros.html#derive-macros). This
/// is a small wrapper around [`ParseAttributes::parse_attributes`].
#[inline]
pub fn parse_attributes<'t, T, R>(obj: &'t T) -> Result<R>
where
    T: HasAttributes,
    R: ParseAttributes<'t, T>,
{
    R::parse_attributes(obj)
}

/// Extracts attributes out of another type holding a list of [`syn::Attribute`], then parses them
/// into a required Rust type.
///
/// Intended for use with [derive
/// macros](https://doc.rust-lang.org/stable/reference/procedural-macros.html#derive-macros). This
/// is a small wrapper around [`ExtractAttributes::extract_attributes`].
#[inline]
pub fn extract_attributes<T, R>(obj: &mut T) -> Result<R>
where
    T: HasAttributes,
    R: ExtractAttributes<T>,
{
    R::extract_attributes(obj)
}

/// Parses a Rust type out of a token stream, returning a default value on failure.
///
/// Calls [`parse`] and returns the result. Upon failure, [`Default::default`] will be returned and
/// any errors will be appended to `errors`. This function should be preferred when parsing
/// multiple attributes, so the errors from all of them can be accumulated instead of returning
/// early.
#[cfg(feature = "proc-macro")]
#[inline]
pub fn parse_optional<T: ParseMetaItem + Default>(
    attr: proc_macro::TokenStream,
    errors: &Errors,
) -> T {
    match parse(attr) {
        Ok(t) => t,
        Err(e) => {
            errors.push_syn(e);
            Default::default()
        }
    }
}

/// Parses a Rust type out of a token stream, returning a default value on failure.
///
/// Calls [`parse2`] and returns the result. Upon failure, [`Default::default`] will be returned
/// and any errors will be appended to `errors`. This function should be preferred when parsing
/// multiple attributes, so the errors from all of them can be accumulated instead of returning
/// early.
#[inline]
pub fn parse2_optional<T: ParseMetaItem + Default>(
    attr: proc_macro2::TokenStream,
    errors: &Errors,
) -> T {
    match parse2(attr) {
        Ok(t) => t,
        Err(e) => {
            errors.push_syn(e);
            Default::default()
        }
    }
}

/// Parses a Rust type out of another type holding a list of [`syn::Attribute`], returning a default value on failure.
///
/// Calls [`parse_attributes`] and returns the result. Upon failure, [`Default::default`] will be
/// returned and any errors will be appended to `errors`. This function should be preferred when
/// parsing multiple attributes, so the errors from all of them can be accumulated instead of
/// returning early.
#[inline]
pub fn parse_attributes_optional<'t, T, R>(obj: &'t T, errors: &Errors) -> R
where
    T: HasAttributes,
    R: ParseAttributes<'t, T> + Default,
{
    match parse_attributes(obj) {
        Ok(t) => t,
        Err(e) => {
            errors.push_syn(e);
            Default::default()
        }
    }
}

/// Extracts attributes out of another type holding a list of [`syn::Attribute`], then parses them
/// into a Rust type, returning a default value on failure.
///
/// Calls [`extract_attributes`] and returns the result. Upon failure, [`Default::default`] will be
/// returned and any errors will be appended to `errors`. This function should be preferred when
/// parsing multiple attributes, so the errors from all of them can be accumulated instead of
/// returning early.
#[inline]
pub fn extract_attributes_optional<T, R>(obj: &mut T, errors: &Errors) -> R
where
    T: HasAttributes,
    R: ExtractAttributes<T> + Default,
{
    match extract_attributes(obj) {
        Ok(t) => t,
        Err(e) => {
            errors.push_syn(e);
            Default::default()
        }
    }
}