xsd_parser/models/meta/
enumeration.rs1use 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#[derive(Default, Debug, Clone)]
12pub struct EnumerationMeta {
13 pub base: Base,
15
16 pub variants: EnumerationMetaVariants,
18
19 pub constrains: Constrains,
21}
22
23#[derive(Debug, Clone)]
25pub struct EnumerationMetaVariant {
26 pub ident: Ident,
28
29 pub use_: Use,
31
32 pub type_: Option<Ident>,
34
35 pub display_name: Option<String>,
37
38 pub documentation: Vec<String>,
40}
41
42#[derive(Default, Debug, Clone)]
44pub struct EnumerationMetaVariants(pub Vec<EnumerationMetaVariant>);
45
46impl 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
74impl EnumerationMetaVariant {
77 #[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 #[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
132impl 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}