xsd_parser/models/meta/
base.rs

1//! Contains the [`Base`] type information and all related types.
2
3use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
4use std::hash::Hasher;
5
6use crate::models::Ident;
7
8use super::{MetaTypes, TypeEq};
9
10/// Describes the base type information of a specific type information.
11#[derive(Default, Debug, Clone, Eq, PartialEq)]
12pub enum Base {
13    /// The type information has no base type.
14    #[default]
15    None,
16
17    /// The type information extends the provided base type.
18    Extension(Ident),
19
20    /// The type information restricts the provided base type.
21    Restriction(Ident),
22}
23
24impl Base {
25    /// Get the identifier of the base type if it is available.
26    #[must_use]
27    pub fn as_ident(&self) -> Option<&Ident> {
28        match self {
29            Self::None => None,
30            Self::Extension(x) => Some(x),
31            Self::Restriction(x) => Some(x),
32        }
33    }
34
35    /// Extracts the identifier of the base type if it is available.
36    #[must_use]
37    pub fn into_ident(self) -> Option<Ident> {
38        match self {
39            Self::None => None,
40            Self::Extension(x) => Some(x),
41            Self::Restriction(x) => Some(x),
42        }
43    }
44}
45
46impl Display for Base {
47    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
48        match self {
49            Self::None => write!(f, "None"),
50            Self::Extension(x) => write!(f, "Extension({x})"),
51            Self::Restriction(x) => write!(f, "Restriction({x})"),
52        }
53    }
54}
55
56impl TypeEq for Base {
57    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
58        match self {
59            Self::None => hasher.write_u8(0),
60            Self::Extension(x) => {
61                hasher.write_u8(1);
62                x.type_hash(hasher, types);
63            }
64            Self::Restriction(x) => {
65                hasher.write_u8(2);
66                x.type_hash(hasher, types);
67            }
68        }
69    }
70
71    fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
72        #[allow(clippy::enum_glob_use)]
73        use Base::*;
74
75        match (self, other) {
76            (None, None) => true,
77            (Extension(x), Extension(y)) => x.type_eq(y, types),
78            (Restriction(x), Restriction(y)) => x.type_eq(y, types),
79            (_, _) => false,
80        }
81    }
82}