xsd_parser/types/info/
element.rs

1//! Contains the [`ElementInfo`] type information and all related types.
2
3use std::hash::{Hash, Hasher};
4use std::ops::{Deref, DerefMut};
5
6use crate::schema::{MaxOccurs, MinOccurs};
7use crate::types::{Ident, TypeEq, Types};
8
9/// Type information that contains data about a element.
10#[derive(Debug, Clone)]
11pub struct ElementInfo {
12    /// Identifier of the element.
13    pub ident: Ident,
14
15    /// Type of the element.
16    pub type_: Ident,
17
18    /// Minimum occurrence of the field.
19    pub min_occurs: MinOccurs,
20
21    /// Maximum occurrence of the field.
22    pub max_occurs: MaxOccurs,
23
24    /// Mode of the element.
25    pub element_mode: ElementMode,
26
27    /// Name of the element to use inside the generated code.
28    pub display_name: Option<String>,
29}
30
31/// Type information that represents a list of [`ElementInfo`] instances.
32#[derive(Default, Debug, Clone)]
33pub struct ElementsInfo(pub Vec<ElementInfo>);
34
35/// Defines the type of an [`ElementInfo`]
36#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
37pub enum ElementMode {
38    /// Represents an actual XML element.
39    Element,
40
41    /// Represents another group of elements.
42    Group,
43}
44
45/* ElementInfo */
46
47impl ElementInfo {
48    /// Create a new [`ElementInfo`] instance from the passed `name`, `type_`
49    /// and `element_mode`.
50    #[must_use]
51    pub fn new(ident: Ident, type_: Ident, element_mode: ElementMode) -> Self {
52        Self {
53            ident,
54            type_,
55            element_mode,
56            min_occurs: 1,
57            max_occurs: MaxOccurs::Bounded(1),
58            display_name: None,
59        }
60    }
61}
62
63impl TypeEq for ElementInfo {
64    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
65        let Self {
66            ident,
67            type_,
68            element_mode,
69            min_occurs,
70            max_occurs,
71            display_name,
72        } = self;
73
74        ident.hash(hasher);
75        type_.type_hash(hasher, types);
76        element_mode.hash(hasher);
77        min_occurs.hash(hasher);
78        max_occurs.hash(hasher);
79        display_name.hash(hasher);
80    }
81
82    fn type_eq(&self, other: &Self, types: &Types) -> bool {
83        let Self {
84            ident,
85            type_,
86            element_mode,
87            min_occurs,
88            max_occurs,
89            display_name,
90        } = self;
91
92        ident.eq(&other.ident)
93            && type_.type_eq(&other.type_, types)
94            && element_mode.eq(&other.element_mode)
95            && min_occurs.eq(&other.min_occurs)
96            && max_occurs.eq(&other.max_occurs)
97            && display_name.eq(&other.display_name)
98    }
99}
100
101/* ElementsInfo */
102
103impl Deref for ElementsInfo {
104    type Target = Vec<ElementInfo>;
105
106    fn deref(&self) -> &Self::Target {
107        &self.0
108    }
109}
110
111impl DerefMut for ElementsInfo {
112    fn deref_mut(&mut self) -> &mut Self::Target {
113        &mut self.0
114    }
115}
116
117impl TypeEq for ElementsInfo {
118    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
119        TypeEq::type_hash_slice(&self.0, hasher, types);
120    }
121
122    fn type_eq(&self, other: &Self, types: &Types) -> bool {
123        TypeEq::type_eq_iter(self.0.iter(), other.0.iter(), types)
124    }
125}