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
use crate::models::{
meta::{MetaTypeVariant, ReferenceMeta},
Name,
};
use super::Optimizer;
impl Optimizer {
/// This will remove any enum variant that has an empty string as name.
///
/// # Examples
///
/// Consider the following XML schema:
/// ```xml
#[doc = include_str!("../../../tests/optimizer/enum_empty_variant.xsd")]
/// ```
///
/// Without this optimization this will result in the following code:
/// ```rust
#[doc = include_str!("../../../tests/optimizer/expected0/remove_empty_enum_variants.rs")]
/// ```
///
/// With this optimization the following code is generated:
/// ```rust
#[doc = include_str!("../../../tests/optimizer/expected1/remove_empty_enum_variants.rs")]
/// ```
pub fn remove_empty_enum_variants(mut self) -> Self {
tracing::debug!("remove_empty_enum_variants");
for type_ in self.types.items.values_mut() {
if let MetaTypeVariant::Enumeration(x) = &mut type_.variant {
x.variants
.retain(|x| !matches!(&x.ident.name, Name::Named(x) if x.is_empty()));
}
}
self
}
/// This will replace the enum with a type reference to the enums base type
/// if the enum does not have any variant.
///
/// This optimization is usually used in combination with
/// [`remove_empty_enum_variants`](Self::remove_empty_enum_variants).
///
/// # Examples
///
/// Consider the following XML schema:
/// ```xml
#[doc = include_str!("../../../tests/optimizer/enum_empty.xsd")]
/// ```
///
/// Without this optimization this will result in the following code:
/// ```rust
#[doc = include_str!("../../../tests/optimizer/expected0/remove_empty_enums.rs")]
/// ```
///
/// With this optimization (and the [`remove_empty_enum_variants`](Self::remove_empty_enum_variants))
/// the following code is generated:
/// ```rust
#[doc = include_str!("../../../tests/optimizer/expected1/remove_empty_enums.rs")]
/// ```
pub fn remove_empty_enums(mut self) -> Self {
tracing::debug!("remove_empty_enums");
for type_ in self.types.items.values_mut() {
if let MetaTypeVariant::Enumeration(x) = &mut type_.variant {
if x.variants.is_empty() {
if let Some(base) = x.base.as_ident() {
self.typedefs = None;
type_.variant =
MetaTypeVariant::Reference(ReferenceMeta::new(base.clone()));
}
}
}
}
self
}
}