xsd_parser/models/meta/
enumeration.rs

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