xsd_parser/models/meta/
reference.rs1use std::hash::{Hash, Hasher};
4
5use crate::models::{
6 schema::{MaxOccurs, MinOccurs},
7 Ident,
8};
9
10use super::{MetaTypes, TypeEq};
11
12#[derive(Debug, Clone)]
14pub struct ReferenceMeta {
15 pub type_: Ident,
17
18 pub nillable: bool,
20
21 pub min_occurs: MinOccurs,
23
24 pub max_occurs: MaxOccurs,
26}
27
28impl ReferenceMeta {
29 #[must_use]
31 pub fn new<T>(type_: T) -> Self
32 where
33 T: Into<Ident>,
34 {
35 Self {
36 type_: type_.into(),
37 nillable: false,
38 min_occurs: 1,
39 max_occurs: MaxOccurs::Bounded(1),
40 }
41 }
42
43 #[must_use]
47 pub fn is_single(&self) -> bool {
48 self.min_occurs == 1 && self.max_occurs == MaxOccurs::Bounded(1)
49 }
50
51 #[must_use]
56 pub fn is_simple(&self) -> bool {
57 self.is_single() && !self.nillable
58 }
59
60 #[must_use]
62 pub fn min_occurs(mut self, min: MinOccurs) -> Self {
63 self.min_occurs = min;
64
65 self
66 }
67
68 #[must_use]
70 pub fn max_occurs(mut self, max: MaxOccurs) -> Self {
71 self.max_occurs = max;
72
73 self
74 }
75
76 #[must_use]
80 pub fn is_emptiable(&self, types: &MetaTypes) -> bool {
81 if self.min_occurs == 0 {
82 return true;
83 }
84
85 types
86 .items
87 .get(&self.type_)
88 .is_none_or(|ty| ty.is_emptiable(types))
89 }
90
91 #[must_use]
95 pub fn is_mixed(&self, types: &MetaTypes) -> bool {
96 types
97 .items
98 .get(&self.type_)
99 .is_some_and(|ty| ty.is_mixed(types))
100 }
101}
102
103impl TypeEq for ReferenceMeta {
104 fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
105 let Self {
106 type_,
107 nillable,
108 min_occurs,
109 max_occurs,
110 } = self;
111
112 type_.type_hash(hasher, types);
113 nillable.hash(hasher);
114 min_occurs.hash(hasher);
115 max_occurs.hash(hasher);
116 }
117
118 fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
119 let Self {
120 type_,
121 nillable,
122 min_occurs,
123 max_occurs,
124 } = self;
125
126 type_.type_eq(&other.type_, types)
127 && nillable.eq(&other.nillable)
128 && min_occurs.eq(&other.min_occurs)
129 && max_occurs.eq(&other.max_occurs)
130 }
131}