xsd_parser/models/meta/
union.rs1use std::hash::{Hash, Hasher};
4use std::ops::{Deref, DerefMut};
5
6use crate::models::Ident;
7
8use super::{Base, Constrains, MetaTypes, TypeEq};
9
10#[derive(Default, Debug, Clone)]
12pub struct UnionMeta {
13 pub base: Base,
15
16 pub types: UnionMetaTypes,
18
19 pub constrains: Constrains,
21}
22
23#[derive(Debug, Clone)]
25pub struct UnionMetaType {
26 pub type_: Ident,
28
29 pub display_name: Option<String>,
31}
32
33#[derive(Default, Debug, Clone)]
35pub struct UnionMetaTypes(pub Vec<UnionMetaType>);
36
37impl TypeEq for UnionMeta {
40 fn type_hash<H: Hasher>(&self, hasher: &mut H, ctx: &MetaTypes) {
41 let Self {
42 base,
43 types,
44 constrains,
45 } = self;
46
47 base.type_hash(hasher, ctx);
48 types.type_hash(hasher, ctx);
49 constrains.hash(hasher);
50 }
51
52 fn type_eq(&self, other: &Self, ctx: &MetaTypes) -> bool {
53 let Self {
54 base,
55 types,
56 constrains,
57 } = self;
58
59 base.type_eq(&other.base, ctx)
60 && types.type_eq(&other.types, ctx)
61 && constrains.eq(&other.constrains)
62 }
63}
64
65impl UnionMetaType {
68 #[must_use]
70 pub fn new(type_: Ident) -> Self {
71 Self {
72 type_,
73 display_name: None,
74 }
75 }
76}
77
78impl TypeEq for UnionMetaType {
79 fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
80 let Self {
81 type_,
82 display_name,
83 } = self;
84
85 type_.type_hash(hasher, types);
86 display_name.hash(hasher);
87 }
88
89 fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
90 let Self {
91 type_,
92 display_name,
93 } = self;
94
95 type_.type_eq(&other.type_, types) && display_name.eq(&other.display_name)
96 }
97}
98
99impl Deref for UnionMetaTypes {
102 type Target = Vec<UnionMetaType>;
103
104 fn deref(&self) -> &Self::Target {
105 &self.0
106 }
107}
108
109impl DerefMut for UnionMetaTypes {
110 fn deref_mut(&mut self) -> &mut Self::Target {
111 &mut self.0
112 }
113}
114
115impl TypeEq for UnionMetaTypes {
116 fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
117 TypeEq::type_hash_slice(&self.0, hasher, types);
118 }
119
120 fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
121 TypeEq::type_eq_iter(self.0.iter(), other.0.iter(), types)
122 }
123}