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
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
//! Code generation pipeline for transforming resolved schema models into Rust data structures.
//!
//! This module defines the [`Generator`] and [`GeneratorFixed`] types, along with supporting
//! logic and configuration mechanisms for converting fully interpreted [`MetaTypes`] into
//! concrete Rust representations [`DataTypes`].
//!
//! The [`Generator`] allows fine-grained configuration such as boxing strategy, type naming,
//! serde support, and handling of `xs:any`/`xs:anyAttribute`. Once configured, the generator
//! can be "fixed" into a [`GeneratorFixed`] state to emit type definitions in a controlled,
//! deterministic fashion.
//!
//! Code generation is performed by walking the dependency graph of types, resolving references,
//! and emitting type-safe Rust structures including enums, structs, and aliases.
//!
//! # Example
//! ```rust,ignore
//! let data_types = Generator::new(meta_types)
//!     .with_flags(GeneratorFlags::USE_MODULES)
//!     .generate_named_types()?
//!     .finish();
//! ```

mod context;
mod custom;
mod data;
mod error;
mod meta;
mod state;

use std::collections::btree_map::{Entry, VacantEntry};
use std::collections::{BTreeMap, VecDeque};
use std::str::FromStr;

use quote::format_ident;
use tracing::instrument;

use crate::config::{BoxFlags, GeneratorFlags, TypedefMode};
use crate::models::{
    code::{IdentPath, ModuleIdent, ModulePath},
    data::{DataType, DataTypes, PathData},
    meta::{MetaTypeVariant, MetaTypes},
    IdentType, TypeIdent,
};
use crate::traits::Naming;

pub use self::context::Context;
pub use self::custom::{ValueGenerator, ValueGeneratorBox, ValueGeneratorMode};
pub use self::error::Error;
pub use self::meta::MetaData;

use self::state::{LoopDetection, PendingType, State, TraitInfos, TypeRef};

/// Configurable Rust code generator for schema-derived type information.
///
/// The [`Generator`] type provides a flexible interface to customize how Rust code
/// structures are generated from XSD-like schema models represented as [`MetaTypes`].
/// It supports configuration of type postfixes, boxing rules, serde integration, and more.
///
/// Once all configuration is applied, the generator can be "sealed" into a
/// [`GeneratorFixed`] instance using [`into_fixed`](Self::into_fixed),
/// after which only code generation (not configuration) is permitted.
#[must_use]
#[derive(Debug)]
pub struct Generator<'types> {
    meta: MetaData<'types>,
    state: State<'types>,
}

/// Finalized code generator that emits Rust types from resolved schema definitions.
///
/// [`GeneratorFixed`] is produced by sealing a [`Generator`] with [`Generator::into_fixed()`],
/// locking its configuration and enabling deterministic generation of all required types.
///
/// It offers methods to:
/// - generate a specific type
/// - generate all named types
/// - generate all available types
///
/// Once generation is complete, use [`finish`](Self::finish) to retrieve the generated
/// [`DataTypes`] output for rendering.
#[must_use]
#[derive(Debug)]
pub struct GeneratorFixed<'types> {
    state: State<'types>,
    data_types: DataTypes<'types>,
}

/* Generator */

impl<'types> Generator<'types> {
    /// Create a new code generator from the passed `types`.
    pub fn new(types: &'types MetaTypes) -> Self {
        let meta = MetaData {
            types,
            flags: GeneratorFlags::empty(),
            postfixes: [
                String::from("Type"),        // Type = 0
                String::new(),               // Group = 1
                String::from("ElementType"), // Element = 2
                String::new(),               // ElementType = 3
                String::new(),               // Attribute = 4
                String::new(),               // AttributeGroup = 5
                String::new(),               // BuildIn = 6
                String::new(),               // Enumeration = 7
                String::from("NotNil"),      // NillableContent = 8
                String::from("Dyn"),         // DynamicElement = 9
            ],
            box_flags: BoxFlags::AUTO,
            typedef_mode: TypedefMode::Auto,
            text_type: IdentPath::from_str("::xsd_parser_types::xml::Text").unwrap(),
            mixed_type: IdentPath::from_str("::xsd_parser_types::xml::Mixed").unwrap(),
            nillable_type: IdentPath::from_str("::xsd_parser_types::xml::Nillable").unwrap(),
            any_type: IdentPath::from_str("::xsd_parser_types::xml::AnyElement").unwrap(),
            any_attributes_type: IdentPath::from_str("::xsd_parser_types::xml::AnyAttributes")
                .unwrap(),
        };
        let state = State {
            cache: BTreeMap::new(),
            pending: VecDeque::new(),
            trait_infos: None,
            loop_detection: LoopDetection::default(),
        };

        Self { meta, state }
    }

