xmlity-derive 0.0.9

Derive proc-macros for xmlity.
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
//! Options for both structs and enum variants
use super::*;

pub mod roots {
    use syn::{parse_quote, Attribute, Path};

    use crate::common::Prefix;

    use super::*;

    #[derive(FromAttributes, Clone)]
    #[darling(attributes(xelement))]
    pub struct RootElementOpts {
        /// The name to serialize to and deserialize from.
        ///
        /// If not specified, the name of the struct or enum variant is used.
        #[darling(default)]
        pub name: Option<LocalName<'static>>,
        /// The namespace of the element, defined as a string.
        ///
        /// This is exclusive with [`namespace_expr`].
        ///
        /// If none of these are specified, the absence of a namespace is assumed.
        #[darling(default)]
        pub namespace: Option<XmlNamespace<'static>>,
        /// The namespace of the element given as an expression to an [`xmlity::XmlNamespace`] value.
        ///
        /// This is exclusive with [`namespace`].
        ///
        /// If none of these are specified, the absence of a namespace is assumed.
        #[darling(default)]
        pub namespace_expr: Option<Expr>,
        /// The element is serialized with the given prefix.
        ///
        /// *Serialize only*
        #[darling(default)]
        pub preferred_prefix: Option<Prefix<'static>>,
        /// Always set the prefix of the element to the prefix set in `preferred_prefix`.
        ///
        /// *Serialize only*
        #[darling(default)]
        pub enforce_prefix: bool,
        /// Allow unknown children when deserializing.
        /// - `Any`: Allow any unknown children.
        /// - `AtEnd` (*default*): Allow unknown children only at the end of the element.
        /// - `None`: Do not allow unknown children at all.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub allow_unknown_children: AllowUnknown,
        /// Allow unknown attributes when deserializing.
        /// - `Any`: Allow any unknown attributes.
        /// - `AtEnd` (*default*): Allow unknown attributes only at the end of the element.
        /// - `None`: Do not allow unknown attributes at all.
        ///
        /// Default is `AtEnd`.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub allow_unknown_attributes: AllowUnknown,
        /// Allow any name for the element when deserializing.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub deserialize_any_name: bool,
        /// Set if the order of attributes is important when serializing or deserializing.
        /// - `Strict`: The order of attributes must match the order in the struct or enum variant.
        /// - `None` (*default*): The order of attributes does not matter, but the attributes must be present.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub attribute_order: ElementOrder,
        /// Set if the order of children is important when serializing or deserializing.
        /// - `Strict`: The order of children must match the order in the struct or enum variant.
        /// - `None` (*default*): The order of children does not matter, but the children must be present.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub children_order: ElementOrder,
        /// Set if whitespace should be ignored when deserializing.
        /// - `Any` (*default*): Ignore any whitespace.
        /// - `None`: Do not ignore whitespace.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub ignore_whitespace: IgnoreWhitespace,
        /// Set if comments should be ignored when deserializing.
        /// - `Any` (*default*): Ignore any comments.
        /// - `None`: Do not ignore comments.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub ignore_comments: IgnoreComments,
    }

    impl RootElementOpts {
        pub fn parse(attrs: &[Attribute]) -> Result<Option<Self>, DeriveError> {
            let Some(attr) = attrs.iter().find(|attr| attr.path().is_ident("xelement")) else {
                return Ok(None);
            };

            let opts = Self::from_attributes(std::slice::from_ref(attr))?;
            if opts.namespace_expr.is_some() && opts.namespace.is_some() {
                return Err(DeriveError::custom(
                    "Cannot specify both `namespace` and `namespace_expr`",
                ));
            }
            Ok(Some(opts))
        }
    }

    impl WithExpandedName for RootElementOpts {
        fn name(&self) -> Option<LocalName<'_>> {
            self.name.clone()
        }

