xsd_parser/types/
ident.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Contains the [`Ident`] helper type and all related types.

use std::fmt::{Display, Formatter, Result as FmtResult};

use crate::schema::NamespaceId;

use super::{Name, Type, TypeEq, Types};

/// Represents a type identifier.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Ident {
    /// Namespace the type is defined in
    pub ns: Option<NamespaceId>,

    /// Name of the type.
    pub name: Name,

    /// Type of the identifier (because pure names are not unique in XSD).
    pub type_: IdentType,
}

/// Type of the identifier.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum IdentType {
    /// Used for `xs:simpleType` or `xs:complexType`.
    Type = 0,

    /// Used for `xs:group`.
    Group = 1,

    /// Used for `xs:element`.
    Element = 2,

    /// Used for `xs:attribute`.
    Attribute = 3,

    /// Used for `xs:attributeGroup`.
    AttributeGroup = 4,

    /// Used for build-in types.
    BuildIn = 5,

    /// Used for `xs:enumeration`.
    Enumeration = 6,
}

#[allow(missing_docs)]
impl Ident {
    pub const U8: Self = Self::build_in("u8");
    pub const U16: Self = Self::build_in("u16");
    pub const U32: Self = Self::build_in("u32");
    pub const U64: Self = Self::build_in("u64");
    pub const U128: Self = Self::build_in("u128");
    pub const USIZE: Self = Self::build_in("usize");

    pub const I8: Self = Self::build_in("i8");
    pub const I16: Self = Self::build_in("i16");
    pub const I32: Self = Self::build_in("i32");
    pub const I64: Self = Self::build_in("i64");
    pub const I128: Self = Self::build_in("i128");
    pub const ISIZE: Self = Self::build_in("isize");

    pub const F32: Self = Self::build_in("f32");
    pub const F64: Self = Self::build_in("f64");

    pub const BOOL: Self = Self::build_in("bool");
    pub const STRING: Self = Self::build_in("String");

    pub const BUILD_IN: &[Self] = &[
        Self::U8,
        Self::U16,
        Self::U32,
        Self::U64,
        Self::U128,
        Self::USIZE,
        Self::I8,
        Self::I16,
        Self::I32,
        Self::I64,
        Self::I128,
        Self::ISIZE,
        Self::F32,
        Self::F64,
        Self::BOOL,
        Self::STRING,
    ];
}

impl Ident {
    /// Create an [`Type`](IdentType::Type) [`Ident`]ifier with the given `name`.
    #[must_use]
    pub fn new(name: Name) -> Self {
        Self {
            ns: None,
            name,
            type_: IdentType::Type,
        }
    }

    /// Create an [`Type`](IdentType::Type) [`Ident`]ifier with the given `name`.
    #[must_use]
    pub const fn type_(name: &'static str) -> Self {
        Self {
            ns: None,
            name: Name::named(name),
            type_: IdentType::Type,
        }
    }

    /// Create an [`BuildIn`](IdentType::BuildIn) [`Ident`]ifier with the given `name`.
    #[must_use]
    pub const fn build_in(name: &'static str) -> Self {
        Self {
            ns: None,
            name: Name::named(name),
            type_: IdentType::BuildIn,
        }
    }

    /// Create an [`Element`](IdentType::Element) [`Ident`]ifier with the given `name`.
    #[must_use]
    pub const fn element(name: &'static str) -> Self {
        Self {
            ns: None,
            name: Name::named(name),
            type_: IdentType::Element,
        }
    }

    /// Set the namespace of the identifier.
    #[must_use]
    pub fn with_ns(mut self, ns: Option<NamespaceId>) -> Self {
        self.ns = ns;

        self
    }

    /// Set the type of the identifier.
    #[must_use]
    pub fn with_type(mut self, type_: IdentType) -> Self {
        self.type_ = type_;

        self
    }

    /// Returns `true` if this is build-in type of the rust language, `false` otherwise.
    #[must_use]
    pub fn is_build_in(&self) -> bool {
        Ident::BUILD_IN.contains(self)
    }
}

impl Display for Ident {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        let Self { ns, name, type_ } = self;

        match type_ {
            IdentType::Type => write!(f, "Type(")?,
            IdentType::Group => write!(f, "Group(")?,
            IdentType::BuildIn => write!(f, "BuildIn(")?,
            IdentType::Element => write!(f, "Element(")?,
            IdentType::Attribute => write!(f, "Attribute(")?,
            IdentType::AttributeGroup => write!(f, "AttributeGroup(")?,
            IdentType::Enumeration => write!(f, "Enumeration(")?,
        }

        if f.sign_minus() {
            write!(f, "{name})")?;
        } else if let Some(ns) = ns {
            write!(f, "ns={}, name={name})", ns.0)?;
        } else {
            write!(f, "ns=default, name={name})")?;
        }

        Ok(())
    }
}

impl TypeEq for Ident {
    fn type_eq(&self, other: &Self, types: &Types) -> bool {
        resolve(types, self) == resolve(types, other)
    }
}

fn resolve<'a>(types: &'a Types, ident: &'a Ident) -> &'a Ident {
    match types.get(ident) {
        Some(Type::Reference(x)) if x.is_single() => resolve(types, &x.type_),
        None | Some(_) => ident,
    }
}