xsd_parser/optimizer/unrestricted_base.rs
1use crate::types::{ReferenceInfo, TypeVariant};
2
3use super::{get_bases, Optimizer};
4
5impl Optimizer {
6 /// This will use the unrestricted base type instead the restricted version
7 /// when ever possible.
8 ///
9 /// This is useful if you want to reduce the amount of different types,
10 /// because the base type can store the same data than the restricted one.
11 /// However this is only useful if you want to deserialize the type only.
12 /// Using this feature for serializing types will cause problems because the
13 /// type information is dropped during deserialization.
14 ///
15 /// # Examples
16 ///
17 /// Consider the following XML schema.
18 /// ```xml
19 #[doc = include_str!("../../tests/optimizer/complex_restricted.xsd")]
20 /// ```
21 ///
22 /// Without this optimization this will result in the following code:
23 /// ```rust
24 #[doc = include_str!("../../tests/optimizer/expected0/use_unrestricted_base_type.rs")]
25 /// ```
26 ///
27 /// With this optimization the following code is generated:
28 /// ```rust
29 #[doc = include_str!("../../tests/optimizer/expected1/use_unrestricted_base_type.rs")]
30 /// ```
31 pub fn use_unrestricted_base_type(mut self) -> Self {
32 tracing::debug!("use_unrestricted_base_type");
33
34 let bases = get_bases!(self);
35
36 for (ident, type_) in &mut *self.types {
37 match &type_.variant {
38 TypeVariant::ComplexType(_)
39 | TypeVariant::Enumeration(_)
40 | TypeVariant::Union(_) => {
41 let base = bases.get_unrestricted(ident).clone();
42 if *ident != base {
43 self.typedefs = None;
44 type_.variant = TypeVariant::Reference(ReferenceInfo::new(base));
45 }
46 }
47 _ => (),
48 }
49 }
50
51 self
52 }
53}