        fn namespace(&self) -> Option<XmlNamespace<'_>> {
            self.namespace.clone()
        }

        fn namespace_expr(&self) -> Option<Expr> {
            self.namespace_expr.clone()
        }
    }

    #[derive(FromAttributes, Clone)]
    #[darling(attributes(xattribute))]
    pub struct RootAttributeOpts {
        /// The name to serialize to and deserialize from.
        ///
        /// If not specified, the name of the struct is used.
        #[darling(default)]
        pub name: Option<LocalName<'static>>,
        /// The namespace of the attribute, defined as a string.
        ///
        /// This is exclusive with [`namespace_expr`].
        ///
        /// If none of these are specified, the absence of a namespace is assumed.
        #[darling(default)]
        pub namespace: Option<XmlNamespace<'static>>,
        /// The namespace of the attribute given as an expression to an [`xmlity::XmlNamespace`] value.
        ///
        /// This is exclusive with [`namespace`].
        ///
        /// If none of these are specified, the absence of a namespace is assumed.
        #[darling(default)]
        pub namespace_expr: Option<Expr>,
        /// The preferred prefix for the attribute, defined as a string.
        ///
        /// This is exclusive with [`enforce_prefix`].
        ///
        /// If none of these are specified, the absence of a prefix is assumed.
        ///
        /// *Serialize only*
        #[darling(default)]
        pub preferred_prefix: Option<Prefix<'static>>,
        /// Always set the prefix of the attribute to the prefix set in `preferred_prefix`.
        ///
        /// *Serialize only*
        #[darling(default)]
        pub enforce_prefix: bool,
        /// Always set the prefix of the attribute to the prefix set in `preferred_prefix`.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub deserialize_any_name: bool,
    }

    impl RootAttributeOpts {
        pub fn parse(attrs: &[Attribute]) -> Result<Option<Self>, DeriveError> {
            let Some(attr) = attrs.iter().find(|attr| attr.path().is_ident("xattribute")) else {
                return Ok(None);
            };

            let opts = Self::from_attributes(std::slice::from_ref(attr))?;
            Ok(Some(opts))
        }
    }

    impl WithExpandedName for RootAttributeOpts {
        fn name(&self) -> Option<LocalName<'_>> {
            self.name.clone()
        }

        fn namespace(&self) -> Option<XmlNamespace<'_>> {
            self.namespace.clone()
        }

        fn namespace_expr(&self) -> Option<Expr> {
            self.namespace_expr.clone()
        }
    }

    #[derive(Default, FromAttributes)]
    #[darling(attributes(xvalue))]
    pub struct RootValueOpts {
        /// The text value to serialize to and deserialize from.
        pub value: Option<String>,
        #[darling(default)]
        /// Set if whitespace should be ignored when deserializing.
        /// - `Any` (*default*): Ignore any whitespace.
        /// - `None`: Do not ignore whitespace.
        ///
        /// *Deserialize only*
        pub ignore_whitespace: IgnoreWhitespace,
        #[darling(default)]
        /// Set if comments should be ignored when deserializing.
        /// - `Any` (*default*): Ignore any comments.
        /// - `None`: Do not ignore comments.
        ///
        /// *Deserialize only*
        pub ignore_comments: IgnoreComments,
        /// Allow unknown values when deserializing.
        /// - `Any`: Allow any unknown values.
        /// - `AtEnd` (*default*): Allow unknown values only at the end of the element.
        /// - `None`: Do not allow unknown values at all.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub allow_unknown: AllowUnknown,
        /// Set if the order of values is important when serializing or deserializing.
        /// - `Strict`: The order of values must match the order in the struct or enum variant.
        /// - `None` (*default*): The order of values does not matter, but the values must be present.
        ///
        /// *Deserialize only*
        #[darling(default)]
        pub order: ElementOrder,
        /// The path to the module that provides the serialization and deserialization functions.
        ///
        /// `::serialize` and `::deserialize` will be appended to this path and used as the `serialize_with` and `deserialize_with` functions.
        #[darling(default)]
        pub with: Option<Path>,
        /// Use function to serialize the value.
        ///
        /// Should have signature like `pub fn serialize<S: xmlity::Serializer>(value: &T, serializer: S) -> Result<S::Ok, S::Error>`
        #[darling(default)]
        pub serialize_with: Option<Expr>,
        /// Use function to deserialize the value.
        ///
        /// Should have signature like `fn deserialize<'de, D: xmlity::Deserializer<'de>>(deserializer: D) -> Result<T, D::Error>`
        #[darling(default)]
        pub deserialize_with: Option<Expr>,
    }

    impl RootValueOpts {
        pub fn parse(attrs: &[Attribute]) -> Result<Option<Self>, DeriveError> {
            let Some(attr) = attrs.iter().find(|attr| attr.path().is_ident("xvalue")) else {
                return Ok(None);
            };

            let opts = Self::from_attributes(std::slice::from_ref(attr))?;
            Ok(Some(opts))
        }

        pub fn serialize_with(&self) -> Option<Expr> {
            self.serialize_with
                .as_ref()
                .map(|serialize_with| {
                    parse_quote! {
                        #serialize_with
                    }
                })
                .or_else(|| {
                    self.with.as_ref().map(|with| {
                        parse_quote! {
                            #with::serialize
                        }
                    })
                })
        }

        pub fn deserialize_with(&self) -> Option<Expr> {
            self.deserialize_with
                .as_ref()
                .map(|deserialize_with| {
                    parse_quote! {
                        #deserialize_with
                    }
                })
                .or_else(|| {
                    self.with.as_ref().map(|with| {
                        parse_quote! {
                            #with::deserialize
                        }
                    })
                })
        }
    }

    #[derive(FromAttributes, Default)]
    #[darling(attributes(xgroup))]
    pub struct RootGroupOpts {
        #[darling(default)]
        /// Set if the order of attributes is important when serializing or deserializing.
        /// - `Strict`: The attributes must come directly after each other, and this group will try to deserialize them in one go.
        /// - `Loose`: The order of attributes must come relative to each other, but they can be separated by other attributes outside this group.
        /// - `None` (*default*): The order of attributes is not important.
        ///
        /// *Deserialize only*
        pub attribute_order: GroupOrder,
        #[darling(default)]
        /// Set if the order of children is important when serializing or deserializing.
        /// - `Strict`: The children must come directly after each other, and this group will try to deserialize them in one go.
        /// - `Loose`: The order of children must come relative to each other, but they can be separated by other children outside this group.
        /// - `None` (*default*): The order of children is not important.
        ///
        /// *Deserialize only*
        pub children_order: GroupOrder,
    }

    impl RootGroupOpts {
        pub fn parse(attrs: &[Attribute]) -> Result<Option<Self>, DeriveError> {
            let Some(attr) = attrs.iter().find(|attr| attr.path().is_ident("xgroup")) else {
                return Ok(None);
            };

            let opts = Self::from_attributes(std::slice::from_ref(attr))?;
            Ok(Some(opts))
        }
    }

    #[allow(clippy::large_enum_variant)]
    pub enum SerializeRootOpts {
        None,
        Element(RootElementOpts),
        Value(RootValueOpts),
    }

    impl SerializeRootOpts {
        pub fn parse(attrs: &[Attribute]) -> Result<Self, DeriveError> {
            let element_opts = RootElementOpts::parse(attrs)?;
            let value_opts = RootValueOpts::parse(attrs)?;

            match (element_opts, value_opts) {
                    (Some(element_opts), None) => Ok(Self::Element(element_opts)),
                    (None, Some(value_opts)) => Ok(Self::Value(value_opts)),
                    (None, None) => Ok(Self::None),
                    _ => Err(DeriveError::custom("Wrong options. Only one of `xelement`, `xattribute`, or `xvalue` can be used for root elements.")),
                }
        }
    }

    pub enum DeserializeRootOpts {
        None,
        Element(RootElementOpts),
        Attribute(RootAttributeOpts),
        Value(RootValueOpts),
    }

    impl DeserializeRootOpts {
        pub fn parse(attrs: &[Attribute]) -> Result<Self, DeriveError> {
            let element_opts = RootElementOpts::parse(attrs)?;
            let attribute_opts = RootAttributeOpts::parse(attrs)?;
            let value_opts = RootValueOpts::parse(attrs)?;

            match (element_opts, attribute_opts, value_opts) {
                    (Some(element_opts), None, None) => Ok(Self::Element(element_opts)),
                    (None, Some(attribute_opts), None) => Ok(Self::Attribute(attribute_opts)),
                    (None, None, Some(value_opts)) => Ok(Self::Value(value_opts)),
                    (None, None, None) => Ok(Self::None),
                    _ => Err(DeriveError::custom("Wrong options. Only one of `xelement`, `xattribute`, or `xvalue` can be used for root elements.")),
                }
        }
    }
}

