xsd_parser/types/info/
reference.rs

1//! Contains the [`ReferenceInfo`] type information and all related types.
2
3use std::hash::{Hash, Hasher};
4
5use crate::schema::{MaxOccurs, MinOccurs};
6use crate::types::{Ident, TypeEq, Types};
7
8/// Type information that defines a reference to another type.
9#[derive(Debug, Clone)]
10pub struct ReferenceInfo {
11    /// Type that is referenced.
12    pub type_: Ident,
13
14    /// Minimum occurrence of the referenced type.
15    pub min_occurs: MinOccurs,
16
17    /// Maximum occurrence of the referenced type.
18    pub max_occurs: MaxOccurs,
19}
20
21impl ReferenceInfo {
22    /// Create a new [`ReferenceInfo`] instance from the passed `type_`.
23    #[must_use]
24    pub fn new<T>(type_: T) -> Self
25    where
26        T: Into<Ident>,
27    {
28        Self {
29            type_: type_.into(),
30            min_occurs: 1,
31            max_occurs: MaxOccurs::Bounded(1),
32        }
33    }
34
35    /// Returns `true` if this is a reference references a single type, `false` otherwise.
36    ///
37    /// This means that it is more or less just a type definition or renaming of an
38    /// existing type.
39    #[must_use]
40    pub fn is_single(&self) -> bool {
41        self.min_occurs == 1 && self.max_occurs == MaxOccurs::Bounded(1)
42    }
43
44    /// Sets the minimum occurrence of the referenced type.
45    #[must_use]
46    pub fn min_occurs(mut self, min: MinOccurs) -> Self {
47        self.min_occurs = min;
48
49        self
50    }
51
52    /// Sets the maximum occurrence of the referenced type.
53    #[must_use]
54    pub fn max_occurs(mut self, max: MaxOccurs) -> Self {
55        self.max_occurs = max;
56
57        self
58    }
59}
60
61impl TypeEq for ReferenceInfo {
62    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
63        let Self {
64            type_,
65            min_occurs,
66            max_occurs,
67        } = self;
68
69        type_.type_hash(hasher, types);
70        min_occurs.hash(hasher);
71        max_occurs.hash(hasher);
72    }
73
74    fn type_eq(&self, other: &Self, types: &Types) -> bool {
75        let Self {
76            type_,
77            min_occurs,
78            max_occurs,
79        } = self;
80
81        type_.type_eq(&other.type_, types)
82            && min_occurs.eq(&other.min_occurs)
83            && max_occurs.eq(&other.max_occurs)
84    }
85}