xsd_parser/types/info/
enumeration.rs

1//! Contains the [`EnumerationInfo`] type information and all related types.
2
3use std::hash::{Hash, Hasher};
4use std::ops::{Deref, DerefMut};
5
6use crate::schema::xs::Use;
7use crate::types::{Ident, TypeEq, Types};
8
9use super::{use_hash, Base};
10
11/// Type information that defines an enumeration type.
12#[derive(Default, Debug, Clone)]
13pub struct EnumerationInfo {
14    /// Base type of this enumeration type.
15    pub base: Base,
16
17    /// Variants defined for this enumeration.
18    pub variants: VariantsInfo,
19}
20
21/// Type information that defines variants of an [`EnumerationInfo`].
22#[derive(Debug, Clone)]
23pub struct VariantInfo {
24    /// Identifier of the variant.
25    pub ident: Ident,
26
27    /// Use of the variant.
28    pub use_: Use,
29
30    /// Type of the variant.
31    pub type_: Option<Ident>,
32
33    /// Name of the variant to use inside the generated code.
34    pub display_name: Option<String>,
35}
36
37/// Type information that represents a list of [`VariantInfo`] instances.
38#[derive(Default, Debug, Clone)]
39pub struct VariantsInfo(pub Vec<VariantInfo>);
40
41/* EnumerationInfo */
42
43impl TypeEq for EnumerationInfo {
44    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
45        let Self { base, variants } = self;
46
47        base.type_hash(hasher, types);
48        variants.type_hash(hasher, types);
49    }
50
51    fn type_eq(&self, other: &Self, types: &Types) -> bool {
52        let Self { base, variants } = self;
53
54        base.type_eq(&other.base, types) && variants.type_eq(&other.variants, types)
55    }
56}
57
58/* VariantInfo */
59
60impl VariantInfo {
61    /// Create a new [`VariantInfo`] instance from the passed `name`.
62    #[must_use]
63    pub fn new(ident: Ident) -> Self {
64        Self {
65            ident,
66            use_: Use::Optional,
67            type_: None,
68            display_name: None,
69        }
70    }
71
72    /// Set the type of this variant information.
73    #[must_use]
74    pub fn with_type(mut self, type_: Option<Ident>) -> Self {
75        self.type_ = type_;
76
77        self
78    }
79}
80
81impl TypeEq for VariantInfo {
82    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
83        let Self {
84            ident,
85            use_,
86            type_,
87            display_name,
88        } = self;
89
90        ident.hash(hasher);
91        use_hash(use_, hasher);
92        type_.type_hash(hasher, types);
93        display_name.hash(hasher);
94    }
95
96    fn type_eq(&self, other: &Self, types: &Types) -> bool {
97        let Self {
98            ident,
99            use_,
100            type_,
101            display_name,
102        } = self;
103
104        ident.eq(&other.ident)
105            && use_.eq(&other.use_)
106            && type_.type_eq(&other.type_, types)
107            && display_name.eq(&other.display_name)
108    }
109}
110
111/* VariantsInfo */
112
113impl Deref for VariantsInfo {
114    type Target = Vec<VariantInfo>;
115
116    fn deref(&self) -> &Self::Target {
117        &self.0
118    }
119}
120
121impl DerefMut for VariantsInfo {
122    fn deref_mut(&mut self) -> &mut Self::Target {
123        &mut self.0
124    }
125}
126
127impl TypeEq for VariantsInfo {
128    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
129        TypeEq::type_hash_slice(&self.0, hasher, types);
130    }
131
132    fn type_eq(&self, other: &Self, types: &Types) -> bool {
133        TypeEq::type_eq_iter(self.0.iter(), other.0.iter(), types)
134    }
135}