xsd_parser/types/type_.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 191
//! Contains the [`Type`] type information and all related types.
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use super::{
AbstractInfo, ComplexInfo, CustomType, EnumerationInfo, GroupInfo, ReferenceInfo, Types,
UnionInfo,
};
/// Represents a type that was read and interpreted from an XML schema.
#[derive(Debug, Clone)]
pub enum Type {
/// Represents a union type
Union(UnionInfo),
/// Represents a build-in type
BuildIn(BuildInInfo),
/// References an other type
Reference(ReferenceInfo),
/// Represents an enumeration
Enumeration(EnumerationInfo),
/// Represents an abstract element
Abstract(AbstractInfo),
/// Represents a specific set of elements
All(GroupInfo),
/// Represents a choice of different elements
Choice(GroupInfo),
/// Represents a sequence of different elements
Sequence(GroupInfo),
/// Represents a complex type
ComplexType(ComplexInfo),
}
/// Trait to check if two types are equal to each other or not.
///
/// This trait will automatically resolve type definitions to its target
/// type before it compares the two instances. This means a type is considered
/// as equal, if all type identifiers point to the same type and all normal
/// values are equal.
pub trait TypeEq {
/// Check if this instance is equal to the `other` instance using the passed
/// `types` to resolve identifiers.
fn type_eq(&self, other: &Self, types: &Types) -> bool;
/// Check if the two passed iterators contain type equal elements.
fn type_eq_iter<'a, X, Y>(x: X, y: Y, types: &Types) -> bool
where
Self: 'a,
X: IntoIterator<Item = &'a Self>,
Y: IntoIterator<Item = &'a Self>,
{
let mut x = x.into_iter();
let mut y = y.into_iter();
loop {
match (x.next(), y.next()) {
(None, None) => return true,
(Some(x), Some(y)) => {
if !x.type_eq(y, types) {
return false;
}
}
(_, _) => return false,
}
}
}
}
/// Union that defined the build in types of the rust language or
/// custom defined types.
#[allow(missing_docs)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum BuildInInfo {
U8,
U16,
U32,
U64,
U128,
Usize,
I8,
I16,
I32,
I64,
I128,
Isize,
F32,
F64,
Bool,
String,
Custom(CustomType),
}
/* Type */
macro_rules! impl_from {
($var:ident, $ty:ty) => {
impl From<$ty> for Type {
fn from(value: $ty) -> Self {
Self::$var(value)
}
}
};
}
impl_from!(Reference, ReferenceInfo);
impl_from!(BuildIn, BuildInInfo);
impl_from!(Enumeration, EnumerationInfo);
impl TypeEq for Type {
fn type_eq(&self, other: &Self, types: &Types) -> bool {
#[allow(clippy::enum_glob_use)]
use Type::*;
match (self, other) {
(Union(x), Union(y)) => x.type_eq(y, types),
(BuildIn(x), BuildIn(y)) => x == y,
(Reference(x), Reference(y)) => x.type_eq(y, types),
(Enumeration(x), Enumeration(y)) => x.type_eq(y, types),
(Abstract(x), Abstract(y)) => x.type_eq(y, types),
(All(x), All(y)) => x.type_eq(y, types),
(Choice(x), Choice(y)) => x.type_eq(y, types),
(Sequence(x), Sequence(y)) => x.type_eq(y, types),
(ComplexType(x), ComplexType(y)) => x.type_eq(y, types),
(_, _) => false,
}
}
}
/* BuildInInfo */
impl BuildInInfo {
/// Get the name of the build-in type as `&str`.
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::U8 => "u8",
Self::U16 => "u16",
Self::U32 => "u32",
Self::U64 => "u64",
Self::U128 => "u128",
Self::Usize => "usize",
Self::I8 => "i8",
Self::I16 => "i16",
Self::I32 => "i32",
Self::I64 => "i64",
Self::I128 => "i128",
Self::Isize => "isize",
Self::F32 => "f32",
Self::F64 => "f64",
Self::Bool => "bool",
Self::String => "String",
Self::Custom(x) => x.name(),
}
}
}
impl Display for BuildInInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}", self.as_str())
}
}
/* TypeEq */
impl<T> TypeEq for Option<T>
where
T: TypeEq,
{
fn type_eq(&self, other: &Self, types: &Types) -> bool {
match (self, other) {
(Some(x), Some(y)) => x.type_eq(y, types),
(None, None) => true,
(_, _) => false,
}
}
}