xsd_parser/types/info/
attribute.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
//! Contains the [`AttributeInfo`] type information and all related types.

use std::ops::{Deref, DerefMut};

use crate::schema::xs::Use;
use crate::types::{Ident, TypeEq, Types};

/// Type information that contains data about attribute definitions.
#[derive(Debug, Clone)]
pub struct AttributeInfo {
    /// Identifier of the attribute.
    pub ident: Ident,

    /// Type of the attribute.
    pub type_: Ident,

    /// Usage of the attribute.
    pub use_: Use,

    /// Default value of the attribute.
    pub default: Option<String>,
}

/// Type information that represents a list of [`AttributeInfo`] instances.
#[derive(Default, Debug, Clone)]
pub struct AttributesInfo(Vec<AttributeInfo>);

/* AttributeInfo */

impl AttributeInfo {
    /// Create a new [`AttributeInfo`] instance from the passed `name` and `type_`.
    #[must_use]
    pub fn new(ident: Ident, type_: Ident) -> Self {
        Self {
            ident,
            type_,
            use_: Use::Optional,
            default: None,
        }
    }

    /// Set the [`Use`] value of the attribute.
    #[must_use]
    pub fn with_use(mut self, use_: Use) -> Self {
        self.use_ = use_;

        self
    }
}

impl TypeEq for AttributeInfo {
    fn type_eq(&self, other: &Self, types: &Types) -> bool {
        let Self {
            ident,
            type_,
            use_,
            default,
        } = self;

        ident.eq(&other.ident)
            && type_.type_eq(&other.type_, types)
            && use_.eq(&other.use_)
            && default.eq(&other.default)
    }
}

/* AttributesInfo */

impl Deref for AttributesInfo {
    type Target = Vec<AttributeInfo>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for AttributesInfo {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl TypeEq for AttributesInfo {
    fn type_eq(&self, other: &Self, types: &Types) -> bool {
        TypeEq::type_eq_iter(self.0.iter(), other.0.iter(), types)
    }
}