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
use bitflags::bitflags;

use super::IdentQuadruple;

/// Configuration for the code generator.
#[derive(Debug, Clone)]
pub struct GeneratorConfig {
    /// Additional flags to control the generator.
    pub flags: GeneratorFlags,

    /// Types to add to the generator before the actual data types are generated.
    ///
    /// See [`with_type`](crate::Generator::with_type) for more details.
    pub types: Vec<IdentQuadruple>,

    /// Specify which meta types the generator should generate data types for.
    pub generate: Generate,

    /// Postfixes that should be applied to the name of the different generated
    /// types.
    ///
    /// See [`with_type_postfix`](crate::Generator::with_type_postfix) for more details.
    pub type_postfix: TypePostfix,

    /// Tell the generator how to deal with boxing.
    pub box_flags: BoxFlags,

    /// Tells the generator how to deal with type definitions.
    pub typedef_mode: TypedefMode,

    /// Type to use to store unformatted text.
    ///
    /// See [`Generator::text_type`](crate::Generator::text_type) for details.
    pub text_type: String,

    /// Type to use to store mixed types.
    ///
    /// See [`Generator::mixed_type`](crate::Generator::mixed_type) for details.
    pub mixed_type: String,

    /// Type to use to store nillable types.
    ///
    /// See [`Generator::nillable_type`](crate::Generator::nillable_type) for details.
    pub nillable_type: String,

    /// Type to use to store unstructured `xs:any` elements.
    ///
    /// See [`Generator::any_type`](crate::Generator::any_type) for details.
    pub any_type: String,

    /// Type to use to store unstructured `xs:anyAttribute` attributes.
    ///
    /// See [`Generator::any_attributes_type`](crate::Generator::any_attributes_type)
    /// for details.
    pub any_attributes_type: String,
}

impl Default for GeneratorConfig {
    fn default() -> Self {
        Self {
            types: vec![],
            type_postfix: TypePostfix::default(),
            box_flags: BoxFlags::AUTO,
            typedef_mode: TypedefMode::Auto,
            generate: Generate::Named,
            flags: GeneratorFlags::empty(),
            text_type: "::xsd_parser_types::xml::Text".into(),
            mixed_type: "::xsd_parser_types::xml::Mixed".into(),
            nillable_type: "::xsd_parser_types::xml::Nillable".into(),
            any_type: "::xsd_parser_types::xml::AnyElement".into(),
            any_attributes_type: "::xsd_parser_types::xml::AnyAttributes".into(),
        }
    }
}

