xsd_parser/config/
optimizer.rs

1use std::path::PathBuf;
2
3use bitflags::bitflags;
4
5/// Configuration for the type information optimizer.
6#[derive(Debug, Clone)]
7pub struct OptimizerConfig {
8    /// Additional flags to control the optimizer.
9    pub flags: OptimizerFlags,
10
11    /// Wether to enable the debug output and where to write it to.
12    pub debug_output: Option<PathBuf>,
13}
14
15impl Default for OptimizerConfig {
16    fn default() -> Self {
17        Self {
18            debug_output: None,
19            flags: OptimizerFlags::REMOVE_EMPTY_ENUM_VARIANTS
20                | OptimizerFlags::REMOVE_EMPTY_ENUMS
21                | OptimizerFlags::REMOVE_DUPLICATE_UNION_VARIANTS
22                | OptimizerFlags::REMOVE_EMPTY_UNIONS
23                | OptimizerFlags::USE_UNRESTRICTED_BASE_TYPE_SIMPLE,
24        }
25    }
26}
27
28bitflags! {
29    /// Flags to control the [`Optimizer`](crate::Optimizer).
30    #[derive(Debug, Clone)]
31    pub struct OptimizerFlags: u32 {
32        /// Whether to remove empty enum variants or not.
33        ///
34        /// See [`remove_empty_enum_variants`](crate::Optimizer::remove_empty_enum_variants) for details.
35        const REMOVE_EMPTY_ENUM_VARIANTS = 1 << 0;
36
37        /// Whether to remove empty enums or not.
38        ///
39        /// See [`remove_empty_enums`](crate::Optimizer::remove_empty_enums) for details.
40        const REMOVE_EMPTY_ENUMS = 1 << 1;
41
42        /// Whether to remove duplicate union variants or not.
43        ///
44        /// See [`remove_duplicate_union_variants`](crate::Optimizer::remove_duplicate_union_variants) for details.
45        const REMOVE_DUPLICATE_UNION_VARIANTS = 1 << 2;
46
47        /// Whether to remove empty unions or not.
48        ///
49        /// See [`remove_empty_unions`](crate::Optimizer::remove_empty_unions) for details.
50        const REMOVE_EMPTY_UNIONS = 1 << 3;
51
52        /// Whether to use the unrestricted base type of a type or not.
53        ///
54        /// See [`use_unrestricted_base_type`](crate::Optimizer::use_unrestricted_base_type) for details.
55        const USE_UNRESTRICTED_BASE_TYPE =
56            Self::USE_UNRESTRICTED_BASE_TYPE_COMPLEX.bits()
57            | Self::USE_UNRESTRICTED_BASE_TYPE_SIMPLE.bits()
58            | Self::USE_UNRESTRICTED_BASE_TYPE_ENUM.bits()
59            | Self::USE_UNRESTRICTED_BASE_TYPE_UNION.bits();
60
61        /// Like [`USE_UNRESTRICTED_BASE_TYPE`](Self::USE_UNRESTRICTED_BASE_TYPE) but for complex types only.
62        const USE_UNRESTRICTED_BASE_TYPE_COMPLEX = 1 << 4;
63
64        /// Like [`USE_UNRESTRICTED_BASE_TYPE`](Self::USE_UNRESTRICTED_BASE_TYPE) but for simple types only.
65        const USE_UNRESTRICTED_BASE_TYPE_SIMPLE = 1 << 5;
66
67        /// Like [`USE_UNRESTRICTED_BASE_TYPE`](Self::USE_UNRESTRICTED_BASE_TYPE) but for enum types only.
68        const USE_UNRESTRICTED_BASE_TYPE_ENUM = 1 << 6;
69
70        /// Like [`USE_UNRESTRICTED_BASE_TYPE`](Self::USE_UNRESTRICTED_BASE_TYPE) but for union types only.
71        const USE_UNRESTRICTED_BASE_TYPE_UNION = 1 << 7;
72
73        /// Whether to convert dynamic types to choices or not.
74        ///
75        /// See [`convert_dynamic_to_choice`](crate::Optimizer::convert_dynamic_to_choice) for details.
76        const CONVERT_DYNAMIC_TO_CHOICE = 1 << 8;
77
78        /// Whether to flatten the content of complex types or not.
79        ///
80        /// See [`flatten_complex_types`](crate::Optimizer::flatten_complex_types) for details.
81        const FLATTEN_COMPLEX_TYPES = 1 << 9;
82
83        /// Whether to flatten unions or not.
84        ///
85        /// See [`flatten_unions`](crate::Optimizer::flatten_unions) for details.
86        const FLATTEN_UNIONS = 1 << 10;
87
88        /// Whether to merge enumerations and unions or not.
89        ///
90        /// See [`merge_enum_unions`](crate::Optimizer::merge_enum_unions) for details.
91        const MERGE_ENUM_UNIONS = 1 << 11;
92
93        /// Whether to resolve type definitions or not.
94        ///
95        /// See [`resolve_typedefs`](crate::Optimizer::resolve_typedefs) for details.
96        const RESOLVE_TYPEDEFS = 1 << 12;
97
98        /// Whether to remove duplicate types or not.
99        ///
100        /// See [`remove_duplicates`](crate::Optimizer::remove_duplicates) for details.
101        const REMOVE_DUPLICATES = 1 << 13;
102
103        /// Group that contains all necessary optimization that should be applied
104        /// if code with [`serde`] support should be rendered.
105        const SERDE =  Self::FLATTEN_COMPLEX_TYPES.bits()
106            | Self::FLATTEN_UNIONS.bits()
107            | Self::MERGE_ENUM_UNIONS.bits();
108
109        /// Wether to merge the cardinality of a complex choice type or not.
110        ///
111        /// See [`merge_choice_cardinalities`](crate::Optimizer::merge_choice_cardinalities) for details.
112        const MERGE_CHOICE_CARDINALITIES = 1 << 14;
113
114        /// Wether to simplify complex mixed types or not.
115        ///
116        /// See [`simplify_mixed_types`](crate::Optimizer::simplify_mixed_types) for details.
117        const SIMPLIFY_MIXED_TYPES = 1 << 15;
118    }
119}