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

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

use crate::types::{Ident, TypeEq, Types};

use super::Base;

/// Type information that defines a union type.
#[derive(Default, Debug, Clone)]
pub struct UnionInfo {
    /// Base type of the union type.
    pub base: Base,

    /// Types that are unified in this union type.
    pub types: UnionTypesInfo,
}

/// Type information that represents one type unified by a [`UnionInfo`].
#[derive(Debug, Clone)]
pub struct UnionTypeInfo {
    /// Target type of this type variant.
    pub type_: Ident,
}

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

/* UnionInfo */

impl TypeEq for UnionInfo {
    fn type_eq(&self, other: &Self, ctx: &Types) -> bool {
        let Self { base, types } = self;

        base.type_eq(&other.base, ctx) && types.type_eq(&other.types, ctx)
    }
}

/* UnionTypeInfo */

impl UnionTypeInfo {
    /// Create a new [`UnionTypeInfo`] from the passed `type_`.
    #[must_use]
    pub fn new(type_: Ident) -> Self {
        Self { type_ }
    }
}

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

        type_.type_eq(&other.type_, types)
    }
}

/* UnionTypesInfo */

impl Deref for UnionTypesInfo {
    type Target = Vec<UnionTypeInfo>;

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

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

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