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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use super::*;
use sbor::rust::prelude::*;

pub fn generate_full_schema_from_single_type<
    T: Describe<S::CustomTypeKind<RustTypeId>>,
    S: CustomSchema,
>() -> (LocalTypeId, VersionedSchema<S>) {
    let mut aggregator = TypeAggregator::new();
    let type_id = aggregator.add_child_type_and_descendents::<T>();
    (type_id, generate_full_schema(aggregator))
}

pub fn generate_full_schema<S: CustomSchema>(
    aggregator: TypeAggregator<S::CustomTypeKind<RustTypeId>>,
) -> VersionedSchema<S> {
    let type_count = aggregator.types.len();
    let type_indices = IndexSet::from_iter(aggregator.types.keys().map(|k| k.clone()));

    let mut type_kinds = Vec::with_capacity(type_count);
    let mut type_metadata = Vec::with_capacity(type_count);
    let mut type_validations = Vec::with_capacity(type_count);
    for (_type_hash, type_data) in aggregator.types {
        type_kinds.push(linearize::<S>(type_data.kind, &type_indices));
        type_metadata.push(type_data.metadata);
        type_validations.push(type_data.validation);
    }

    VersionedSchema::V1(Schema {
        type_kinds,
        type_metadata,
        type_validations,
    })
}

pub fn localize_well_known_type_data<S: CustomSchema>(
    type_data: TypeData<S::CustomTypeKind<RustTypeId>, RustTypeId>,
) -> TypeData<S::CustomTypeKind<LocalTypeId>, LocalTypeId> {
    let TypeData {
        kind,
        metadata,
        validation,
    } = type_data;
    TypeData {
        kind: linearize::<S>(kind, &indexset!()),
        metadata,
        validation,
    }
}

pub fn localize_well_known<S: CustomSchema>(
    type_kind: TypeKind<S::CustomTypeKind<RustTypeId>, RustTypeId>,
) -> TypeKind<S::CustomTypeKind<LocalTypeId>, LocalTypeId> {
    linearize::<S>(type_kind, &indexset!())
}

fn linearize<S: CustomSchema>(
    type_kind: TypeKind<S::CustomTypeKind<RustTypeId>, RustTypeId>,
    type_indices: &IndexSet<TypeHash>,
) -> TypeKind<S::CustomTypeKind<LocalTypeId>, LocalTypeId> {
    match type_kind {
        TypeKind::Any => TypeKind::Any,
        TypeKind::Bool => TypeKind::Bool,
        TypeKind::I8 => TypeKind::I8,
        TypeKind::I16 => TypeKind::I16,
        TypeKind::I32 => TypeKind::I32,
        TypeKind::I64 => TypeKind::I64,
        TypeKind::I128 => TypeKind::I128,
        TypeKind::U8 => TypeKind::U8,
        TypeKind::U16 => TypeKind::U16,
        TypeKind::U32 => TypeKind::U32,
        TypeKind::U64 => TypeKind::U64,
        TypeKind::U128 => TypeKind::U128,
        TypeKind::String => TypeKind::String,
        TypeKind::Array { element_type } => TypeKind::Array {
            element_type: resolve_local_type_id(type_indices, &element_type),
        },
        TypeKind::Tuple { field_types } => TypeKind::Tuple {
            field_types: field_types
                .into_iter()
                .map(|t| resolve_local_type_id(type_indices, &t))
                .collect(),
        },
        TypeKind::Enum { variants } => TypeKind::Enum {
            variants: variants
                .into_iter()
                .map(|(variant_index, field_types)| {
                    let new_field_types = field_types
                        .into_iter()
                        .map(|t| resolve_local_type_id(type_indices, &t))
                        .collect();
                    (variant_index, new_field_types)
                })
                .collect(),
        },
        TypeKind::Map {
            key_type,
            value_type,
        } => TypeKind::Map {
            key_type: resolve_local_type_id(type_indices, &key_type),
            value_type: resolve_local_type_id(type_indices, &value_type),
        },
        TypeKind::Custom(custom_type_kind) => {
            TypeKind::Custom(S::linearize_type_kind(custom_type_kind, type_indices))
        }
    }
}

