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
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
//! Schema interpretation logic for transforming parsed XML schemas into semantic
//! type definitions.
//!
//! This module defines the [`Interpreter`] type, which processes raw [`Schemas`] loaded
//! by the [`Parser`](crate::Parser) and converts them into semantic [`MetaTypes`].
//! These types represent meaningful, structured representations such as complex types,
//! enums, references, and attributes.
//!
//! The interpreter is capable of:
//! - registering custom or user-defined types
//! - resolving XSD primitive types and typedefs
//! - adding default build-in or XML-specific types (e.g., `xs:string`, `xs:anyType`)
//! - integrating numeric backends (e.g., `num::BigInt`) for large integers
//!
//! The resulting [`MetaTypes`] structure can then be passed to the generator to
//! generate Rust specific type structures.
//!
//! # Example
//! ```rust,ignore
//! let meta_types = Interpreter::new(&schemas)
//!     .with_buildin_types()?
//!     .with_default_typedefs()?
//!     .finish()?;
//! ```

mod error;
mod state;

use std::fmt::Debug;

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

use xsd_parser_types::misc::Namespace;

use crate::models::meta::{
    AnyAttributeMeta, AnyMeta, AttributeMeta, BuildInMeta, ComplexMeta, CustomMeta, ElementMeta,
    GroupMeta, MetaType, MetaTypeVariant, MetaTypes, ReferenceMeta, SimpleMeta,
};
use crate::models::schema::{
    xs::{FormChoiceType, ProcessContentsType},
    MaxOccurs, NamespaceId, Schemas,
};
use crate::models::{AttributeIdent, ElementIdent, IdentCache, Name, TypeIdent};
use crate::pipeline::generator::{
    Context as GeneratorContext, Error as GeneratorError, ValueGenerator, ValueGeneratorMode,
};
use crate::pipeline::renderer::{Context as RendererContext, ValueRendererBox};
use crate::traits::{NameBuilderExt as _, Naming};

pub use error::Error;

use self::state::State;

/// The `Interpreter` transforms raw parsed XML schema data into semantically
/// meaningful Rust-compatible type metadata.
///
/// It operates on a [`Schemas`] structure produced by the [`Parser`](crate::Parser)
/// and produces a [`MetaTypes`] structure, which is the central format used for
/// code generation.
///
/// This abstraction allows the intermediate schema format to be reshaped into a form
/// suitable for deterministic and idiomatic Rust code generation.
#[must_use]
#[derive(Debug)]
pub struct Interpreter<'a> {
    state: State<'a>,
}

impl<'a> Interpreter<'a> {
    /// Create a new [`Interpreter`] instance using the passed `schemas` reference.
    pub fn new(schemas: &'a Schemas) -> Self {
        let state = State::new(schemas);

        Self { state }
    }

    /// Add a custom [`MetaType`] information for the passed `ident`ifier to the
    /// resulting [`MetaTypes`] structure.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    #[instrument(err, level = "trace", skip(self))]
    pub fn with_type<I, T>(mut self, ident: I, type_: T) -> Result<Self, Error>
    where
        I: Into<TypeIdent> + Debug,
        T: Into<MetaType> + Debug,
    {
        self.state.add_type(ident, type_)?;

        Ok(self)
    }

    /// Add a simple type definition to the resulting [`MetaTypes`] structure using
    /// `ident` as identifier for the new type and `type_` as target type for the
    /// type definition.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    #[instrument(err, level = "trace", skip(self))]
    pub fn with_typedef<I, T>(mut self, ident: I, type_: T) -> Result<Self, Error>
    where
        I: Into<TypeIdent> + Debug,
        T: Into<TypeIdent> + Debug,
    {
        self.state.add_type(ident, ReferenceMeta::new(type_))?;

        Ok(self)
    }

    /// Adds the default build-in types to the resulting [`MetaTypes`] structure.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    #[instrument(err, level = "trace", skip(self))]
    pub fn with_buildin_types(mut self) -> Result<Self, Error> {
        let anonymous = self
            .state
            .schemas()
            .resolve_namespace(&None)
            .ok_or_else(|| Error::AnonymousNamespaceIsUndefined)?;

        macro_rules! add {
            ($ident:ident, $type:ident) => {{
                let ident = TypeIdent::$ident.with_ns(anonymous);
                let ty = BuildInMeta::$type;

                self.state.add_type(ident, ty)?;
            }};
        }

        add!(U8, U8);
        add!(U16, U16);
        add!(U32, U32);
        add!(U64, U64);
        add!(U128, U128);
        add!(USIZE, Usize);

        add!(I8, I8);
        add!(I16, I16);
        add!(I32, I32);
        add!(I64, I64);
        add!(I128, I128);
        add!(ISIZE, Isize);

        add!(F32, F32);
        add!(F64, F64);

        add!(BOOL, Bool);
        add!(STR, Str);
        add!(STRING, String);

        Ok(self)
    }

