Skip to main content

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