xsd_parser/models/data/
occurs.rs

1use crate::models::schema::{MaxOccurs, MinOccurs};
2
3/// Defines the occurrence (how often the field is used) of a field in a specific type.
4#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
5pub enum Occurs {
6    /// The field is not used at all.
7    #[default]
8    None,
9
10    /// The field is used exactly one time.
11    Single,
12
13    /// The field is used optional (zero or exactly one time).
14    Optional,
15
16    /// The field is used as a dynamic list.
17    DynamicList,
18
19    /// The field is used as a list with a fixed size.
20    StaticList(usize),
21}
22
23impl Occurs {
24    /// Create the [`Occurs`] value from the [`MinOccurs`] and [`MaxOccurs`] from
25    /// the XSD schema.
26    #[must_use]
27    pub fn from_occurs(min: MinOccurs, max: MaxOccurs) -> Self {
28        match (min, max) {
29            (0, MaxOccurs::Bounded(0)) => Self::None,
30            (1, MaxOccurs::Bounded(1)) => Self::Single,
31            (0, MaxOccurs::Bounded(1)) => Self::Optional,
32            (a, MaxOccurs::Bounded(b)) if a == b => Self::StaticList(a),
33            (_, _) => Self::DynamicList,
34        }
35    }
36
37    /// Returns `true` if not `Occurs::None`
38    #[must_use]
39    pub fn is_some(&self) -> bool {
40        *self != Self::None
41    }
42
43    /// Returns `true` if [`make_type`](Self::make_type) would generate a static type
44    /// (a type without memory indirection), like `T`, `Option<T>` or `[T; SIZE]`.
45    #[must_use]
46    pub fn is_direct(&self) -> bool {
47        matches!(self, Self::Single | Self::Optional | Self::StaticList(_))
48    }
49}