    /// Set the [`BoxFlags`] flags the generator should use for generating the code.
    pub fn box_flags(mut self, value: BoxFlags) -> Self {
        self.meta.box_flags = value;

        self
    }

    /// Set the [`TypedefMode`] value the generator should use for generating the code.
    pub fn typedef_mode(mut self, value: TypedefMode) -> Self {
        self.meta.typedef_mode = value;

        self
    }

    /// Set the [`GeneratorFlags`] flags the generator should use for generating the code.
    pub fn flags(mut self, value: GeneratorFlags) -> Self {
        self.meta.flags = value;

        self
    }

    /// Set the type to use to store unformatted text.
    ///
    /// # Errors
    ///
    /// Forwards the error that is thrown, if `path` could not be converted.
    pub fn text_type<P>(mut self, path: P) -> Result<Self, P::Error>
    where
        P: TryInto<IdentPath>,
    {
        self.meta.text_type = path.try_into()?;

        Ok(self)
    }

    /// Set the type to use to store mixed types.
    ///
    /// # Errors
    ///
    /// Forwards the error that is thrown, if `path` could not be converted.
    pub fn mixed_type<P>(mut self, path: P) -> Result<Self, P::Error>
    where
        P: TryInto<IdentPath>,
    {
        self.meta.mixed_type = path.try_into()?;

        Ok(self)
    }

    /// Set the type to use to store nillable types.
    ///
    /// # Errors
    ///
    /// Forwards the error that is thrown, if `path` could not be converted.
    pub fn nillable_type<P>(mut self, path: P) -> Result<Self, P::Error>
    where
        P: TryInto<IdentPath>,
    {
        self.meta.nillable_type = path.try_into()?;

        Ok(self)
    }

    /// Set the type to use to store unstructured `xs:any` elements.
    ///
    /// # Errors
    ///
    /// Forwards the error that is thrown, if `path` could not be converted.
    pub fn any_type<P>(mut self, path: P) -> Result<Self, P::Error>
    where
        P: TryInto<IdentPath>,
    {
        self.meta.any_type = path.try_into()?;

        Ok(self)
    }

    /// Set the type to use to store unstructured `xs:anyAttribute` attributes.
    ///
    /// # Errors
    ///
    /// Forwards the error that is thrown, if `path` could not be converted.
    pub fn any_attributes_type<P>(mut self, path: P) -> Result<Self, P::Error>
    where
        P: TryInto<IdentPath>,
    {
        self.meta.any_attributes_type = path.try_into()?;

        Ok(self)
    }

    /// Add the passed [`GeneratorFlags`] flags the generator should use for generating the code.
    pub fn with_flags(mut self, value: GeneratorFlags) -> Self {
        self.meta.flags |= value;

        self
    }

    /// Set the postfixes the generator should use for the different types.
    ///
    /// Default is `"Type"` for the [`IdentType::Type`] type and `""` for the other types.
    pub fn with_type_postfix<S: Into<String>>(mut self, type_: IdentType, postfix: S) -> Self {
        self.meta.postfixes[type_ as usize] = postfix.into();

        self
    }

    /// Add a custom implemented type to the generator.
    ///
    /// This will add a custom implemented type to the generator. These types are
    /// usually implemented and provided by the user of the generated code. The
    /// generator will just reference to the type definition and will not generate
    /// any code related to this type.
    ///
    /// # Errors
    ///
    /// Returns an error if the namespace of the passed identifier is unknown.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let generator = Generator::new(types)
    ///     .with_type(TypeIdent::type_("UserDefinedType"));
    /// ```
    pub fn with_type(mut self, ident: TypeIdent) -> Result<Self, Error> {
        let module_ident = self
            .meta
            .types
            .naming
            .format_module(self.meta.types, Some(ident.ns));
        let type_ident = format_ident!("{}", ident.name.to_string());
        let path = PathData::from_path(IdentPath::from_parts(module_ident, type_ident));

        let id = self.state.loop_detection.next_id(ident.clone());
        let type_ref = TypeRef::new_fixed(id, path);

        self.state.cache.insert(ident, type_ref);

        Ok(self)
    }