pub mod fields {
    use quote::ToTokens;
    use syn::{parse_quote, Path};

    use crate::common::Prefix;

    use super::*;

    #[derive(FromAttributes, Clone)]
    #[darling(attributes(xelement))]
    pub struct ElementOpts {
        /// Default value for the field if the element is not present.
        #[darling(default)]
        pub default: bool,
        /// Use function to provide a default value for the field.
        ///
        /// Should have signature like `pub fn default_value() -> T`.
        #[darling(default)]
        pub default_with: Option<Path>,
        #[darling(default)]
        pub extendable: Extendable,
        #[darling(default)]
        pub name: Option<LocalName<'static>>,
        #[darling(default)]
        pub namespace: Option<XmlNamespace<'static>>,
        #[darling(default)]
        pub namespace_expr: Option<Expr>,
        #[darling(default)]
        pub preferred_prefix: Option<Prefix<'static>>,
        #[darling(default)]
        pub enforce_prefix: bool,
        #[darling(default)]
        pub optional: bool,
        #[darling(default)]
        pub group: bool,
        #[darling(default)]
        pub skip_serializing_if: Option<Path>,
    }

    impl ElementOpts {
        pub fn default_or_else(&self) -> Option<Expr> {
            if let Some(default_with) = self.default_with.as_ref() {
                Some(parse_quote! {
                    #default_with
                })
            } else if self.default || self.optional {
                Some(parse_quote! {
                    ::core::default::Default::default
                })
            } else {
                None
            }
        }

