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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::rust::prelude::*;
use crate::*;

define_versioned!(
    #[derive(Debug, Clone, PartialEq, Eq, Sbor)]
    #[sbor(child_types = "S::CustomTypeKind<LocalTypeId>, S::CustomTypeValidation")]
    pub enum VersionedSchema<S: CustomSchema> {
        latest_version: {
            1 => Schema<S> = SchemaV1::<S>,
        }
    }
);

impl<S: CustomSchema> VersionedSchema<S> {
    pub fn v1(&self) -> &SchemaV1<S> {
        match self {
            VersionedSchema::V1(schema) => schema,
        }
    }

    pub fn v1_mut(&mut self) -> &mut SchemaV1<S> {
        match self {
            VersionedSchema::V1(schema) => schema,
        }
    }
}

impl<S: CustomSchema> VersionedSchema<S> {
    pub fn empty() -> Self {
        Schema::empty().into()
    }
}

impl<S: CustomSchema> Default for VersionedSchema<S> {
    fn default() -> Self {
        Self::empty()
    }
}

/// An array of custom type kinds, and associated extra information which can attach to the type kinds
#[derive(Debug, Clone, PartialEq, Eq, Sbor)]
// NB - the generic parameter E isn't embedded in the value model itself - instead:
// * Via TypeKind, S::CustomTypeKind<LocalTypeId> gets embedded
// * Via TypeValidation, S::CustomTypeValidation gets embedded
// So theses are the child types which need to be registered with the sbor macro for it to compile
#[sbor(child_types = "S::CustomTypeKind<LocalTypeId>, S::CustomTypeValidation")]
pub struct SchemaV1<S: CustomSchema> {
    pub type_kinds: Vec<SchemaTypeKind<S>>,
    pub type_metadata: Vec<TypeMetadata>, // TODO: reconsider adding type hash when it's ready!
    pub type_validations: Vec<TypeValidation<S::CustomTypeValidation>>,
}

pub type SchemaTypeKind<S> =
    TypeKind<<S as CustomSchema>::CustomTypeKind<LocalTypeId>, LocalTypeId>;

impl<S: CustomSchema> SchemaV1<S> {
    pub fn empty() -> Self {
        Self {
            type_kinds: vec![],
            type_metadata: vec![],
            type_validations: vec![],
        }
    }

    pub fn resolve_type_kind(
        &self,
        type_id: LocalTypeId,
    ) -> Option<&TypeKind<S::CustomTypeKind<LocalTypeId>, LocalTypeId>> {
        match type_id {
            LocalTypeId::WellKnown(index) => {
                S::resolve_well_known_type(index).map(|data| &data.kind)
            }
            LocalTypeId::SchemaLocalIndex(index) => self.type_kinds.get(index),
        }
    }

    pub fn resolve_type_metadata(&self, type_id: LocalTypeId) -> Option<&TypeMetadata> {
        match type_id {
            LocalTypeId::WellKnown(index) => {
                S::resolve_well_known_type(index).map(|data| &data.metadata)
            }
            LocalTypeId::SchemaLocalIndex(index) => self.type_metadata.get(index),
        }
    }

    pub fn resolve_matching_tuple_metadata(
        &self,
        type_id: LocalTypeId,
        fields_length: usize,
    ) -> TupleData<'_> {
        self.resolve_type_metadata(type_id)
            .map(|m| m.get_matching_tuple_data(fields_length))
            .unwrap_or_default()
    }

    pub fn resolve_matching_enum_metadata<'s>(
        &self,
        type_id: LocalTypeId,
        variant_id: u8,
        fields_length: usize,
    ) -> EnumVariantData<'_> {
        self.resolve_type_metadata(type_id)
            .map(|m| m.get_matching_enum_variant_data(variant_id, fields_length))
            .unwrap_or_default()
    }

    pub fn resolve_matching_array_metadata(&self, type_id: LocalTypeId) -> ArrayData<'_> {
        let Some(TypeKind::Array { element_type }) = self.resolve_type_kind(type_id) else {
            return ArrayData::default();
        };
        ArrayData {
            array_name: self
                .resolve_type_metadata(type_id)
                .and_then(|m| m.get_name()),
            element_name: self
                .resolve_type_metadata(*element_type)
                .and_then(|m| m.get_name()),
        }
    }

    pub fn resolve_matching_map_metadata(&self, type_id: LocalTypeId) -> MapData<'_> {
        let Some(TypeKind::Map {
            key_type,
            value_type,
        }) = self.resolve_type_kind(type_id)
        else {
            return MapData::default();
        };
        MapData {
            map_name: self
                .resolve_type_metadata(type_id)
                .and_then(|m| m.get_name()),
            key_name: self
                .resolve_type_metadata(*key_type)
                .and_then(|m| m.get_name()),
            value_name: self
                .resolve_type_metadata(*value_type)
                .and_then(|m| m.get_name()),
        }
    }

    pub fn resolve_type_name_from_metadata(&self, type_id: LocalTypeId) -> Option<&'_ str> {
        self.resolve_type_metadata(type_id)
            .and_then(|m| m.get_name())
    }

    pub fn resolve_type_validation(
        &self,
        type_id: LocalTypeId,
    ) -> Option<&TypeValidation<S::CustomTypeValidation>> {
        match type_id {
            LocalTypeId::WellKnown(index) => {
                S::resolve_well_known_type(index).map(|data| &data.validation)
            }
            LocalTypeId::SchemaLocalIndex(index) => self.type_validations.get(index),
        }
    }

    pub fn validate(&self) -> Result<(), SchemaValidationError> {
        validate_schema(self)
    }
}

impl<S: CustomSchema> Default for SchemaV1<S> {
    fn default() -> Self {
        Self::empty()
    }
}

#[derive(Debug, Default)]
pub struct ArrayData<'m> {
    pub array_name: Option<&'m str>,
    pub element_name: Option<&'m str>,
}

#[derive(Debug, Default)]
pub struct MapData<'m> {
    pub map_name: Option<&'m str>,
    pub key_name: Option<&'m str>,
    pub value_name: Option<&'m str>,
}