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
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
//! Contains the [`Config`] structures for the [`generate`](super::generate) method.

mod generator;
mod interpreter;
mod optimizer;
mod parser;
mod renderer;

use url::Url;

pub use xsd_parser_types::misc::{Namespace, NamespacePrefix};

pub use crate::models::{meta::MetaType, schema::NamespaceId, IdentType, Name, TypeIdent};

use crate::models::{
    meta::CustomMeta,
    schema::{xs::SchemaContent, SchemaId, Schemas},
};
use crate::pipeline::renderer::NamespaceSerialization;
use crate::traits::Naming;
use crate::InterpreterError;

pub use self::generator::{
    BoxFlags, Generate, GeneratorConfig, GeneratorFlags, TypePostfix, TypedefMode,
};
pub use self::interpreter::{InterpreterConfig, InterpreterFlags};
pub use self::optimizer::{OptimizerConfig, OptimizerFlags};
pub use self::parser::{ParserConfig, ParserFlags, Resolver, Schema};
pub use self::renderer::{
    DynTypeTraits, RenderStep, RenderStepConfig, RendererConfig, RendererFlags, SerdeXmlRsVersion,
};

/// Configuration structure for the [`generate`](super::generate) method.
#[must_use]
#[derive(Default, Debug, Clone)]
pub struct Config {
    /// Configuration for the schema parser.
    pub parser: ParserConfig,

    /// Configuration for the schema interpreter.
    pub interpreter: InterpreterConfig,

    /// Configuration for the type information optimizer.
    pub optimizer: OptimizerConfig,

    /// Configuration for the code generator.
    pub generator: GeneratorConfig,

    /// Configuration for the code renderer.
    pub renderer: RendererConfig,
}

impl Config {
    /// Adds the passed `schema` to the list of schemas to parse.
    pub fn with_schema<T>(mut self, schema: T) -> Self
    where
        T: Into<Schema>,
    {
        self.parser.schemas.push(schema.into());

        self
    }

