xsd-parser 1.5.2

Rust code generator for XML schema files
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
use std::borrow::Cow;
use std::ops::Deref;

use bitflags::bitflags;
use proc_macro2::{Ident as Ident2, Literal, TokenStream};

use crate::{
    models::{
        data::TagName,
        meta::{AttributeMeta, ElementMeta},
        schema::{MaxOccurs, MinOccurs},
        TypeIdent,
    },
    pipeline::renderer::ValueRendererBox,
};

use super::{Occurs, PathData};

/// Contains additional information for the rendering process of a
/// [`MetaTypeVariant::All`](crate::models::meta::MetaTypeVariant::All),
/// [`MetaTypeVariant::Choice`](crate::models::meta::MetaTypeVariant::Choice),
/// [`MetaTypeVariant::Sequence`](crate::models::meta::MetaTypeVariant::Sequence)
/// or [`MetaTypeVariant::ComplexType`](crate::models::meta::MetaTypeVariant::ComplexType)
/// type.
///
/// To simplify the rendering process this recursive type was added to the
/// generator. It basically boils down to the following:
///
///   - A complex type with a `choice` will result in a struct with a enum
///     content type.
///   - A complex type with a `all` or `sequence` will result in a struct
///     with a struct content type.
///   - A simple `choice` will result in a single enum type.
///   - A simple `all` or `sequence` will result in a single sequence.
///
/// Additional improvements may be applied to the type, to reduce the complexity
/// of the generated type (for example flattening the content if possible).
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum ComplexData<'types> {
    /// The type represents an enumeration.
    ///
    /// This is normally used for `choice` elements.
    Enum {
        /// The main type.
        type_: ComplexDataEnum<'types>,

        /// The content of the main type (if needed).
        content_type: Option<Box<ComplexData<'types>>>,
    },

    /// The type represents a struct.
    ///
    /// This is normally used for `all`
    Struct {
        /// The main type.
        type_: ComplexDataStruct<'types>,

        /// The content of the main type (if needed).
        content_type: Option<Box<ComplexData<'types>>>,
    },
}

/// Contains basic information for that is shared between [`ComplexDataEnum`]
/// and [`ComplexDataStruct`].
#[derive(Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct ComplexBase<'types> {
    /// Flags set for this type.
    pub flags: ComplexFlags,

    /// The identifier of the rendered type.
    pub type_ident: Ident2,

    /// List of traits that needs to be implemented by this type.
    pub trait_impls: Vec<TokenStream>,

    /// Name of the XML tag of the type (if the type represents an element in the XML).
    pub tag_name: Option<TagName<'types>>,

    /// Identifier of the serializer for this type.
    pub serializer_ident: Ident2,

    /// Identifier of the state of the serializer for this type.
    pub serializer_state_ident: Ident2,

    /// Identifier of the deserializer for this type.
    pub deserializer_ident: Ident2,

    /// Identifier of the state of the deserializer for this type.
    pub deserializer_state_ident: Ident2,
}

/// Represents a rust enum.
///
/// Is used as part of the [`ComplexData`].
#[derive(Debug)]
pub struct ComplexDataEnum<'types> {
    /// Basic type information.
    pub base: ComplexBase<'types>,

    /// List of `xs:element`s or variants contained in this enum
    pub elements: Vec<ComplexDataElement<'types>>,

    /// Whether any kind of element is allowed in the enum or not
    ///
    /// This is only true if no type for `xs:any` element is defined.
    pub allow_any: bool,

    /// Whether any kind of attribute is allowed in the enum or not
    ///
    /// This is only true if no type for `xs:anyAttribute` element is defined.
    pub allow_any_attribute: bool,
}

/// Represents a rust struct.
///
/// Is used as part of the [`ComplexData`].
#[derive(Debug)]
pub struct ComplexDataStruct<'types> {
    /// Basic type information.
    pub base: ComplexBase<'types>,

    /// Additional information about the content of the struct.
    pub mode: StructMode<'types>,

    /// List of `xs:attribute`s contained in this struct.
    pub attributes: Vec<ComplexDataAttribute<'types>>,

    /// Whether any kind of attribute is allowed in the struct or not
    ///
    /// This is only true if no type for `xs:anyAttribute` element is defined.
    pub allow_any_attribute: bool,
}