    /// Adds the type definitions for common XML types (like `xs:string` or `xs:int`)
    /// to the resulting [`MetaTypes`] structure.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    #[instrument(err, level = "trace", skip(self))]
    pub fn with_default_typedefs(mut self) -> Result<Self, Error> {
        let anonymous = self
            .state
            .schemas()
            .resolve_namespace(&None)
            .ok_or_else(|| Error::AnonymousNamespaceIsUndefined)?;
        let xs = self
            .state
            .schemas()
            .resolve_namespace(&Some(Namespace::XS))
            .ok_or_else(|| Error::UnknownNamespace(Namespace::XS.clone()))?;

        macro_rules! add {
            ($src:expr, $dst:ident) => {{
                let src = TypeIdent::type_($src).with_ns(xs);
                let dst = TypeIdent::$dst.with_ns(anonymous);

                self.state.add_type(src, ReferenceMeta::new(dst))?;
            }};
        }
        macro_rules! add_list {
            ($src:expr, $dst:ident) => {{
                let src = TypeIdent::type_($src).with_ns(xs);
                let dst = TypeIdent::$dst.with_ns(anonymous);

                self.state.add_type(
                    src,
                    ReferenceMeta::new(dst)
                        .min_occurs(0)
                        .max_occurs(MaxOccurs::Unbounded),
                )?;
            }};
        }

        /* Primitive Types */

        add!("string", STRING);
        add!("boolean", BOOL);
        add!("decimal", F64);
        add!("float", F32);
        add!("double", F64);

        /* time related types */

        add!("duration", STRING);
        add!("dateTime", STRING);
        add!("time", STRING);
        add!("date", STRING);
        add!("gYearMonth", STRING);
        add!("gYear", STRING);
        add!("gMonthDay", STRING);
        add!("gMonth", STRING);
        add!("gDay", STRING);

        /* Data related types */

        add!("hexBinary", STRING);
        add!("base64Binary", STRING);

        /* URL related types */

        add!("anyURI", STRING);
        add!("QName", STRING);
        add!("NOTATION", STRING);

        /* Numeric Types */

        add!("long", I64);
        add!("int", I32);
        add!("integer", I32);
        add!("short", I16);
        add!("byte", I8);
        add!("negativeInteger", ISIZE);
        add!("nonPositiveInteger", ISIZE);

        add!("unsignedLong", U64);
        add!("unsignedInt", U32);
        add!("unsignedShort", U16);
        add!("unsignedByte", U8);
        add!("positiveInteger", USIZE);
        add!("nonNegativeInteger", USIZE);

        /* String Types */

        add!("normalizedString", STRING);
        add!("token", STRING);
        add!("language", STRING);
        add!("NMTOKEN", STRING);
        add!("Name", STRING);
        add!("NCName", STRING);
        add!("ID", STRING);
        add!("IDREF", STRING);
        add!("ENTITY", STRING);

        add!("anySimpleType", STRING);

        add_list!("NMTOKENS", STRING);
        add_list!("IDREFS", STRING);
        add_list!("ENTITIES", STRING);

        Ok(self)
    }

    /// Adds a default type definition for `xs:anyType`.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    #[instrument(err, level = "trace", skip(self))]
    pub fn with_xs_any_type(mut self) -> Result<Self, Error> {
        let xs = self
            .state
            .schemas()
            .resolve_namespace(&Some(Namespace::XS))
            .ok_or_else(|| Error::UnknownNamespace(Namespace::XS.clone()))?;

        /* content type */

        let any_ident = ElementIdent::new(Name::ANY);
        let mut any = ElementMeta::any(
            any_ident,
            AnyMeta {
                id: None,
                namespace: None,
                not_q_name: None,
                not_namespace: None,
                process_contents: ProcessContentsType::Lax,
            },
        );
        any.min_occurs = 0;
        any.max_occurs = MaxOccurs::Unbounded;

        let mut content_sequence = GroupMeta {
            is_mixed: true,
            ..GroupMeta::default()
        };
        content_sequence.elements.push(any);

        let content_name = self.state.name_builder().shared_name("Content").finish();
        let content_ident = TypeIdent::new(content_name).with_ns(xs);
        let content_variant = MetaTypeVariant::Sequence(content_sequence);
        let content_type = MetaType::new(content_variant);

        self.state.add_type(content_ident.clone(), content_type)?;

        /* xs:anyType */

        let ident = TypeIdent::new(Name::ANY_TYPE).with_ns(xs);
        let any_attribute_ident = AttributeIdent::new(Name::ANY_ATTRIBUTE);
        let any_attribute = AttributeMeta::any(
            any_attribute_ident,
            AnyAttributeMeta {
                id: None,
                namespace: None,
                not_q_name: None,
                not_namespace: None,
                process_contents: ProcessContentsType::Lax,
            },
        );

        let mut complex = ComplexMeta {
            content: Some(content_ident),
            is_mixed: true,
            min_occurs: 1,
            max_occurs: MaxOccurs::Bounded(1),
            ..Default::default()
        };
        complex.attributes.push(any_attribute);

        let variant = MetaTypeVariant::ComplexType(complex);
        let type_ = MetaType::new(variant);

        self.state.add_type(ident, type_)?;

        Ok(self)
    }