    /// Adds the passed `schemas` to the list of schemas to parse.
    pub fn with_schemas<I>(mut self, schemas: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<Schema>,
    {
        for schema in schemas {
            self = self.with_schema(schema.into());
        }

        self
    }

    /// Set parser flags to the config.
    pub fn set_parser_flags(mut self, flags: ParserFlags) -> Self {
        self.parser.flags = flags;

        self
    }

    /// Add parser flags to the config.
    pub fn with_parser_flags(mut self, flags: ParserFlags) -> Self {
        self.parser.flags.insert(flags);

        self
    }

    /// Remove parser flags to the config.
    pub fn without_parser_flags(mut self, flags: ParserFlags) -> Self {
        self.parser.flags.remove(flags);

        self
    }

    /// Set interpreter flags to the config.
    pub fn set_interpreter_flags(mut self, flags: InterpreterFlags) -> Self {
        self.interpreter.flags = flags;

        self
    }

    /// Add code interpreter flags to the config.
    pub fn with_interpreter_flags(mut self, flags: InterpreterFlags) -> Self {
        self.interpreter.flags.insert(flags);

        self
    }

    /// Remove code interpreter flags to the config.
    pub fn without_interpreter_flags(mut self, flags: InterpreterFlags) -> Self {
        self.interpreter.flags.remove(flags);

        self
    }

    /// Set optimizer flags to the config.
    pub fn set_optimizer_flags(mut self, flags: OptimizerFlags) -> Self {
        self.optimizer.flags = flags;

        self
    }

    /// Add optimizer flags to the config.
    pub fn with_optimizer_flags(mut self, flags: OptimizerFlags) -> Self {
        self.optimizer.flags.insert(flags);

        self
    }

    /// Remove optimizer flags to the config.
    pub fn without_optimizer_flags(mut self, flags: OptimizerFlags) -> Self {
        self.optimizer.flags.remove(flags);

        self
    }

    /// Set generator flags to the config.
    pub fn set_generator_flags(mut self, flags: GeneratorFlags) -> Self {
        self.generator.flags = flags;

        self
    }

    /// Add code generator flags to the config.
    pub fn with_generator_flags(mut self, flags: GeneratorFlags) -> Self {
        self.generator.flags.insert(flags);

        self
    }

    /// Remove code generator flags to the config.
    pub fn without_generator_flags(mut self, flags: GeneratorFlags) -> Self {
        self.generator.flags.remove(flags);

        self
    }

    /// Set renderer flags to the config.
    pub fn set_renderer_flags(mut self, flags: RendererFlags) -> Self {
        self.renderer.flags = flags;

        self
    }

    /// Add code renderer flags to the config.
    pub fn with_renderer_flags(mut self, flags: RendererFlags) -> Self {
        self.renderer.flags.insert(flags);

        self
    }

    /// Remove code renderer flags to the config.
    pub fn without_renderer_flags(mut self, flags: RendererFlags) -> Self {
        self.renderer.flags.remove(flags);

        self
    }

    /// Set boxing flags to the code generator config.
    pub fn set_box_flags(mut self, flags: BoxFlags) -> Self {
        self.generator.box_flags = flags;

        self
    }

    /// Add boxing flags to the code generator config.
    pub fn with_box_flags(mut self, flags: BoxFlags) -> Self {
        self.generator.box_flags.insert(flags);

        self
    }

    /// Remove boxing flags to the code generator config.
    pub fn without_box_flags(mut self, flags: BoxFlags) -> Self {
        self.generator.box_flags.remove(flags);

        self
    }

    /// Adds the passed `step` to the config.
    ///
    /// If the same type of renderer was already added,
    /// it is replaced by the new one.
    pub fn with_render_step<T>(mut self, step: T) -> Self
    where
        T: RenderStepConfig + 'static,
    {
        let step = Box::new(step);
        let mut index = 0;
        let mut position = None;

        // Find the position to place the new step and remove any other mutual exclusive step
        self.renderer.steps.retain(|x| {
            let mut remove = x.is_mutual_exclusive_to(&*step) || step.is_mutual_exclusive_to(&**x);

            if remove && position.is_none() {
                remove = false;
                position = Some(index);
            }

            index += 1;

            !remove
        });

        // Insert at the found position or append
        if let Some(pos) = position {
            self.renderer.steps[pos] = step;
        } else {
            self.renderer.steps.push(step);
        }

        self
    }

    /// Add multiple renderers to the generator config.
    ///
    /// See [`with_render_step`](Self::with_render_step) for details.
    pub fn with_render_steps<I>(mut self, steps: I) -> Self
    where
        I: IntoIterator,
        I::Item: RenderStepConfig + 'static,
    {
        for step in steps {
            self = self.with_render_step(step);
        }

        self
    }

    /// Enable code generation for [`quick_xml`] serialization.
    pub fn with_quick_xml_serialize(self) -> Self {
        self.with_render_steps([
            RenderStep::Types,
            RenderStep::Defaults,
            RenderStep::PrefixConstants,
            RenderStep::NamespaceConstants,
            RenderStep::QuickXmlSerialize {
                namespaces: NamespaceSerialization::Global,
                default_namespace: None,
            },
        ])
    }

    /// Enable code generation for [`quick_xml`] serialization.
    pub fn with_quick_xml_serialize_config(
        mut self,
        namespaces: NamespaceSerialization,
        default_namespace: Option<Namespace>,
    ) -> Self {
        self = self.with_render_steps([RenderStep::Types, RenderStep::Defaults]);

        if namespaces != NamespaceSerialization::None {
            self = self.with_render_step(RenderStep::PrefixConstants);
        }

        if namespaces == NamespaceSerialization::Dynamic {
            self = self.with_render_step(RenderStep::QuickXmlCollectNamespaces);
        }

        self.with_render_steps([
            RenderStep::NamespaceConstants,
            RenderStep::QuickXmlSerialize {
                namespaces,
                default_namespace,
            },
        ])
    }

    /// Enable code generation for [`quick_xml`] deserialization with the default settings.
    pub fn with_quick_xml_deserialize(self) -> Self {
        self.with_quick_xml_deserialize_config(false)
    }

    /// Enable render steps for [`quick_xml`] deserialization
    /// with the passed configuration.
    pub fn with_quick_xml_deserialize_config(self, boxed_deserializer: bool) -> Self {
        self.with_render_steps([
            RenderStep::Types,
            RenderStep::Defaults,
            RenderStep::NamespaceConstants,
            RenderStep::QuickXmlDeserialize { boxed_deserializer },
        ])
    }

    /// Enable render steps for [`quick_xml`] serialization and deserialization
    /// with the default settings.
    pub fn with_quick_xml(self) -> Self {
        self.with_quick_xml_serialize().with_quick_xml_deserialize()
    }

    /// Enable render steps for [`quick_xml`] serialization and deserialization
    /// with the passed configuration.
    pub fn with_quick_xml_config(
        self,
        namespace_serialization: NamespaceSerialization,
        default_serialize_namespace: Option<Namespace>,
        boxed_deserializer: bool,
    ) -> Self {
        self.with_quick_xml_serialize_config(namespace_serialization, default_serialize_namespace)
            .with_quick_xml_deserialize_config(boxed_deserializer)
    }

    /// Enable render steps for types with [`quick_xml`] serde support.
    pub fn with_serde_quick_xml(mut self) -> Self {
        self.optimizer.flags |= OptimizerFlags::SERDE;

        self.with_render_steps([RenderStep::TypesSerdeQuickXml, RenderStep::Defaults])
    }

    /// Enable render steps for types with [`quick_xml`] serde support.
    pub fn with_serde_xml_rs(mut self, version: SerdeXmlRsVersion) -> Self {
        self.optimizer.flags |= OptimizerFlags::SERDE;

        self.with_render_steps([
            RenderStep::TypesSerdeXmlRs { version },
            RenderStep::Defaults,
        ])
    }

    /// Enable support for advanced enums.
    ///
    /// Advanced enums will automatically add constants for each enum variant
    /// using its simple base type. During deserialization, the base type is
    /// deserialized first and then compared against the defined constants to
    /// determine the actual enum variant. This is useful for type like `QName`
    /// where the actual used prefix can change, but the name still refers to
    /// the object.
    pub fn with_advanced_enums(self) -> Self {
        self.with_generator_flags(GeneratorFlags::ADVANCED_ENUMS)
            .with_render_step(RenderStep::EnumConstants)
    }

    /// Add a namespace to the parser config.
    ///
    /// See [`ParserConfig::namespaces`] for more details.
    pub fn with_namespace<P, N>(mut self, prefix: P, namespace: N) -> Self
    where
        P: Into<NamespacePrefix>,
        N: Into<Namespace>,
    {
        self.parser
            .namespaces
            .push((prefix.into(), namespace.into()));

        self
    }

    /// Add multiple namespaces to the parser config.
    ///
    /// See [`ParserConfig::namespaces`] for more details.
    pub fn with_namespaces<I, P, N>(mut self, namespaces: I) -> Self
    where
        I: IntoIterator<Item = (P, N)>,
        P: Into<NamespacePrefix>,
        N: Into<Namespace>,
    {
        for (prefix, namespace) in namespaces {
            self.parser
                .namespaces
                .push((prefix.into(), namespace.into()));
        }

        self
    }

    /// Set the types the code should be generated for.
    pub fn with_generate<I>(mut self, types: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<IdentQuadruple>,
    {
        self.generator.generate = Generate::Types(types.into_iter().map(Into::into).collect());

        self
    }

    /// Set the typedef mode for the generator.
    pub fn with_typedef_mode(mut self, mode: TypedefMode) -> Self {
        self.generator.typedef_mode = mode;

        self
    }

    /// Set the traits the generated types should derive from.
    pub fn with_derive<I>(mut self, derive: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        self.renderer.derive = Some(
            derive
                .into_iter()
                .map(Into::into)
                .filter(|s| !s.is_empty())
                .collect(),
        );

        self
    }

    /// Enable support for `xs:any` types.
    pub fn with_any_type_support(self) -> Self {
        self.with_generator_flags(GeneratorFlags::ANY_TYPE_SUPPORT)
    }

    /// Set the types to use to handle `xs:any` and `xs:anyAttribute` elements.
    pub fn with_any_types<S, T>(mut self, any_type: S, any_attributes_type: T) -> Self
    where
        S: Into<String>,
        T: Into<String>,
    {
        self.generator.any_type = any_type.into();
        self.generator.any_attributes_type = any_attributes_type.into();

        self.with_any_type_support()
    }

    /// Enable support for mixed types.
    pub fn with_mixed_type_support(self) -> Self {
        self.with_generator_flags(GeneratorFlags::MIXED_TYPE_SUPPORT)
    }

    /// Set the types to use to handle mixed types and text data.
    pub fn with_mixed_types<S, T>(mut self, text_type: S, mixed_type: T) -> Self
    where
        S: Into<String>,
        T: Into<String>,
    {
        self.generator.text_type = text_type.into();
        self.generator.mixed_type = mixed_type.into();

        self.with_mixed_type_support()
    }

    /// Enable support for nillable types.
    pub fn with_nillable_type_support(self) -> Self {
        self.with_generator_flags(GeneratorFlags::NILLABLE_TYPE_SUPPORT)
    }

    /// Set the type to use to handle nillable elements.
    pub fn with_nillable_type<S>(mut self, nillable_type: S) -> Self
    where
        S: Into<String>,
    {
        self.generator.nillable_type = nillable_type.into();

        self.with_nillable_type_support()
    }

    /// Set the [`Naming`] trait that is used to generated names in the interpreter.
    pub fn with_naming<X>(mut self, naming: X) -> Self
    where
        X: Naming + 'static,
    {
        self.interpreter.naming = Some(Box::new(naming));

        self
    }

    /// Add a type to the interpreter.
    ///
    /// This can be used to add or overwrite type definitions to the interpreter,
    /// for example to support `xs:anySimpleType` with a custom type.
    ///
    /// # Parameters
    /// - `ident`: Identifier quadruple for the type to add/overwrite, for example
    ///   `(IdentType::Type, Namespace::XS, "anySimpleType")` for `xs:anySimpleType`.
    /// - `meta`: The type definition to use for the specified identifier.
    pub fn with_type<I, M>(mut self, ident: I, meta: M) -> Self
    where
        I: Into<IdentQuadruple>,
        M: Into<MetaType>,
    {
        self.interpreter.types.push((ident.into(), meta.into()));

        self
    }

    /// Add a type to the interpreter that should be used to handle `xs:anySimpleType`.
    ///
    /// This is a convenient method for adding support for `xs:anySimpleType` with a custom type.
    ///
    /// # Parameters
    /// - `path`: The path to the type to use for handling `xs:anySimpleType`, for example
    ///   `"xsd_parser_types::xml::AnySimpleType"`.
    pub fn with_xs_any_simple_type<S>(self, path: S) -> Self
    where
        S: AsRef<str>,
    {
        let path = path.as_ref();
        let name = path.rsplit_once("::").map_or(path, |(_, name)| name);

        let ident = (IdentType::Type, Namespace::XS, "anySimpleType");
        let meta = CustomMeta::new(name)
            .include_from(path)
            .with_namespace(Namespace::XS)
            .with_namespace(Namespace::XSI);

        self.with_type(ident, meta)
    }

    /// Add a type definition for `xs:QName` that uses the `xsd_parser_types::xml::QName` type.
    pub fn with_qname_type(self) -> Self {
        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) -> Self {
        let name = path.rsplit_once("::").map_or(path, |(_, name)| name);

        let ident = (IdentType::Type, Namespace::XS, "QName");
        let meta = CustomMeta::new(name)
            .include_from(path)
            .with_namespace(Namespace::XS)
            .with_default(crate::misc::qname_default);

        self.with_type(ident, meta)
    }

    /// Set the postfix that should be applied to the name of types.
    ///
    /// For details please refer to [`GeneratorConfig::type_postfix`].
    pub fn with_type_postfix<S>(mut self, value: S) -> Self
    where
        S: Into<String>,
    {
        self.generator.type_postfix.type_ = value.into();

        self
    }

    /// Set the postfix that should be applied to the name of elements.
    ///
    /// For details please refer to [`GeneratorConfig::type_postfix`].
    pub fn with_element_postfix<S>(mut self, value: S) -> Self
    where
        S: Into<String>,
    {
        self.generator.type_postfix.element = value.into();

        self
    }

    /// Set the postfix that should be applied to the name of element types.
    ///
    /// For details please refer to [`GeneratorConfig::type_postfix`].
    pub fn with_element_type_postfix<S>(mut self, value: S) -> Self
    where
        S: Into<String>,
    {
        self.generator.type_postfix.element_type = value.into();

        self
    }

    /// Set the postfix that should be applied to the name of nillable content types.
    ///
    /// For details please refer to [`GeneratorConfig::type_postfix`].
    pub fn with_nillable_content_postfix<S>(mut self, value: S) -> Self
    where
        S: Into<String>,
    {
        self.generator.type_postfix.nillable_content = value.into();

        self
    }

    /// Set the postfix that should be applied to the name of dynamic elements.
    ///
    /// For details please refer to [`GeneratorConfig::type_postfix`].
    pub fn with_dynamic_element_postfix<S>(mut self, value: S) -> Self
    where
        S: Into<String>,
    {
        self.generator.type_postfix.dynamic_element = value.into();

        self
    }
}

/// Convenient type to not break the public interface.
///
/// The type was renamed to [`IdentQuadruple`].
#[deprecated(note = "Use IdentQuadruple instead")]
pub type IdentTriple = IdentQuadruple;

/// Identifier that is used inside the config.
///
/// Each element in a XML schema is uniquely identified by the following four
/// values:
///     - The namespace of the element (or no namespace at all).
///     - The schema the element was defined in.
///     - The name of the element.
///     - The type of element.
///
/// This struct is used to bundle these three information to a unique identifier
/// for an element.
#[derive(Debug, Clone)]
pub struct IdentQuadruple {
    /// Namespace where the type is defined in.
    pub ns: Option<NamespaceIdent>,

    /// Id of the schema the type is defined in.
    pub schema: Option<SchemaIdent>,

    /// Name of the type.
    pub name: String,

    /// Type of the identifier (because pure names are not unique in XSD).
    pub type_: IdentType,
}

impl IdentQuadruple {
    /// Create a new [`IdentQuadruple`] instance from the passed `name` and `type_`.
    #[inline]
    #[must_use]
    pub fn new<S>(name: S, type_: IdentType) -> Self
    where
        S: Into<String>,
    {
        Self {
            ns: None,
            schema: None,
            name: name.into(),
            type_,
        }
    }

    /// Adds a [`NamespaceIdent`] to this identifier quadruple.
    #[inline]
    #[must_use]
    pub fn with_ns<X>(mut self, ns: X) -> Self
    where
        X: Into<NamespaceIdent>,
    {
        self.ns = Some(ns.into());

        self
    }

    /// Adds a [`SchemaIdent`] to this identifier quadruple.
    #[inline]
    #[must_use]
    pub fn with_schema<X>(mut self, schema: X) -> Self
    where
        X: Into<SchemaIdent>,
    {
        self.schema = Some(schema.into());

        self
    }

    /// Sets the name of the type that is identified by this identifier quadruple.
    #[inline]
    #[must_use]
    pub fn with_name<X>(mut self, name: X) -> Self
    where
        X: Into<String>,
    {
        self.name = name.into();

        self
    }

    /// Sets the identifier type of this identifier quadruple.
    #[inline]
    #[must_use]
    pub fn with_type(mut self, type_: IdentType) -> Self {
        self.type_ = type_;

        self
    }

    /// Resolve the quadruple to an actual type identifier that is available in
    /// the schema.
    ///
    /// /// <div class="warning">
    /// *Caution*
    /// This may end up in a type with the [`schema`](TypeIdent::schema) not fully
    /// resolved. This can happen if you specified the wrong schema name, or schema
    /// location. If you didn't provide a [`SchemaIdent`] at all, the type is resolved
    /// by it's name, which is also not always successful, if the type is not defined
    /// in the root of the available schemas.
    ///
    /// If you use this to get suitable identifiers to define types for the interpreter
    /// (see [`with_type`](crate::Interpreter::with_type)), then you are fine, because
    /// the interpreter will resolve unknown schema IDs by it's own.
    ///
    /// If you want to use the resolved identifier for selecting a [`MetaType`]
    /// from the resulting [`MetaTypes`](crate::MetaTypes) structure created by
    /// the interpreted, you have to resolve the type additionally using the
    /// [`IdentCache`](crate::IdentCache), which is also returned by the
    /// [`Interpreter`](crate::Interpreter)
    /// (see [`exec_interpreter_with_ident_cache`](crate::exec_interpreter_with_ident_cache)).
    /// </div>
    ///
    /// # Errors
    ///
    /// Returns an error if the namespace or the namespace prefix could not be
    /// resolved.
    pub fn resolve(self, schemas: &Schemas) -> Result<TypeIdent, InterpreterError> {
        let Self {
            ns,
            schema,
            name,
            type_,
        } = self;

        let name = Name::new_named(name);

        let ns = match ns {
            None | Some(NamespaceIdent::Anonymous) => schemas
                .resolve_namespace(&None)
                .ok_or(InterpreterError::AnonymousNamespaceIsUndefined)?,
            Some(NamespaceIdent::Id(ns)) => ns,
            Some(NamespaceIdent::Prefix(prefix)) => schemas
                .resolve_prefix(&prefix)
                .ok_or(InterpreterError::UnknownNamespacePrefix(prefix))?,
            #[allow(clippy::unnecessary_literal_unwrap)]
            Some(NamespaceIdent::Namespace(ns)) => {
                let ns = Some(ns);

                schemas
                    .resolve_namespace(&ns)
                    .ok_or_else(|| InterpreterError::UnknownNamespace(ns.unwrap()))?
            }
        };

        let schema = match schema {
            None => schemas
                .get_namespace_info(&ns)
                .and_then(|x| {
                    if x.schemas.len() == 1 {
                        Some(x.schemas[0])
                    } else {
                        None
                    }
                })
                .or_else(|| schemas.resolve_schema(ns, name.as_str(), type_))
                .unwrap_or(SchemaId::UNKNOWN),
            Some(SchemaIdent::Id(schema)) => schema,
            Some(SchemaIdent::Name(s)) => schemas
                .schemas()
                .find(|(_, info)| matches!(&info.name, Some(name) if *name == s))
                .map_or(SchemaId::UNKNOWN, |(id, _)| *id),
            Some(SchemaIdent::Location(url)) => schemas
                .schemas()
                .find(|(_, info)| matches!(&info.location, Some(location) if *location == url))
                .map_or(SchemaId::UNKNOWN, |(id, _)| *id),
        };

        Ok(TypeIdent {
            ns,
            schema,
            name,
            type_,
        })
    }
}

impl<X> From<(IdentType, X)> for IdentQuadruple
where
    X: AsRef<str>,
{
    fn from((type_, ident): (IdentType, X)) -> Self {
        let ident = ident.as_ref();
        let (prefix, name) = ident
            .split_once(':')
            .map_or((None, ident), |(a, b)| (Some(a), b));
        let ns = prefix.map(|x| NamespaceIdent::prefix(x.as_bytes().to_owned()));
        let name = name.to_owned();
        let schema = None;

        Self {
            ns,
            schema,
            name,
            type_,
        }
    }
}

impl<N, X> From<(IdentType, N, X)> for IdentQuadruple
where
    N: Into<NamespaceIdent>,
    X: Into<String>,
{
    fn from((type_, ns, name): (IdentType, N, X)) -> Self {
        Self::from((type_, Some(ns), name))
    }
}

impl<N, X> From<(IdentType, Option<N>, X)> for IdentQuadruple
where
    N: Into<NamespaceIdent>,
    X: Into<String>,
{
    fn from((type_, ns, name): (IdentType, Option<N>, X)) -> Self {
        let ns = ns.map(Into::into);
        let name = name.into();
        let schema = None;

        Self {
            ns,
            schema,
            name,
            type_,
        }
    }
}

impl<N, S, X> From<(IdentType, N, S, X)> for IdentQuadruple
where
    N: Into<NamespaceIdent>,
    S: Into<SchemaIdent>,
    X: Into<String>,
{
    fn from((type_, ns, schema, name): (IdentType, N, S, X)) -> Self {
        Self::from((type_, Some(ns), Some(schema), name))
    }
}

impl<N, S, X> From<(IdentType, Option<N>, S, X)> for IdentQuadruple
where
    N: Into<NamespaceIdent>,
    S: Into<SchemaIdent>,
    X: Into<String>,
{
    fn from((type_, ns, schema, name): (IdentType, Option<N>, S, X)) -> Self {
        Self::from((type_, ns, Some(schema), name))
    }
}

impl<N, S, X> From<(IdentType, N, Option<S>, X)> for IdentQuadruple
where
    N: Into<NamespaceIdent>,
    S: Into<SchemaIdent>,
    X: Into<String>,
{
    fn from((type_, ns, schema, name): (IdentType, N, Option<S>, X)) -> Self {
        Self::from((type_, Some(ns), schema, name))
    }
}

impl<N, S, X> From<(IdentType, Option<N>, Option<S>, X)> for IdentQuadruple
where
    N: Into<NamespaceIdent>,
    S: Into<SchemaIdent>,
    X: Into<String>,
{
    fn from((type_, ns, schema, name): (IdentType, Option<N>, Option<S>, X)) -> Self {
        let ns = ns.map(Into::into);
        let schema = schema.map(Into::into);
        let name = name.into();

        Self {
            ns,
            schema,
            name,
            type_,
        }
    }
}

/// Identifies a namespace by either it's id, it's known prefix or it's namespace.
///
/// Used in [`IdentQuadruple`].
#[derive(Debug, Clone)]
pub enum NamespaceIdent {
    /// Identifies the anonymous namespace.
    Anonymous,

    /// Use the actual id the namespace is identified with.
    Id(NamespaceId),

    /// Uses a namespace prefix to refer to a specific namespace in the schema.
    Prefix(NamespacePrefix),

    /// Uses the full namespace to refer to a specific namespace in the schema.
    Namespace(Namespace),
}

impl NamespaceIdent {
    /// Creates a new [`NamespaceIdent::Id`] instance from the passed `value`.
    #[inline]
    #[must_use]
    pub fn id(value: NamespaceId) -> Self {
        Self::Id(value)
    }

    /// Creates a new [`NamespaceIdent::Prefix`] instance from the passed `value`.
    #[inline]
    #[must_use]
    pub fn prefix<X>(value: X) -> Self
    where
        NamespacePrefix: From<X>,
    {
        Self::Prefix(NamespacePrefix::from(value))
    }

    /// Creates a new [`NamespaceIdent::Namespace`] instance from the passed `value`.
    #[inline]
    #[must_use]
    pub fn namespace<X>(value: X) -> Self
    where
        Namespace: From<X>,
    {
        Self::Namespace(Namespace::from(value))
    }
}

impl From<NamespaceId> for NamespaceIdent {
    #[inline]
    fn from(value: NamespaceId) -> Self {
        Self::Id(value)
    }
}

impl From<Namespace> for NamespaceIdent {
    #[inline]
    fn from(value: Namespace) -> Self {
        Self::Namespace(value)
    }
}

impl From<NamespacePrefix> for NamespaceIdent {
    #[inline]
    fn from(value: NamespacePrefix) -> Self {
        Self::Prefix(value)
    }
}

/// Identifies a schema by either it's id, it's name or it's location.
///
/// Used in [`IdentQuadruple`].
#[derive(Debug, Clone)]
pub enum SchemaIdent {
    /// Identify the schema by it's [`SchemaId`].
    Id(SchemaId),

    /// Identify the schema by it's name.
    Name(String),

    /// Identify the schema by it's location.
    Location(Url),
}

impl SchemaIdent {
    /// Creates a new [`SchemaIdent::Id`] instance from the passed `value`.
    #[inline]
    #[must_use]
    pub fn id(value: SchemaId) -> Self {
        Self::Id(value)
    }

    /// Creates a new [`SchemaIdent::Name`] instance from the passed `value`.
    #[inline]
    #[must_use]
    pub fn name<X>(value: X) -> Self
    where
        X: Into<String>,
    {
        Self::Name(value.into())
    }

    /// Creates a new [`SchemaIdent::Location`] instance from the passed `value`.
    #[inline]
    #[must_use]
    pub fn location<X>(value: X) -> Self
    where
        X: Into<Url>,
    {
        Self::Location(value.into())
    }
}

impl From<SchemaId> for SchemaIdent {
    #[inline]
    fn from(value: SchemaId) -> Self {
        Self::Id(value)
    }
}

impl From<String> for SchemaIdent {
    #[inline]
    fn from(value: String) -> Self {
        Self::Name(value)
    }
}

impl From<Url> for SchemaIdent {
    #[inline]
    fn from(value: Url) -> Self {
        Self::Location(value)
    }
}

impl Schemas {
    fn resolve_schema(&self, ns: NamespaceId, name: &str, type_: IdentType) -> Option<SchemaId> {
        let ns_info = self.get_namespace_info(&ns)?;

        for schema in &ns_info.schemas {
            let Some(schema_info) = self.get_schema(schema) else {
                continue;
            };

            for c in &schema_info.schema.content {
                match (type_, c) {
                    (IdentType::Element, SchemaContent::Element(x)) if matches!(&x.name, Some(n) if n == name) => {
                        return Some(*schema)
                    }
                    (IdentType::Type, SchemaContent::SimpleType(x)) if matches!(&x.name, Some(n) if n == name) => {
                        return Some(*schema)
                    }
                    (IdentType::Type, SchemaContent::ComplexType(x)) if matches!(&x.name, Some(n) if n == name) => {
                        return Some(*schema)
                    }
                    (_, _) => (),
                }
            }
        }

        None
    }
}