bitflags! {
    /// Different flags that control what code the [`Generator`](crate::Generator)
    /// is generating.
    #[derive(Debug, Clone, Copy)]
    pub struct GeneratorFlags: u32 {
        /// None of the features are enabled.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/generator_flags/schema.xsd")]
        /// ```
        ///
        /// Setting none of the flags will result in the following code:
        /// ```rust
        #[doc = include_str!("../../tests/generator/generator_flags/expected/empty.rs")]
        /// ```
        const NONE = 0;

        /// The generated code uses modules for the different namespaces.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/generator_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `USE_MODULES` feature only will result in the following code:
        /// ```rust,ignore
        #[doc = include_str!("../../tests/generator/generator_flags/expected/use_modules.rs")]
        /// ```
        const USE_MODULES = Self::USE_NAMESPACE_MODULES.bits();

        /// The generated code uses modules for the different namespaces.
        ///
        /// See [`USE_MODULES`](Self::USE_MODULES) for details.
        const USE_NAMESPACE_MODULES = 1 << 0;

        /// The generated code uses modules for the different schemas.
        ///
        /// See [`USE_MODULES`](Self::USE_MODULES) for details.
        const USE_SCHEMA_MODULES = 1 << 1;

        /// The generator flattens the content type of choice types if it does not
        /// define any element attributes.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/generator_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `FLATTEN_CONTENT` feature only will result in the following code:
        /// ```rust
        #[doc = include_str!("../../tests/generator/generator_flags/expected/flatten_content.rs")]
        /// ```
        const FLATTEN_CONTENT = Self::FLATTEN_ENUM_CONTENT.bits()
            | Self::FLATTEN_STRUCT_CONTENT.bits();

        /// The generator flattens the content of enum types if possible.
        ///
        /// See [`FLATTEN_CONTENT`](Self::FLATTEN_CONTENT) for details.
        const FLATTEN_ENUM_CONTENT = 1 << 2;

        /// The generator flattens the content of struct types if possible.
        ///
        /// See [`FLATTEN_CONTENT`](Self::FLATTEN_CONTENT) for details.
        const FLATTEN_STRUCT_CONTENT = 1 << 3;

        /// Enable support for mixed types.
        ///
        /// This will enable code generation for mixed types. This feature needs
        /// to be used with caution, because support for mixed types when using
        /// `serde` is quite limited.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/generator_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `MIXED_TYPE_SUPPORT` feature only will result in the following code:
        /// ```rust,ignore
        #[doc = include_str!("../../tests/generator/generator_flags/expected/mixed_type_support.rs")]
        /// ```
        const MIXED_TYPE_SUPPORT = 1 << 4;

        /// Enable support for nillable types.
        ///
        /// This will enable code generation for nillable types. This feature needs
        /// to be used with caution, because support for nillable types when using
        /// `serde` is quite limited.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/generator_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `NILLABLE_TYPE_SUPPORT` feature only will result in the following code:
        /// ```rust,ignore
        #[doc = include_str!("../../tests/generator/generator_flags/expected/nillable_type_support.rs")]
        /// ```
        const NILLABLE_TYPE_SUPPORT = 1 << 5;

        /// Enable support for `xs:any` types.
        ///
        /// This will enable code generation for `xs:any` types. This feature needs
        /// to be used with caution, because support for any types when using
        /// `serde` is quite limited.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/generator_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `ANY_TYPE_SUPPORT` feature only will result in the following code:
        /// ```rust,ignore
        #[doc = include_str!("../../tests/generator/generator_flags/expected/any_type_support.rs")]
        /// ```
        const ANY_TYPE_SUPPORT = 1 << 6;

        /// Use absolute paths for build-in types and traits.
        ///
        /// Using this flag will instruct the generator to use absolute paths
        /// for build-in types and traits (e.g. `usize`, `String` or `From`) to
        /// avoid naming conflicts with generated types.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/generator_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `BUILD_IN_ABSOLUTE_PATHS` feature only will result in the following code:
        /// ```rust,ignore
        #[doc = include_str!("../../tests/generator/generator_flags/expected/build_in_absolute_paths.rs")]
        /// ```
        const BUILD_IN_ABSOLUTE_PATHS = 1 << 7;

        /// Use absolute paths instead of using directives for all non build-in
        /// types and traits.
        ///
        /// Using this flag will instruct the generator to use absolute paths
        /// for non build-in and generated types and traits to avoid naming
        /// conflicts with other generated types.
        ///
        /// This does not include build-in types (like `usize`, `String` or `From`),
        /// to use absolute paths for these also, you have to add the
        /// [`BUILD_IN_ABSOLUTE_PATHS`](Self::BUILD_IN_ABSOLUTE_PATHS) as well.
        const ABSOLUTE_PATHS_INSTEAD_USINGS = 1 << 8;

        /// Enable support for advanced enums.
        ///
        /// This will enable support for advanced enums, which are enums that have
        /// additional constants or associated functions for the expected values of
        /// the enum. The values are then used to match the expected values of the
        /// enum when deserializing instead of doing a simple match on the byte string
        /// literal.
        ///
        /// Caution! If you enable this flag, you have to provide suitable constants
        /// or associated functions for the expected values of the enum, otherwise
        /// the generated code will not compile. You can use the
        /// [`EnumConstantsRenderStep`](crate::pipeline::renderer::EnumConstantsRenderStep)
        /// or the [`RenderStep::EnumConstants`](super::RenderStep::EnumConstants)
        /// to generate suitable code for these constants and associated functions.
        const ADVANCED_ENUMS = 1 << 9;
    }
}

