xsd_parser/types/info/
attribute.rs

1//! Contains the [`AttributeInfo`] type information and all related types.
2
3use std::hash::{Hash, Hasher};
4use std::ops::{Deref, DerefMut};
5
6use crate::schema::xs::Use;
7use crate::types::{Ident, TypeEq, Types};
8
9use super::use_hash;
10
11/// Type information that contains data about attribute definitions.
12#[derive(Debug, Clone)]
13pub struct AttributeInfo {
14    /// Identifier of the attribute.
15    pub ident: Ident,
16
17    /// Type of the attribute.
18    pub type_: Ident,
19
20    /// Usage of the attribute.
21    pub use_: Use,
22
23    /// Default value of the attribute.
24    pub default: Option<String>,
25
26    /// Name of the attribute to use inside the generated code.
27    pub display_name: Option<String>,
28}
29
30/// Type information that represents a list of [`AttributeInfo`] instances.
31#[derive(Default, Debug, Clone)]
32pub struct AttributesInfo(Vec<AttributeInfo>);
33
34/* AttributeInfo */
35
36impl AttributeInfo {
37    /// Create a new [`AttributeInfo`] instance from the passed `name` and `type_`.
38    #[must_use]
39    pub fn new(ident: Ident, type_: Ident) -> Self {
40        Self {
41            ident,
42            type_,
43            use_: Use::Optional,
44            default: None,
45            display_name: None,
46        }
47    }
48
49    /// Set the [`Use`] value of the attribute.
50    #[must_use]
51    pub fn with_use(mut self, use_: Use) -> Self {
52        self.use_ = use_;
53
54        self
55    }
56}
57
58impl TypeEq for AttributeInfo {
59    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
60        let Self {
61            ident,
62            type_,
63            use_,
64            default,
65            display_name,
66        } = self;
67
68        ident.hash(hasher);
69        type_.type_hash(hasher, types);
70        use_hash(use_, hasher);
71        default.hash(hasher);
72        display_name.hash(hasher);
73    }
74
75    fn type_eq(&self, other: &Self, types: &Types) -> bool {
76        let Self {
77            ident,
78            type_,
79            use_,
80            default,
81            display_name,
82        } = self;
83
84        ident.eq(&other.ident)
85            && type_.type_eq(&other.type_, types)
86            && use_.eq(&other.use_)
87            && default.eq(&other.default)
88            && display_name.eq(&other.display_name)
89    }
90}
91
92/* AttributesInfo */
93
94impl Deref for AttributesInfo {
95    type Target = Vec<AttributeInfo>;
96
97    fn deref(&self) -> &Self::Target {
98        &self.0
99    }
100}
101
102impl DerefMut for AttributesInfo {
103    fn deref_mut(&mut self) -> &mut Self::Target {
104        &mut self.0
105    }
106}
107
108impl TypeEq for AttributesInfo {
109    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
110        TypeEq::type_hash_slice(&self.0, hasher, types);
111    }
112
113    fn type_eq(&self, other: &Self, types: &Types) -> bool {
114        TypeEq::type_eq_iter(self.0.iter(), other.0.iter(), types)
115    }
116}