xsd_parser/models/meta/
union.rs

1//! Contains the [`UnionMeta`] type information and all related types.
2
3use std::hash::{Hash, Hasher};
4use std::ops::{Deref, DerefMut};
5
6use crate::models::Ident;
7
8use super::{Base, Constrains, MetaTypes, TypeEq};
9
10/// Type information that defines a union type.
11#[derive(Default, Debug, Clone)]
12pub struct UnionMeta {
13    /// Base type of the union type.
14    pub base: Base,
15
16    /// Types that are unified in this union type.
17    pub types: UnionMetaTypes,
18
19    /// Constraining facets defined for this type.
20    pub constrains: Constrains,
21}
22
23/// Type information that represents one type unified by a [`UnionMeta`].
24#[derive(Debug, Clone)]
25pub struct UnionMetaType {
26    /// Target type of this type variant.
27    pub type_: Ident,
28
29    /// Name of the variant to use inside the generated code.
30    pub display_name: Option<String>,
31}
32
33/// Type information that represents a list of [`UnionMetaType`] instances.
34#[derive(Default, Debug, Clone)]
35pub struct UnionMetaTypes(pub Vec<UnionMetaType>);
36
37/* UnionMeta */
38
39impl 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
65/* UnionMetaType */
66
67impl UnionMetaType {
68    /// Create a new [`UnionMetaType`] from the passed `type_`.
69    #[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
99/* UnionMetaTypes */
100
101impl 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}