Skip to main content

xsd_parser/models/meta/
dynamic.rs

1//! Contains the [`DynamicMeta`] type information and all related types.
2
3use std::hash::{Hash, Hasher};
4
5use crate::models::TypeIdent;
6
7use super::{MetaTypes, TypeEq};
8
9/// Type information that contains data about dynamic types.
10#[derive(Default, Debug, Clone)]
11pub struct DynamicMeta {
12    /// Base type of the dynamic type.
13    pub type_: Option<TypeIdent>,
14
15    /// List of derived types.
16    pub derived_types: Vec<DerivedTypeMeta>,
17}
18
19/// Meta^ information about a derived type.
20#[derive(Debug, Clone)]
21pub struct DerivedTypeMeta {
22    /// Identifier of the derived type.
23    pub type_: TypeIdent,
24
25    /// Name of the element to use inside the generated code.
26    pub display_name: Option<String>,
27}
28
29impl TypeEq for DynamicMeta {
30    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
31        let Self {
32            type_,
33            derived_types,
34        } = self;
35
36        type_.type_hash(hasher, types);
37        TypeEq::type_hash_slice(derived_types, hasher, types);
38    }
39
40    fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
41        let Self {
42            type_,
43            derived_types,
44        } = self;
45
46        type_.type_eq(&other.type_, types)
47            && TypeEq::type_eq_iter(derived_types.iter(), other.derived_types.iter(), types)
48    }
49}
50
51impl DerivedTypeMeta {
52    /// Creates a new [`DerivedTypeMeta`] instance with the given `type_`.
53    #[must_use]
54    pub fn new(type_: TypeIdent) -> Self {
55        Self {
56            type_,
57            display_name: None,
58        }
59    }
60}
61
62impl TypeEq for DerivedTypeMeta {
63    fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
64        let Self {
65            type_,
66            display_name,
67        } = self;
68
69        type_.type_hash(hasher, types);
70        display_name.hash(hasher);
71    }
72
73    fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
74        let Self {
75            type_,
76            display_name,
77        } = self;
78
79        type_.type_eq(&other.type_, types) && display_name == &other.display_name
80    }
81}