    /// Will fix the generator by call [`into_fixed`](Self::into_fixed) and then
    /// [`generate_type`](GeneratorFixed::generate_type).
    ///
    /// # Errors
    ///
    /// Raises an [`Error`] if the type generation failed.
    #[instrument(err, level = "trace", skip(self))]
    pub fn generate_type(self, ident: TypeIdent) -> Result<GeneratorFixed<'types>, Error> {
        self.into_fixed().generate_type(ident)
    }

    /// Will fix the generator by call [`into_fixed`](Self::into_fixed) and then
    /// [`generate_named_types`](GeneratorFixed::generate_named_types).
    ///
    /// # Errors
    ///
    /// Will just forward the errors from [`generate_named_types`](GeneratorFixed::generate_named_types).
    #[instrument(err, level = "trace", skip(self))]
    pub fn generate_named_types(self) -> Result<GeneratorFixed<'types>, Error> {
        self.into_fixed().generate_named_types()
    }

    /// Will fix the generator by call [`into_fixed`](Self::into_fixed) and then
    /// [`generate_all_types`](GeneratorFixed::generate_all_types).
    ///
    /// # Errors
    ///
    /// Will just forward the errors from [`generate_all_types`](GeneratorFixed::generate_all_types).
    pub fn generate_all_types(self) -> Result<GeneratorFixed<'types>, Error> {
        self.into_fixed().generate_all_types()
    }

    /// Will convert the generator into a [`GeneratorFixed`].
    ///
    /// You need to call this method if the general configuration of the generator
    /// is finished. The resulting [`GeneratorFixed`] type will only provide methods
    /// to generate data types for specific types. The underlying configuration can
    /// not be changed anymore.
    pub fn into_fixed(self) -> GeneratorFixed<'types> {
        let Self { meta, state } = self;

        let data_types = DataTypes::new(meta);

        GeneratorFixed { state, data_types }
    }
}

impl<'types> GeneratorFixed<'types> {
    /// Generate the code for the given type.
    ///
    /// This will generate the code for the passed type identifier and all
    /// dependencies of this type.
    ///
    /// # Errors
    ///
    /// Raises an [`Error`] if the type generation failed.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let generator = Generator::new(types)
    ///     .generate_type(Ident::type_("Root"));
    /// ```
    #[instrument(err, level = "trace", skip(self))]
    pub fn generate_type(mut self, ident: TypeIdent) -> Result<GeneratorFixed<'types>, Error> {
        self.state
            .get_or_create_type_ref_mut(&self.data_types.meta, &ident)?;
        self.generate_pending()?;