    /// Adds a default type definition for `xs:anySimpleType`.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    #[instrument(err, level = "trace", skip(self))]
    pub fn with_xs_any_simple_type(mut self) -> Result<Self, Error> {
        let xs = self
            .state
            .schemas()
            .resolve_namespace(&Some(Namespace::XS))
            .ok_or_else(|| Error::UnknownNamespace(Namespace::XS.clone()))?;
        let xsi = self
            .state
            .schemas()
            .resolve_namespace(&Some(Namespace::XSI))
            .ok_or_else(|| Error::UnknownNamespace(Namespace::XSI.clone()))?;

        /* content type */

        let content_name = self.state.name_builder().shared_name("Content").finish();
        let content_ident = TypeIdent::new(content_name).with_ns(xs);
        let content_type = MetaType::new(MetaTypeVariant::SimpleType(SimpleMeta::new(
            TypeIdent::STRING,
        )));

        self.state.add_type(content_ident.clone(), content_type)?;

        /* xs:anySimpleType */

        let type_attribute_ident = AttributeIdent::new(Name::TYPE).with_ns(xsi);
        let type_attribute = AttributeMeta::new(
            type_attribute_ident,
            TypeIdent::STRING,
            FormChoiceType::Qualified,
        );

        let mut complex = ComplexMeta {
            content: Some(content_ident),
            is_mixed: true,
            min_occurs: 1,
            max_occurs: MaxOccurs::Bounded(1),
            ..Default::default()
        };
        complex.attributes.push(type_attribute);

        let ident = TypeIdent::new(Name::ANY_SIMPLE_TYPE).with_ns(xs);
        let variant = MetaTypeVariant::ComplexType(complex);
        let type_ = MetaType::new(variant);

        self.state.add_type(ident, type_)?;

        Ok(self)
    }

    /// Add a type definition for `xs:QName` that uses the
    /// `xsd_parser_types::xml::QName` type.
    pub fn with_qname_type(self) -> Result<Self, Error> {
        self.with_qname_type_from("::xsd_parser_types::xml::QName")
    }

    /// Add a type definition for `xs:QName` that uses the type defined at the passed `path`.
    pub fn with_qname_type_from(self, path: &str) -> Result<Self, Error> {
        let xs = self
            .state
            .schemas()
            .resolve_namespace(&Some(Namespace::XS))
            .ok_or_else(|| Error::UnknownNamespace(Namespace::XS.clone()))?;

        let name = path.rsplit_once("::").map_or(path, |(_, name)| name);

        self.with_type(
            TypeIdent::type_("QName").with_ns(xs),
            CustomMeta::new(name)
                .include_from(path)
                .with_namespace(xs)
                .with_default(crate::misc::qname_default),
        )
    }

    /// Add type definitions for numeric XML types (like `xs:int`) that
    /// uses `num::BigInt` and `num::BigUint` instead of build-in integer types.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    pub fn with_num_big_int(mut self) -> Result<Self, Error> {
        let xs = self
            .state
            .schemas()
            .resolve_namespace(&Some(Namespace::XS))
            .ok_or_else(|| Error::UnknownNamespace(Namespace::XS.clone()))?;

        macro_rules! add {
            ($src:expr, $dst:expr) => {{
                self.state
                    .add_type(TypeIdent::type_($src).with_ns(xs), ReferenceMeta::new($dst))?;
            }};
        }

        let big_int = CustomMeta::new("BigInt")
            .include_from("::num::BigInt")
            .with_default(make_from_str_value_generator("::num::BigInt"));

