xsd_parser/pipeline/optimizer/
unrestricted_base.rs

1use bitflags::bitflags;
2
3use crate::models::meta::{MetaTypeVariant, ReferenceMeta};
4
5use super::{get_bases, Optimizer};
6
7bitflags! {
8    /// Flags to control the [`Optimizer::use_unrestricted_base_type`].
9    #[derive(Debug, Clone, Copy)]
10    pub struct UnrestrictedBaseFlags: u32 {
11        /// Use the unrestricted base type for complex types.
12        const COMPLEX = 1 << 0;
13
14        /// Use the unrestricted base type for simple types.
15        const SIMPLE = 1 << 1;
16
17        /// Use the unrestricted base type for enum types.
18        const ENUM = 1 << 2;
19
20        /// Use the unrestricted base type for union types.
21        const UNION = 1 << 3;
22    }
23}
24
25impl Optimizer {
26    /// This will use the unrestricted base type instead the restricted version
27    /// when ever possible.
28    ///
29    /// This is useful if you want to reduce the amount of different types,
30    /// because the base type can store the same data than the restricted one.
31    /// However this is only useful if you want to deserialize the type only.
32    /// Using this feature for serializing types will cause problems because the
33    /// type information is dropped during deserialization.
34    ///
35    /// # Examples
36    ///
37    /// Consider the following XML schema.
38    /// ```xml
39    #[doc = include_str!("../../../tests/optimizer/complex_restricted.xsd")]
40    /// ```
41    ///
42    /// Without this optimization this will result in the following code:
43    /// ```rust
44    #[doc = include_str!("../../../tests/optimizer/expected0/use_unrestricted_base_type.rs")]
45    /// ```
46    ///
47    /// With this optimization the following code is generated:
48    /// ```rust
49    #[doc = include_str!("../../../tests/optimizer/expected1/use_unrestricted_base_type.rs")]
50    /// ```
51    pub fn use_unrestricted_base_type(mut self, flags: UnrestrictedBaseFlags) -> Self {
52        tracing::debug!("use_unrestricted_base_type");
53
54        let bases = get_bases!(self);
55
56        for (ident, type_) in &mut self.types.items {
57            let replace = match &type_.variant {
58                MetaTypeVariant::ComplexType(_) => flags.intersects(UnrestrictedBaseFlags::COMPLEX),
59                MetaTypeVariant::SimpleType(x) => {
60                    !x.is_list && flags.intersects(UnrestrictedBaseFlags::SIMPLE)
61                }
62                MetaTypeVariant::Enumeration(_) => flags.intersects(UnrestrictedBaseFlags::ENUM),
63                MetaTypeVariant::Union(_) => flags.intersects(UnrestrictedBaseFlags::UNION),
64                _ => false,
65            };
66
67            if replace {
68                let base = bases.get_unrestricted(ident).clone();
69                if *ident != base {
70                    self.typedefs = None;
71                    type_.variant = MetaTypeVariant::Reference(ReferenceMeta::new(base));
72                }
73            }
74        }
75
76        self
77    }
78}