        Ok(self)
    }

    /// Generate the code for all types.
    ///
    /// This will generate the code for all types that are specified in
    /// the [`MetaTypes`] object passed to the generator.
    ///
    /// # Errors
    ///
    /// Raises an [`Error`] if the type generation failed.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let generator = Generator::new(types)
    ///     .generate_all_types();
    /// ```
    #[instrument(err, level = "trace", skip(self))]
    pub fn generate_all_types(mut self) -> Result<Self, Error> {
        for ident in self.data_types.meta.types.items.keys() {
            self.state
                .get_or_create_type_ref_mut(&self.data_types.meta, ident)?;
        }
        self.generate_pending()?;

        Ok(self)
    }

    /// Generate the code for all named types.
    ///
    /// This will generate the code for all types with an explicit name and all
    /// dependencies of these types that are specified in the [`MetaTypes`] object
    /// passed to the generator.
    ///
    /// # Errors
    ///
    /// Raises an [`Error`] if the type generation failed.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let generator = Generator::new(types)
    ///     .generate_named_types();
    /// ```
    #[instrument(err, level = "trace", skip(self))]
    pub fn generate_named_types(mut self) -> Result<Self, Error> {
        for ident in self.data_types.meta.types.items.keys() {
            if ident.name.is_named() {
                self.state
                    .get_or_create_type_ref_mut(&self.data_types.meta, ident)?;
            }
        }
        self.generate_pending()?;

        Ok(self)
    }

    /// Finish the code generation.
    ///
    /// This will return the generated data types as [`DataTypes`].
    #[instrument(level = "trace", skip(self))]
    pub fn finish(self) -> DataTypes<'types> {
        self.data_types
    }

    #[instrument(err, level = "trace", skip(self))]
    fn generate_pending(&mut self) -> Result<(), Error> {
        while let Some(args) = self.state.pending.pop_front() {
            self.generate_type_intern(args)?;
        }

        Ok(())
    }

    #[instrument(err, level = "trace", skip(self))]
    fn generate_type_intern(&mut self, data: PendingType<'types>) -> Result<(), Error> {
        let PendingType { ty, ident } = data;
        let Self { state, data_types } = self;

        let mut context = Context::new(&data_types.meta, &ident, state);
        let ty = DataType::new(ty, &mut context)?;

        data_types.items.insert(ident, ty);

        Ok(())
    }
}

impl<'types> State<'types> {
    #[instrument(level = "trace", skip(self, meta))]
    fn get_or_create_type_ref_mut(
        &mut self,
        meta: &MetaData<'types>,
        ident: &TypeIdent,
    ) -> Result<&mut TypeRef, Error> {
        match self.cache.entry(ident.clone()) {
            Entry::Occupied(e) => Ok(e.into_mut()),
            Entry::Vacant(e) => {
                let id = self.loop_detection.next_id(e.key().clone());

                Self::create_type_ref(id, &*meta.naming, &mut self.pending, e, meta, ident)
            }
        }
    }

    fn create_type_ref<'a>(
        id: usize,
        naming: &dyn Naming,
        pending: &mut VecDeque<PendingType<'types>>,
        entry: VacantEntry<'a, TypeIdent, TypeRef>,
        meta: &MetaData<'types>,
        ident: &TypeIdent,
    ) -> Result<&'a mut TypeRef, Error> {
        let ty = meta
            .types
            .items
            .get(ident)
            .ok_or_else(|| Error::UnknownType(ident.clone()))?;
        let name = naming.make_type_name(&meta.postfixes, ty, ident);
        let path = match &ty.variant {
            MetaTypeVariant::BuildIn(x) => {
                let path = if meta.check_generator_flags(GeneratorFlags::BUILD_IN_ABSOLUTE_PATHS) {
                    x.absolute_ident_path()
                } else {
                    x.ident_path()
                };

                PathData::from_path(path)
            }
            MetaTypeVariant::Custom(x) => {
                if let Some(using) = x.include() {
                    if meta.check_generator_flags(GeneratorFlags::ABSOLUTE_PATHS_INSTEAD_USINGS) {
                        let path = IdentPath::from_str(using)?;

                        PathData::from_path(path)
                    } else {
                        let path = IdentPath::from_ident(format_ident!("{}", x.name()));

                        PathData::from_path(path).with_using(using)
                    }
                } else {
                    let path = IdentPath::from_ident(format_ident!("{}", x.name())).with_path(None);

                    PathData::from_path(path)
                }
            }
            _ => {
                let module_ident = ModuleIdent::new(
                    meta.types,
                    ident,
                    meta.check_generator_flags(GeneratorFlags::USE_NAMESPACE_MODULES),
                    meta.check_generator_flags(GeneratorFlags::USE_SCHEMA_MODULES),
                );
                let module_path = ModulePath::from_ident(meta.types, module_ident);
                let type_ident = naming.format_type_ident(&name, ty.display_name.as_deref());

                let path = IdentPath::from_parts(module_path.0, type_ident);

                PathData::from_path(path)
            }
        };

        tracing::debug!("Queue new type generation: {ident}");

        pending.push_back(PendingType {
            ty,
            ident: ident.clone(),
        });

        Ok(entry.insert(TypeRef::new_pending(id, path)))
    }
}