xsd_parser/models/meta/
simple.rs

1use std::hash::{Hash, Hasher};
2
3use crate::models::Ident;
4
5use super::{Constrains, MetaTypes, TypeEq};
6
7/// Type information that contains data about a simple type including
8/// it's potential restrictions.
9///
10/// If a simple type definition has additional restrictions (like `xs:minExclusive`
11/// or `xs:minLength`) it is represented as [`SimpleType`](super::MetaTypeVariant::SimpleType)
12/// instead of a simple [`Reference`](super::MetaTypeVariant::Reference).
13#[derive(Debug, Clone)]
14pub struct SimpleMeta {
15    /// Type that is referenced.
16    pub base: Ident,
17
18    /// `true` if this simple type is a list, `false` otherwise.
19    pub is_list: bool,
20
21    /// Constraining facets defined for this type.
22    pub constrains: Constrains,
23}
24
25/// Defines how to deal with whitespaces inside a XML element.
26#[derive(Default, Debug, Clone, Copy, Hash, Eq, PartialEq)]
27pub enum WhiteSpace {
28    /// Whitespace is kept exactly as written.
29    #[default]
30    Preserve,
31
32    /// Tabs, line feeds, and carriage returns are replaced with spaces.
33    Replace,
34
35    /// All whitespace sequences are collapsed to a single space, and
36    /// leading/trailing whitespace is removed.
37    Collapse,
38}
39
40impl SimpleMeta {
41    /// Create a new [`SimpleMeta`] instance from the passed `base` identifier.
42    #[must_use]
43    pub fn new(base: Ident) -> Self {
44        Self {
45            base,
46            is_list: false,
47            constrains: Constrains::default(),
48        }
49    }
50}
51
52impl TypeEq for SimpleMeta {
53    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
54        let Self {
55            base,
56            is_list,
57            constrains,
58        } = self;
59
60        base.type_hash(hasher, types);
61        is_list.hash(hasher);
62        constrains.hash(hasher);
63    }
64
65    fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
66        let Self {
67            base,
68            is_list,
69            constrains,
70        } = self;
71
72        base.type_eq(&other.base, types)
73            && is_list.eq(&other.is_list)
74            && constrains.eq(&other.constrains)
75    }
76}
77
78impl TypeEq for WhiteSpace {
79    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
80        let _types = types;
81
82        self.hash(hasher);
83    }
84
85    fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
86        let _types = types;
87
88        self.eq(other)
89    }
90}