/// Content of a rust struct.
///
/// Used by [`ComplexDataStruct`] to tell how the actual content of the struct
/// should be rendered.
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum StructMode<'types> {
    /// The struct does not contain any `xs:element`s.
    Empty {
        /// Whether any kind of element is allowed in the struct or not
        ///
        /// This is only true if no type for `xs:any` element is defined.
        allow_any: bool,
    },

    /// The content of the struct is another generated type that contains
    /// the actual data.
    Content {
        /// Information about the content of the struct.
        content: ComplexDataContent<'types>,
    },

    /// The content of the struct is a `xs:all` group.
    All {
        /// List of `xs:element`s inside the group.
        elements: Vec<ComplexDataElement<'types>>,

        /// Whether any kind of element is allowed in the struct or not
        ///
        /// This is only true if no type for `xs:any` element is defined.
        allow_any: bool,
    },

    /// The content of the struct is a `xs:sequence` group.
    Sequence {
        /// List of `xs:element`s inside the group.
        elements: Vec<ComplexDataElement<'types>>,

        /// Whether any kind of element is allowed in the struct or not
        ///
        /// This is only true if no type for `xs:any` element is defined.
        allow_any: bool,
    },
}

/// Contains details about the content of a struct.
///
/// Is used by [`StructMode`] to define the content of a struct.
#[derive(Debug)]
pub struct ComplexDataContent<'types> {
    /// Occurrence of the content within this struct.
    pub occurs: Occurs,

    /// Type identifier of the content type if this `ComplexDataContent` was
    /// constructed from a complex type with simple content.
    pub simple_type: Option<&'types TypeIdent>,

    /// Minimum occurrence.
    pub min_occurs: MinOccurs,

    /// Maximum occurrence.
    pub max_occurs: MaxOccurs,

    /// Actual target type of the content.
    pub target_type: PathData,

    /// Additional attributes that will be added to the element.
    pub extra_attributes: Vec<TokenStream>,
}

/// Contains the details of an XML element.
///
/// Is used in [`ComplexDataEnum`] or [`StructMode`].
#[derive(Debug)]
pub struct ComplexDataElement<'types> {
    /// Origin of the element
    pub origin: ComplexDataElementOrigin<'types>,

    /// Occurrence of the element within it's parent type.
    pub occurs: Occurs,

    /// Name of the element as string.
    pub s_name: String,

    /// Name of the element as byte string literal.
    pub b_name: Literal,

    /// Name of the XML tag of the element.
    pub tag_name: TagName<'types>,

    /// Field identifier of the element.
    pub field_ident: Ident2,

    /// Variant identifier of the element.
    pub variant_ident: Ident2,

    /// Actual target type of the element.
    pub target_type: PathData,

    /// `true` if this element needs some indirection
    /// (like a `Box` or a `Vec`), `false` otherwise.
    pub need_indirection: bool,

    /// `true` if the target type of this element is dynamic,
    /// `false` otherwise.
    pub target_is_dynamic: bool,

    /// Additional attributes that will be added to the element.
    pub extra_attributes: Vec<TokenStream>,
}

/// Origin of a [`ComplexDataElement`].
#[derive(Debug)]
pub enum ComplexDataElementOrigin<'types> {
    /// The element was created from a corresponding [`ElementMeta`]
    Meta(&'types ElementMeta),

    /// The element was generated as text element.
    Generated(Box<ElementMeta>),
}

/// Contains the details of an XML attribute.
///
/// Is used in [`ComplexDataStruct`].
#[derive(Debug)]
pub struct ComplexDataAttribute<'types> {
    /// Reference to the original type information.
    pub meta: Cow<'types, AttributeMeta>,

    /// Identifier of the attribute.
    pub ident: Ident2,

    /// Name of the attribute as string.
    pub s_name: String,

    /// Name of the attribute as byte string literal.
    pub b_name: Literal,

    /// Name of the attribute inside the XML tag.
    pub tag_name: TagName<'types>,

    /// `true` if this attribute is optional, `false` otherwise.
    pub is_option: bool,

    /// The actual target type of the attribute.
    pub target_type: PathData,

    /// The default value of the attribute.
    pub default_value: Option<ValueRendererBox>,

    /// Additional attributes that will be added to the attribute.
    pub extra_attributes: Vec<TokenStream>,
}

bitflags! {
    /// Different flags that may be set for a [`ComplexBase`] type.
    #[derive(Debug, Clone, Copy, Eq, PartialEq)]
    pub struct ComplexFlags: u32 {
        /// Whether the type has at least one `xs:any` element or not.
        const HAS_ANY = 1 << 0;

        /// `true` if the type is a mixed type, `false` otherwise.
        const IS_MIXED = 1 << 1;

        /// `true` if the type is a complex type, `false` otherwise.
        const IS_COMPLEX = 1 << 2;

        /// `true/// `true` if the type is dynamic, `false` otherwise.` if the type is dynamic, `false` otherwise.
        const IS_DYNAMIC = 1 << 3;

        /// `true` if the type is the content type of another complex type.
        const IS_CONTENT = 1 << 4;
    }
}

impl<'types> ComplexBase<'types> {
    /// Whether the type has at least one `xs:any` element or not.
    #[must_use]
    pub fn has_any(&self) -> bool {
        self.flags.contains(ComplexFlags::HAS_ANY)
    }

