xsd_parser/models/meta/
enumeration.rs1use 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#[derive(Default, Debug, Clone)]
14pub struct EnumerationMeta {
15 pub base: Base,
17
18 pub variants: EnumerationMetaVariants,
20
21 pub constrains: Constrains,
23}
24
25#[derive(Debug, Clone)]
27pub struct EnumerationMetaVariant {
28 pub ident: EnumerationIdent,
30
31 pub use_: Use,
33
34 pub type_: Option<TypeIdent>,
36
37 pub display_name: Option<String>,
39
40 pub documentation: Vec<String>,
42
43 pub namespaces: Option<NamespacesShared<'static>>,
45}
46
47#[derive(Default, Debug, Clone)]
49pub struct EnumerationMetaVariants(pub Vec<EnumerationMetaVariant>);
50
51impl 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
79impl EnumerationMetaVariant {
82 #[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 #[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
142impl 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}