bitflags! {
    /// Flags to tell the [`Generator`](crate::Generator) how to deal with boxed
    /// types.
    #[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
    pub struct BoxFlags: u32 {
        /// Boxed types will only be used if necessary.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/box_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `AUTO` feature only will result in the following code:
        /// ```rust
        #[doc = include_str!("../../tests/generator/box_flags/expected/auto.rs")]
        /// ```
        const AUTO = 0;

        /// Elements in a `xs:choice` type will always be boxed.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/box_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `ENUM_ELEMENTS` feature only will result in the following code:
        /// ```rust
        #[doc = include_str!("../../tests/generator/box_flags/expected/enum_elements.rs")]
        /// ```
        const ENUM_ELEMENTS = 1 << 0;

        /// Elements in a `xs:all` or `xs:sequence` type will always be boxed.
        ///
        /// # Examples
        ///
        /// Consider the following XML schema:
        /// ```xml
        #[doc = include_str!("../../tests/generator/box_flags/schema.xsd")]
        /// ```
        ///
        /// Enable the `STRUCT_ELEMENTS` feature only will result in the following code:
        /// ```rust
        #[doc = include_str!("../../tests/generator/box_flags/expected/struct_elements.rs")]
        /// ```
        const STRUCT_ELEMENTS = 1 << 1;
    }
}

/// Postfixed that will be added to the different types generated by the code generator.
#[derive(Debug, Clone)]
pub struct TypePostfix {
    /// Postfix added to normal types (like `xs:simpleType` or `xs:complexType`).
    pub type_: String,

    /// Postfixes added to elements (like `xs:element`).
    pub element: String,

    /// Postfixes added to inline types of elements (like `xs:element`).
    pub element_type: String,

    /// Postfix for the type that is used as content for [`Nillable`](xsd_parser_types::xml::Nillable) elements.
    pub nillable_content: String,

    /// Postfix for concrete elements in a substitution group.
    pub dynamic_element: String,
}

impl Default for TypePostfix {
    fn default() -> Self {
        Self {
            type_: String::from("Type"),
            element: String::new(),
            element_type: String::from("ElementType"),
            nillable_content: String::from("NotNil"),
            dynamic_element: String::from("Dyn"),
        }
    }
}

/// Tells the [`Generator`](crate::Generator) how to deal with type definitions.
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
pub enum TypedefMode {
    /// The [`Generator`](crate::Generator) will automatically detect if a
    /// new type struct or a simple type definition should be used
    /// for a [`Reference`](crate::models::meta::MetaTypeVariant::Reference) type.
    ///
    /// Detecting the correct type automatically depends basically on the
    /// occurrence of the references type. If the target type is only referenced
    /// exactly once, a type definition is rendered. If a different
    /// occurrence is used, it is wrapped in a new type struct because usually
    /// additional code needs to be implemented for such types.
    ///
    /// # Examples
    ///
    /// Consider the following XML schema:
    /// ```xml
    #[doc = include_str!("../../tests/generator/typedef_mode/schema.xsd")]
    /// ```
    ///
    /// If the typedef mode is set to [`TypedefMode::Auto`] the following code is rendered:
    /// ```rust
    #[doc = include_str!("../../tests/generator/typedef_mode/expected/auto.rs")]
    /// ```
    #[default]
    Auto,

    /// The [`Generator`](crate::Generator) will always use a simple type definition
    /// for a [`Reference`](crate::models::meta::MetaTypeVariant::Reference) type.
    ///
    /// # Examples
    ///
    /// Consider the following XML schema:
    /// ```xml
    #[doc = include_str!("../../tests/generator/typedef_mode/schema.xsd")]
    /// ```
    ///
    /// If the typedef mode is set to [`TypedefMode::Typedef`] the following code is rendered:
    /// ```rust
    #[doc = include_str!("../../tests/generator/typedef_mode/expected/typedef.rs")]
    /// ```
    Typedef,

    /// The [`Generator`](crate::Generator) will always use a new type struct
    /// for a [`Reference`](crate::models::meta::MetaTypeVariant::Reference) type.
    ///
    /// # Examples
    ///
    /// Consider the following XML schema:
    /// ```xml
    #[doc = include_str!("../../tests/generator/typedef_mode/schema.xsd")]
    /// ```
    ///
    /// If the typedef mode is set to [`TypedefMode::NewType`] the following code is rendered:
    /// ```rust
    #[doc = include_str!("../../tests/generator/typedef_mode/expected/new_type.rs")]
    /// ```
    NewType,
}

/// Configuration which types the [`Generator`](crate::Generator) should generate
/// code for used in [`GeneratorConfig`].
#[derive(Debug, Clone)]
pub enum Generate {
    /// The generator will generate code for all types of the schemas.
    All,

    /// The generator will generate code for all types that have a name.
    Named,

    /// List of identifiers the generator will generate code for.
    Types(Vec<IdentQuadruple>),
}