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
//! Contains the [`ComplexMeta`] type information and all related types.
use std::hash::{Hash, Hasher};
use crate::models::{
meta::{ElementMetaVariant, ElementMode},
schema::{MaxOccurs, MinOccurs},
TypeIdent,
};
use super::{AttributesMeta, Base, ElementsMeta, MetaType, MetaTypeVariant, MetaTypes, TypeEq};
/// Represents a group of elements.
///
/// This is usually a `xs:all`, `xs:choice` or `xs:sequence`.
#[derive(Default, Debug, Clone)]
pub struct GroupMeta {
/// Wether the content of this type is mixed (contains also text) or not.
pub is_mixed: bool,
/// List of elements defined in this group.
pub elements: ElementsMeta,
}
/// Type information that contains data about a complex type.
#[derive(Debug, Clone)]
pub struct ComplexMeta {
/// Base type of the complex type.
pub base: Base,
/// Content type information of the complex type that contains the actual
/// information about the elements that are defined for this type.
pub content: Option<TypeIdent>,
/// Minimum occurrence of this complex types content type.
pub min_occurs: MinOccurs,
/// Maximum occurrence of this complex types content type.
pub max_occurs: MaxOccurs,
/// Whether the type is dynamic or not.
pub is_dynamic: bool,
/// Wether the content of this type is mixed (contains also text) or not.
pub is_mixed: bool,
/// List of attributes defined for this complex type.
pub attributes: AttributesMeta,
}
/* GroupMeta */
impl GroupMeta {
/// Returns `true` if this type is emptiable, `false` otherwise.
///
/// Emptiable means that the type may not have any element, but a simple
/// text value.
#[must_use]
pub fn is_emptiable(&self, types: &MetaTypes) -> bool {
for element in &*self.elements {
if element.min_occurs == 0 {
continue;
}
match &element.variant {
ElementMetaVariant::Text => (),
ElementMetaVariant::Any { .. }
| ElementMetaVariant::Type {
mode: ElementMode::Element,
..
} => return false,
ElementMetaVariant::Type { type_, .. } => {
if let Some(ty) = types.items.get(type_) {
if !ty.is_emptiable(types) {
return false;
}
}
}
}
}
true
}
}
impl TypeEq for GroupMeta {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
let Self { is_mixed, elements } = self;
is_mixed.hash(hasher);
elements.type_hash(hasher, types);
}
fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
let Self { is_mixed, elements } = self;
is_mixed.eq(&other.is_mixed) && elements.type_eq(&other.elements, types)
}
}
/* ComplexMeta */
impl ComplexMeta {
/// Get the meta type information of the content fo this complex type.
#[must_use]
pub fn content_meta<'a>(&'a self, types: &'a MetaTypes) -> Option<&'a MetaType> {
self.content
.as_ref()
.and_then(|ident| types.get_resolved_type(ident))
}
/// Returns `true` if the content of this complex type information
/// is a [`MetaTypeVariant::All`], `false` otherwise.
#[must_use]
pub fn has_complex_all_content(&self, types: &MetaTypes) -> bool {
matches!(
self.content_meta(types).map(|ty| &ty.variant),
Some(MetaTypeVariant::All(_))
)
}
/// Returns `true` if the content of this complex type information
/// is a [`MetaTypeVariant::Choice`], `false` otherwise.
#[must_use]
pub fn has_complex_choice_content(&self, types: &MetaTypes) -> bool {
matches!(
self.content_meta(types).map(|ty| &ty.variant),
Some(MetaTypeVariant::Choice(_))
)
}
/// Returns `true` if the content of this complex type information
/// is a [`MetaTypeVariant::Sequence`], `false` otherwise.
#[must_use]
pub fn has_complex_sequence_content(&self, types: &MetaTypes) -> bool {
matches!(
self.content_meta(types).map(|ty| &ty.variant),
Some(MetaTypeVariant::Sequence(_))
)
}
/// Returns `true` if the content of this complex type information
/// is a [`MetaTypeVariant::All`], [`MetaTypeVariant::Choice`] or [`MetaTypeVariant::Sequence`],
/// `false` otherwise.
#[must_use]
pub fn has_complex_content(&self, types: &MetaTypes) -> bool {
matches!(
self.content_meta(types).map(|ty| &ty.variant),
Some(
MetaTypeVariant::All(_) | MetaTypeVariant::Choice(_) | MetaTypeVariant::Sequence(_)
)
)
}
/// Returns `true` if the content of this complex type information
/// is a [`MetaTypeVariant::BuildIn`], [`MetaTypeVariant::Union`] or [`MetaTypeVariant::Enumeration`],
/// `false` otherwise.
#[must_use]
pub fn has_simple_content(&self, types: &MetaTypes) -> bool {
matches!(
self.content_meta(types).map(|ty| &ty.variant),
Some(
MetaTypeVariant::Reference(_)
| MetaTypeVariant::BuildIn(_)
| MetaTypeVariant::Union(_)
| MetaTypeVariant::Enumeration(_)
)
)
}
/// Returns `true` if this type is emptiable, `false` otherwise.
///
/// Emptiable means that the type may not have any element.
#[must_use]
pub fn is_emptiable(&self, types: &MetaTypes) -> bool {
if self.min_occurs == 0 {
return true;
}
self.content
.as_ref()
.and_then(|ident| types.items.get(ident))
.is_none_or(|ty| ty.is_emptiable(types))
}
}
impl Default for ComplexMeta {
fn default() -> Self {
Self {
base: Base::None,
content: None,
min_occurs: 1,
max_occurs: MaxOccurs::Bounded(1),
is_dynamic: false,
is_mixed: false,
attributes: AttributesMeta::default(),
}
}
}
impl TypeEq for ComplexMeta {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
let Self {
base,
content,
min_occurs,
max_occurs,
is_dynamic,
is_mixed: mixed_content,
attributes,
} = self;
base.type_hash(hasher, types);
content.type_hash(hasher, types);
min_occurs.hash(hasher);
max_occurs.hash(hasher);
is_dynamic.hash(hasher);
mixed_content.hash(hasher);
attributes.type_hash(hasher, types);
}
fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
let Self {
base,
content,
min_occurs,
max_occurs,
is_dynamic,
is_mixed: mixed_content,
attributes,
} = self;
base.type_eq(&other.base, types)
&& content.type_eq(&other.content, types)
&& min_occurs.eq(&other.min_occurs)
&& max_occurs.eq(&other.max_occurs)
&& is_dynamic.eq(&other.is_dynamic)
&& mixed_content.eq(&other.is_mixed)
&& attributes.type_eq(&other.attributes, types)
}
}