pub fn resolve_local_type_id(
    type_indices: &IndexSet<TypeHash>,
    type_id: &RustTypeId,
) -> LocalTypeId {
    match type_id {
        RustTypeId::WellKnown(well_known_type_id) => LocalTypeId::WellKnown(*well_known_type_id),
        RustTypeId::Novel(type_hash) => {
            LocalTypeId::SchemaLocalIndex(resolve_index(type_indices, type_hash))
        }
    }
}

fn resolve_index(type_indices: &IndexSet<TypeHash>, type_hash: &TypeHash) -> usize {
    type_indices.get_index_of(type_hash).unwrap_or_else(|| {
        panic!(
            "Fatal error in the type aggregation process - this is likely due to a type impl missing a dependent type in add_all_dependencies. The following type hash wasn't added in add_all_dependencies: {:?}",
            type_hash
        )
    })
}

pub struct TypeAggregator<C: CustomTypeKind<RustTypeId>> {
    already_read_dependencies: IndexSet<TypeHash>,
    types: IndexMap<TypeHash, TypeData<C, RustTypeId>>,
}

impl<C: CustomTypeKind<RustTypeId>> TypeAggregator<C> {
    pub fn new() -> Self {
        Self {
            already_read_dependencies: index_set_new(),
            types: IndexMap::default(),
        }
    }

    /// Adds the dependent type (and its dependencies) to the `TypeAggregator`.
    pub fn add_child_type_and_descendents<T: Describe<C>>(&mut self) -> LocalTypeId {
        let schema_type_id = self.add_child_type(T::TYPE_ID, || T::type_data());
        self.add_schema_descendents::<T>();
        schema_type_id
    }

    /// Adds the type's `TypeData` to the `TypeAggregator`.
    ///
    /// If the type is well known or already in the aggregator, this returns early with the existing index.
    ///
    /// Typically you should use [`add_schema_descendents`], unless you're replacing/mutating
    /// the child types somehow. In which case, you'll likely wish to call [`add_child_type`] and
    /// [`add_schema_descendents`] separately.
    ///
    /// [`add_child_type`]: #method.add_child_type
    /// [`add_schema_descendents`]: #method.add_schema_descendents
    /// [`add_child_type_and_descendents`]: #method.add_child_type_and_descendents
    pub fn add_child_type(
        &mut self,
        type_id: RustTypeId,
        get_type_data: impl FnOnce() -> TypeData<C, RustTypeId>,
    ) -> LocalTypeId {
        let complex_type_hash = match type_id {
            RustTypeId::WellKnown(well_known_type_id) => {
                return LocalTypeId::WellKnown(well_known_type_id);
            }
            RustTypeId::Novel(complex_type_hash) => complex_type_hash,
        };

        if let Some(index) = self.types.get_index_of(&complex_type_hash) {
            return LocalTypeId::SchemaLocalIndex(index);
        }

        let new_index = self.types.len();
        self.types.insert(complex_type_hash, get_type_data());
        LocalTypeId::SchemaLocalIndex(new_index)
    }

    /// Adds the type's descendent types to the `TypeAggregator`, if they've not already been added.
    ///
    /// Typically you should use [`add_child_type_and_descendents`], unless you're replacing/mutating
    /// the child types somehow. In which case, you'll likely wish to call [`add_child_type`] and
    /// [`add_schema_descendents`] separately.
    ///
    /// [`add_child_type`]: #method.add_child_type
    /// [`add_schema_descendents`]: #method.add_schema_descendents
    /// [`add_child_type_and_descendents`]: #method.add_child_type_and_descendents
    pub fn add_schema_descendents<T: Describe<C>>(&mut self) -> bool {
        let RustTypeId::Novel(complex_type_hash) = T::TYPE_ID else {
            return false;
        };

        if self.already_read_dependencies.contains(&complex_type_hash) {
            return false;
        }

        self.already_read_dependencies.insert(complex_type_hash);

        T::add_all_dependencies(self);

        return true;
    }
}