    /// `true` if the type is a mixed type, `false` otherwise.
    #[must_use]
    pub fn is_mixed(&self) -> bool {
        self.flags.contains(ComplexFlags::IS_MIXED)
    }

    /// `true` if the type is a complex type, `false` otherwise.
    #[must_use]
    pub fn is_complex(&self) -> bool {
        self.flags.contains(ComplexFlags::IS_COMPLEX)
    }

    /// `true` if the type is dynamic, `false` otherwise.
    #[must_use]
    pub fn is_dynamic(&self) -> bool {
        self.flags.contains(ComplexFlags::IS_DYNAMIC)
    }

    /// `true` if the type is the content type of another complex type.
    #[must_use]
    pub fn is_content(&self) -> bool {
        self.flags.contains(ComplexFlags::IS_CONTENT)
    }

    /// Returns the name of the element tag, if type is represented by a XML element.
    #[must_use]
    pub fn element_tag(&self) -> Option<&TagName<'types>> {
        (self.is_complex() && !self.is_dynamic())
            .then_some(self.tag_name.as_ref())
            .flatten()
    }

    /// Returns `true` if this type represents an element, `false` otherwise.
    #[must_use]
    pub fn represents_element(&self) -> bool {
        self.is_complex() && self.tag_name.is_some() && !self.is_dynamic()
    }
}

impl<'types> Deref for ComplexDataEnum<'types> {
    type Target = ComplexBase<'types>;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl<'types> ComplexDataStruct<'types> {
    /// Returns `true` if this struct is a unit struct, `false` otherwise.
    #[must_use]
    pub fn is_unit_struct(&self) -> bool {
        matches!(&self.mode, StructMode::Empty { .. }) && !self.has_attributes()
    }

    /// Returns `true` if this struct has attributes, `false` otherwise.
    #[must_use]
    pub fn has_attributes(&self) -> bool {
        !self.attributes.is_empty()
    }

    /// Returns `true` if this struct has a content field, `false` otherwise.
    #[must_use]
    pub fn has_content(&self) -> bool {
        match &self.mode {
            StructMode::All { elements, .. } | StructMode::Sequence { elements, .. } => {
                !elements.is_empty()
            }
            StructMode::Content { .. } => true,
            StructMode::Empty { .. } => false,
        }
    }

    /// Returns the elements (fields) of this struct.
    #[must_use]
    pub fn elements(&self) -> &[ComplexDataElement<'_>] {
        if let StructMode::All { elements, .. } | StructMode::Sequence { elements, .. } = &self.mode
        {
            elements
        } else {
            &[]
        }
    }

    /// Returns `true` if any kind of element is allowed in the struct, `false` otherwise.
    #[must_use]
    pub fn allow_any(&self) -> bool {
        if let StructMode::Empty { allow_any }
        | StructMode::All { allow_any, .. }
        | StructMode::Sequence { allow_any, .. } = &self.mode
        {
            *allow_any
        } else {
            false
        }
    }

    /// Returns the content type if this struct has one.
    #[must_use]
    pub fn content(&self) -> Option<&ComplexDataContent<'types>> {
        if let StructMode::Content { content, .. } = &self.mode {
            Some(content)
        } else {
            None
        }
    }
}

impl<'types> Deref for ComplexDataStruct<'types> {
    type Target = ComplexBase<'types>;

    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl ComplexDataContent<'_> {
    /// Returns `true` if the content is a simple type (e.g. a enum, union,
    /// string, integer, ...), `false` otherwise.
    #[must_use]
    pub fn is_simple(&self) -> bool {
        self.simple_type.is_some()
    }

    /// Returns `true` if the content is a complex type (e.g. a sequence, choice,
    /// all, ...), `false` otherwise.
    #[must_use]
    pub fn is_complex(&self) -> bool {
        self.simple_type.is_none()
    }
}

impl ComplexDataElement<'_> {
    /// Returns the [`ElementMeta`] this element was created for.
    #[inline]
    #[must_use]
    pub fn meta(&self) -> &ElementMeta {
        match &self.origin {
            ComplexDataElementOrigin::Generated(meta) => meta,
            ComplexDataElementOrigin::Meta(meta) => meta,
        }
    }
}

impl ComplexFlags {
    /// Adds the `other` flags to the current one if `value` is true and return
    /// the result.
    #[must_use]
    pub fn with(mut self, other: ComplexFlags, value: bool) -> Self {
        self.set(other, value);

        self
    }

    /// Extracts the `other` flags from the current ones and return them.
    #[must_use]
    pub fn extract(&mut self, other: ComplexFlags) -> Self {
        let ret = self.intersection(other);
        self.remove(other);
        ret
    }
}