        pub fn skip_serializing_if<T: ToTokens>(&self, access: T) -> Option<Expr> {
            self.skip_serializing_if
                .as_ref()
                .map(|skip_serializing_if| {
                    parse_quote! {
                        #skip_serializing_if(#access)
                    }
                })
                .or(self.optional.then(|| {
                    parse_quote! {
                        ::core::option::Option::is_none(#access)
                    }
                }))
        }
    }

    impl WithExpandedName for ElementOpts {
        fn name(&self) -> Option<LocalName<'_>> {
            self.name.clone()
        }

        fn namespace(&self) -> Option<XmlNamespace<'_>> {
            self.namespace.clone()
        }

        fn namespace_expr(&self) -> Option<Expr> {
            self.namespace_expr.clone()
        }
    }

    #[derive(FromAttributes, Clone, Default)]
    #[darling(attributes(xvalue))]
    pub struct ValueOpts {
        /// Default value for the field if the element is not present.
        #[darling(default)]
        pub default: bool,
        /// Use function to provide a default value for the field.
        ///
        /// Should have signature like `pub fn default_value() -> T`.
        #[darling(default)]
        pub default_with: Option<Path>,
        #[darling(default)]
        pub extendable: Extendable,
        #[darling(default)]
        pub skip_serializing_if: Option<Path>,
    }

    impl ValueOpts {
        pub fn default_or_else(&self) -> Option<Expr> {
            if let Some(default_with) = &self.default_with {
                Some(parse_quote! {
                    #default_with
                })
            } else if self.default {
                Some(parse_quote! {
                    ::core::default::Default::default
                })
            } else {
                None
            }
        }

        pub fn skip_serializing_if<T: ToTokens>(&self, access: T) -> Option<Expr> {
            self.skip_serializing_if
                .as_ref()
                .map(|skip_serializing_if| {
                    parse_quote! {
                        #skip_serializing_if(#access)
                    }
                })
        }
    }

    #[allow(clippy::large_enum_variant)]
    #[derive(Clone)]
    pub enum ChildOpts {
        Value(ValueOpts),
        Element(ElementOpts),
    }

    impl Default for ChildOpts {
        fn default() -> Self {
            Self::Value(ValueOpts::default())
        }
    }

    impl ChildOpts {
        pub fn default_or_else(&self) -> Option<Expr> {
            let (default, default_with) = match self {
                ChildOpts::Value(ValueOpts {
                    default,
                    default_with,
                    ..
                }) => (*default, default_with),
                ChildOpts::Element(ElementOpts {
                    default,
                    default_with,
                    optional,
                    ..
                }) => (*default || *optional, default_with),
            };

            if let Some(default_with) = default_with {
                Some(parse_quote! {
                    #default_with
                })
            } else if default {
                Some(parse_quote! {
                    ::core::default::Default::default
                })
            } else {
                None
            }
        }

        pub fn from_field(field: &syn::Field) -> Result<Option<Self>, DeriveError> {
            let xvalue_attribute = field
                .attrs
                .iter()
                .find(|attr| attr.path().is_ident("xvalue"))
                .cloned();
            let xelement_attribute = field
                .attrs
                .iter()
                .find(|attr| attr.path().is_ident("xelement"))
                .cloned();

            match (xvalue_attribute, xelement_attribute) {
                (None, None) => Ok(None),
                (Some(_), Some(_)) => Err(DeriveError::custom(
                    "Cannot have both `xvalue` and `xelement` attributes on the same field.",
                )),
                (Some(xvalue_attribute), None) => Self::from_xvalue_attribute(xvalue_attribute),
                (None, Some(xelement_attribute)) => {
                    Self::from_xelement_attribute(xelement_attribute)
                }
            }
        }

        pub fn from_xvalue_attribute(
            xvalue_attribute: syn::Attribute,
        ) -> Result<Option<Self>, DeriveError> {
            let opts = ValueOpts::from_attributes(&[xvalue_attribute])?;
            Ok(Some(ChildOpts::Value(opts)))
        }

        pub fn from_xelement_attribute(
            xelement_attribute: syn::Attribute,
        ) -> Result<Option<Self>, DeriveError> {
            let opts = ElementOpts::from_attributes(&[xelement_attribute])?;
            Ok(Some(ChildOpts::Element(opts)))
        }
    }

    #[derive(Clone)]
    pub struct AttributeDeferredOpts {
        /// Default value for the field if the element is not present.
        pub default: bool,
        /// Use function to provide a default value for the field.
        ///
        /// Should have signature like `pub fn default_value() -> T`.
        pub default_with: Option<Path>,
        /// Use function to skip serializing the field if it is not set.
        ///
        /// Should have signature like `pub fn skip_serializing_if(value: &T) -> bool`.
        pub skip_serializing_if: Option<Path>,
        /// If the field is an [`Option<T>`], it will not be serialized if it is not set.
        pub optional: bool,
    }

    #[derive(Clone)]
    pub struct AttributeDeclaredOpts {
        /// Default value for the field if the element is not present.
        pub default: bool,
        /// Use function to provide a default value for the field.
        ///
        /// Should have signature like `pub fn default_value() -> T`.
        pub default_with: Option<Path>,
        /// The name to serialize to and deserialize from.
        ///
        /// If not specified, the name of the struct is used.
        pub name: Option<LocalName<'static>>,
        /// The namespace of the attribute, defined as a string.
        ///
        /// This is exclusive with [`namespace_expr`].
        ///
        /// If none of these are specified, the absence of a namespace is assumed.
        pub namespace: Option<XmlNamespace<'static>>,
        /// The namespace of the attribute given as an expression to an [`xmlity::XmlNamespace`] value.
        ///
        /// This is exclusive with [`namespace`].
        ///
        /// If none of these are specified, the absence of a namespace is assumed.
        pub namespace_expr: Option<Expr>,
        /// The preferred prefix for the attribute, defined as a string.
        ///
        /// This is exclusive with [`enforce_prefix`].
        ///
        /// If none of these are specified, the absence of a prefix is assumed.
        pub preferred_prefix: Option<Prefix<'static>>,
        /// Always set the prefix of the attribute to the prefix set in `preferred_prefix`.
        ///
        /// *Serialize only*
        pub enforce_prefix: bool,
        /// Use function to skip serializing the field if it is not set.
        ///
        /// Should have signature like `pub fn skip_serializing_if(value: &T) -> bool`.
        pub skip_serializing_if: Option<Path>,
        /// If the field is an [`Option<T>`], it will not be serialized if it is not set.
        pub optional: bool,
    }

    impl WithExpandedName for AttributeDeclaredOpts {
        fn name(&self) -> Option<LocalName<'_>> {
            self.name.clone()
        }

        fn namespace(&self) -> Option<XmlNamespace<'_>> {
            self.namespace.clone()
        }

        fn namespace_expr(&self) -> Option<Expr> {
            self.namespace_expr.clone()
        }
    }

    #[allow(clippy::large_enum_variant)]
    #[derive(Clone)]
    pub enum AttributeOpts {
        Deferred(AttributeDeferredOpts),
        Declared(AttributeDeclaredOpts),
    }

    impl AttributeOpts {
        pub fn default_or_else(&self) -> Option<Expr> {
            let (default, default_with, optional) = match self {
                AttributeOpts::Deferred(AttributeDeferredOpts {
                    default,
                    default_with,
                    optional,
                    ..
                }) => (default, default_with, optional),
                AttributeOpts::Declared(AttributeDeclaredOpts {
                    default,
                    default_with,
                    optional,
                    ..
                }) => (default, default_with, optional),
            };

            if let Some(default_with) = default_with {
                Some(parse_quote! {
                    #default_with
                })
            } else if *default || *optional {
                Some(parse_quote! {
                    ::core::default::Default::default
                })
            } else {
                None
            }
        }

        pub fn skip_serializing_if<T: ToTokens>(&self, access: T) -> Option<Expr> {
            let (skip_serializing_if, optional) = match self {
                AttributeOpts::Deferred(AttributeDeferredOpts {
                    skip_serializing_if,
                    optional,
                    ..
                }) => (skip_serializing_if, optional),
                AttributeOpts::Declared(AttributeDeclaredOpts {
                    skip_serializing_if,
                    optional,
                    ..
                }) => (skip_serializing_if, optional),
            };

            skip_serializing_if
                .as_ref()
                .map(|skip_serializing_if| {
                    parse_quote! {
                        #skip_serializing_if(#access)
                    }
                })
                .or(optional.then(|| {
                    parse_quote! {
                        ::core::option::Option::is_none(#access)
                    }
                }))
        }

        pub fn from_field(field: &syn::Field) -> Result<Option<Self>, DeriveError> {
            let Some(attribute) = field
                .attrs
                .iter()
                .find(|attr| attr.path().is_ident("xattribute"))
                .cloned()
            else {
                return Ok(None);
            };

            #[derive(FromAttributes)]
            #[darling(attributes(xattribute))]
            pub struct FieldAttributeRawOpts {
                #[darling(default)]
                pub default: bool,
                #[darling(default)]
                pub default_with: Option<Path>,
                #[darling(default)]
                pub deferred: bool,
                #[darling(default)]
                pub name: Option<LocalName<'static>>,
                #[darling(default)]
                pub namespace: Option<XmlNamespace<'static>>,
                #[darling(default)]
                pub namespace_expr: Option<Expr>,
                #[darling(default)]
                pub preferred_prefix: Option<Prefix<'static>>,
                #[darling(default)]
                pub enforce_prefix: Option<bool>,
                #[darling(default)]
                pub optional: bool,
                #[darling(default)]
                pub skip_serializing_if: Option<Path>,
            }

            let raw = FieldAttributeRawOpts::from_attributes(&[attribute])
                .map(Some)
                .map_err(DeriveError::Darling)?;

            let Some(raw) = raw else {
                return Ok(None);
            };

            if raw.deferred {
                let unallowed_fields = [
                    (raw.name.is_some(), "name"),
                    (raw.namespace.is_some(), "namespace"),
                    (raw.namespace_expr.is_some(), "namespace_expr"),
                    (raw.preferred_prefix.is_some(), "preferred_prefix"),
                    (raw.enforce_prefix.is_some(), "enforce_prefix"),
                ];
                if let Some((true, field)) =
                    unallowed_fields.iter().find(|(unallowed, _)| *unallowed)
                {
                    return Err(DeriveError::custom(format!(
                        "{field} can not be set if deferred is set"
                    )));
                }

                Ok(Some(Self::Deferred(AttributeDeferredOpts {
                    default: raw.default,
                    default_with: raw.default_with,
                    skip_serializing_if: raw.skip_serializing_if,
                    optional: raw.optional,
                })))
            } else {
                Ok(Some(Self::Declared(AttributeDeclaredOpts {
                    default: raw.default,
                    default_with: raw.default_with,
                    name: raw.name,
                    namespace: raw.namespace,
                    namespace_expr: raw.namespace_expr,
                    preferred_prefix: raw.preferred_prefix,
                    enforce_prefix: raw.enforce_prefix.unwrap_or(false),
                    skip_serializing_if: raw.skip_serializing_if,
                    optional: raw.optional,
                })))
            }
        }
    }

    #[derive(FromAttributes, Clone)]
    #[darling(attributes(xgroup))]
    pub struct GroupOpts {}

    impl GroupOpts {
        pub fn from_field(field: &syn::Field) -> Result<Option<Self>, DeriveError> {
            let Some(attribute) = field
                .attrs
                .iter()
                .find(|attr| attr.path().is_ident("xgroup"))
                .cloned()
            else {
                return Ok(None);
            };
            Self::from_attributes(&[attribute])
                .map(Some)
                .map_err(DeriveError::Darling)
        }
    }

    #[derive(Clone)]
    pub enum FieldOpts {
        Value(ChildOpts),
        Attribute(AttributeOpts),
        Group(GroupOpts),
    }

    impl FieldOpts {
        pub fn value_group(self) -> Option<FieldValueGroupOpts> {
            match self {
                FieldOpts::Value(child_opts) => Some(FieldValueGroupOpts::Value(child_opts)),
                FieldOpts::Attribute(_) => None,
                FieldOpts::Group(group_opts) => Some(FieldValueGroupOpts::Group(group_opts)),
            }
        }

        pub fn attribute(self) -> Option<AttributeOpts> {
            match self {
                FieldOpts::Value(_) => None,
                FieldOpts::Attribute(attribute_opts) => Some(attribute_opts),
                FieldOpts::Group(_) => None,
            }
        }

        pub fn attribute_group(self) -> Option<FieldAttributeGroupOpts> {
            match self {
                FieldOpts::Value(_) => None,
                FieldOpts::Attribute(attribute_opts) => {
                    Some(FieldAttributeGroupOpts::Attribute(attribute_opts))
                }
                FieldOpts::Group(group_opts) => Some(FieldAttributeGroupOpts::Group(group_opts)),
            }
        }
    }

    #[allow(clippy::large_enum_variant)]
    #[derive(Clone)]
    pub enum FieldAttributeGroupOpts {
        Attribute(AttributeOpts),
        Group(GroupOpts),
    }

    #[allow(clippy::large_enum_variant)]
    #[derive(Clone)]
    pub enum FieldValueGroupOpts {
        Value(ChildOpts),
        Group(GroupOpts),
    }

    impl FieldOpts {
        pub fn from_field(field: &syn::Field) -> Result<Self, DeriveError> {
            let element = ChildOpts::from_field(field)?;
            let attribute = AttributeOpts::from_field(field)?;
            let group = GroupOpts::from_field(field)?;
            Ok(match (element, attribute, group) {
                (Some(element), None, None) => Self::Value(element),
                (None, Some(attribute), None) => Self::Attribute(attribute),
                (None, None, Some(group)) => Self::Group(group),
                (None, None, None) => Self::Value(ChildOpts::default()),
                _ => {
                    return Err(DeriveError::custom(
                        "Cannot have multiple xmlity field attributes on the same field.",
                    ))
                }
            })
        }
    }
}