        let big_uint = CustomMeta::new("BigUint")
            .include_from("::num::BigUint")
            .with_default(make_from_str_value_generator("::num::BigUint"));

        let ident_big_int = TypeIdent::type_("BigInt").with_ns(NamespaceId::ANONYMOUS);
        let ident_big_uint = TypeIdent::type_("BigUint").with_ns(NamespaceId::ANONYMOUS);

        self.state.add_type(ident_big_int.clone(), big_int)?;
        self.state.add_type(ident_big_uint.clone(), big_uint)?;

        add!("integer", ident_big_int.clone());
        add!("negativeInteger", ident_big_int.clone());
        add!("nonPositiveInteger", ident_big_int);

        add!("positiveInteger", ident_big_uint.clone());
        add!("nonNegativeInteger", ident_big_uint);

        Ok(self)
    }

    /// Add type definitions for numeric XML types (like `xs:positiveInteger`) that
    /// uses `::core::num::NonZeroIsize` and `::core::num::NonZeroUsize` instead
    /// of the simple integer types.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    pub fn with_nonzero_typedefs(mut self) -> Result<Self, Error> {
        let xs = self
            .state
            .schemas()
            .resolve_namespace(&Some(Namespace::XS))
            .ok_or_else(|| Error::UnknownNamespace(Namespace::XS.clone()))?;

        macro_rules! add {
            ($src:expr, $dst:expr) => {{
                self.state
                    .add_type(TypeIdent::type_($src).with_ns(xs), ReferenceMeta::new($dst))?;
            }};
        }

        let non_zero_usize = CustomMeta::new("NonZeroUsize")
            .include_from("::core::num::NonZeroUsize")
            .with_default(make_from_str_value_generator("::core::num::NonZeroUsize"));
        let non_zero_isize = CustomMeta::new("NonZeroIsize")
            .include_from("::core::num::NonZeroIsize")
            .with_default(make_from_str_value_generator("::core::num::NonZeroIsize"));

        let ident_non_zero_usize = TypeIdent::type_("NonZeroUsize").with_ns(NamespaceId::ANONYMOUS);
        let ident_non_zero_isize = TypeIdent::type_("NonZeroIsize").with_ns(NamespaceId::ANONYMOUS);

        self.state
            .add_type(ident_non_zero_usize.clone(), non_zero_usize)?;
        self.state
            .add_type(ident_non_zero_isize.clone(), non_zero_isize)?;

        add!("positiveInteger", ident_non_zero_usize);
        add!("negativeInteger", ident_non_zero_isize);

        Ok(self)
    }

    /// Set the [`Naming`](Naming) trait that is used to generate and format names.
    ///
    /// This accepts any type that implements the [`Naming`](Naming) trait.
    /// If you want to use an already boxed version have a look at
    /// [`with_naming_boxed`](Self::with_naming_boxed).
    #[instrument(level = "trace", skip(self))]
    pub fn with_naming<X>(self, naming: X) -> Self
    where
        X: Naming + 'static,
    {
        self.with_naming_boxed(Box::new(naming))
    }

    /// Set the [`Naming`] trait that is used to generate and format names.
    ///
    /// This accepts only boxed [`Naming`](Naming) trait. For easier use you can
    /// use [`with_naming`](Self::with_naming) instead.
    #[instrument(level = "trace", skip(self))]
    pub fn with_naming_boxed(mut self, naming: Box<dyn Naming>) -> Self {
        self.state.set_naming(naming);

        self
    }

    /// Finishes the interpretation of the [`Schemas`] structure and returns
    /// the [`MetaTypes`] structure with the generated type information.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the operation was not successful.
    #[instrument(err, level = "trace", skip(self))]
    pub fn finish(self) -> Result<(MetaTypes, IdentCache), Error> {
        self.state.finish()
    }
}

fn make_from_str_value_generator(type_path: &'static str) -> impl ValueGenerator + 'static {
    move |ctx: &GeneratorContext<'_, '_>,
          value: &str,
          mode: ValueGeneratorMode|
          -> Result<ValueRendererBox, GeneratorError> {
        if mode != ValueGeneratorMode::Value {
            return Err(GeneratorError::InvalidDefaultValue {
                ident: ctx.ident.clone(),
                value: value.into(),
                mode,
            });
        }

        let s = value.to_string();

        Ok(Box::new(move |ctx: &RendererContext<'_, '_>| {
            let type_ = ctx.resolve_ident_path(type_path);
            let from_str = ctx.resolve_ident_path("::core::str::FromStr");

            quote! {
                <#type_ as #from_str>::from_str(#s).unwrap()
            }
        }))
    }
}