xsd_parser/types/info/
mod.rs

1//! Contains different type information types.
2
3pub mod attribute;
4pub mod complex;
5pub mod dynamic;
6pub mod element;
7pub mod enumeration;
8pub mod reference;
9pub mod union;
10
11use std::fmt::{Display, Formatter, Result as FmtResult};
12use std::hash::Hasher;
13
14pub use attribute::{AttributeInfo, AttributesInfo};
15pub use complex::{AnyAttributeInfo, AnyInfo, ComplexInfo, GroupInfo};
16pub use dynamic::DynamicInfo;
17pub use element::{ElementInfo, ElementMode, ElementsInfo};
18pub use enumeration::{EnumerationInfo, VariantInfo};
19pub use reference::ReferenceInfo;
20pub use union::{UnionInfo, UnionTypeInfo, UnionTypesInfo};
21
22use crate::schema::xs::Use;
23
24use super::{Ident, TypeEq, Types};
25
26/// Describes the base type information of a specific type information.
27#[derive(Default, Debug, Clone, Eq, PartialEq)]
28pub enum Base {
29    /// The type information has no base type.
30    #[default]
31    None,
32
33    /// The type information extends the provided base type.
34    Extension(Ident),
35
36    /// The type information restricts the provided base type.
37    Restriction(Ident),
38}
39
40impl Base {
41    /// Get the identifier of the base type if it is available.
42    #[must_use]
43    pub fn as_ident(&self) -> Option<&Ident> {
44        match self {
45            Self::None => None,
46            Self::Extension(x) => Some(x),
47            Self::Restriction(x) => Some(x),
48        }
49    }
50
51    /// Extracts the identifier of the base type if it is available.
52    #[must_use]
53    pub fn into_ident(self) -> Option<Ident> {
54        match self {
55            Self::None => None,
56            Self::Extension(x) => Some(x),
57            Self::Restriction(x) => Some(x),
58        }
59    }
60}
61
62impl Display for Base {
63    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
64        match self {
65            Self::None => write!(f, "None"),
66            Self::Extension(x) => write!(f, "Extension({x})"),
67            Self::Restriction(x) => write!(f, "Restriction({x})"),
68        }
69    }
70}
71
72impl TypeEq for Base {
73    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
74        match self {
75            Self::None => hasher.write_u8(0),
76            Self::Extension(x) => {
77                hasher.write_u8(1);
78                x.type_hash(hasher, types);
79            }
80            Self::Restriction(x) => {
81                hasher.write_u8(2);
82                x.type_hash(hasher, types);
83            }
84        }
85    }
86
87    fn type_eq(&self, other: &Self, types: &Types) -> bool {
88        #[allow(clippy::enum_glob_use)]
89        use Base::*;
90
91        match (self, other) {
92            (None, None) => true,
93            (Extension(x), Extension(y)) => x.type_eq(y, types),
94            (Restriction(x), Restriction(y)) => x.type_eq(y, types),
95            (_, _) => false,
96        }
97    }
98}
99
100fn use_hash<H: Hasher>(use_: &Use, hasher: &mut H) {
101    match use_ {
102        Use::Prohibited => hasher.write_u8(0),
103        Use::Optional => hasher.write_u8(1),
104        Use::Required => hasher.write_u8(2),
105    }
106}