odra_schema/
custom_type.rs

1use std::collections::BTreeMap;
2
3use casper_contract_schema::CustomType;
4use casper_types::bytesrepr::{FromBytes, ToBytes};
5use casper_types::{bytesrepr::Bytes, CLTyped, PublicKey, U128, U256, U512};
6use casper_types::{Key, URef};
7use num_traits::{Num, One, Zero};
8use odra_core::args::Maybe;
9use odra_core::prelude::*;
10use odra_core::ContractRef;
11
12use crate::{SchemaCustomTypes, SchemaErrors, SchemaEvents};
13
14macro_rules! impl_schema_custom_types {
15    ($($t:ty),*) => {
16        $(
17            impl SchemaCustomTypes for $t {
18            }
19        )*
20    };
21}
22
23macro_rules! impl_schema_errors {
24    ($($t:ty),*) => {
25        $(
26            impl SchemaErrors for $t {
27            }
28        )*
29    };
30}
31
32macro_rules! impl_schema_events {
33    ($($t:ty),*) => {
34        $(
35            impl SchemaEvents for $t {
36            }
37        )*
38    };
39}
40
41impl_schema_custom_types!(
42    u8,
43    u32,
44    u64,
45    i32,
46    i64,
47    U128,
48    U256,
49    U512,
50    Address,
51    bool,
52    String,
53    (),
54    PublicKey,
55    Bytes,
56    URef
57);
58
59impl<T: SchemaCustomTypes> SchemaCustomTypes for Option<T> {
60    fn schema_types() -> Vec<Option<CustomType>> {
61        T::schema_types()
62    }
63}
64
65impl<T: SchemaCustomTypes> SchemaCustomTypes for Vec<T> {
66    fn schema_types() -> Vec<Option<CustomType>> {
67        T::schema_types()
68    }
69}
70
71impl<T: SchemaCustomTypes, E: SchemaCustomTypes> SchemaCustomTypes for Result<T, E> {
72    fn schema_types() -> Vec<Option<CustomType>> {
73        let mut types = vec![];
74        types.extend(T::schema_types());
75        types.extend(E::schema_types());
76        types
77    }
78}
79
80impl<T: SchemaCustomTypes, E: SchemaCustomTypes> SchemaCustomTypes for BTreeMap<T, E> {
81    fn schema_types() -> Vec<Option<CustomType>> {
82        let mut types = vec![];
83        types.extend(T::schema_types());
84        types.extend(E::schema_types());
85        types
86    }
87}
88
89impl<T1: SchemaCustomTypes> SchemaCustomTypes for (T1,) {
90    fn schema_types() -> Vec<Option<CustomType>> {
91        T1::schema_types()
92    }
93}
94
95impl<T1: SchemaCustomTypes, T2: SchemaCustomTypes> SchemaCustomTypes for (T1, T2) {
96    fn schema_types() -> Vec<Option<CustomType>> {
97        let mut types = vec![];
98        types.extend(T1::schema_types());
99        types.extend(T2::schema_types());
100        types
101    }
102}
103
104impl<T1: SchemaCustomTypes, T2: SchemaCustomTypes, T3: SchemaCustomTypes> SchemaCustomTypes
105    for (T1, T2, T3)
106{
107    fn schema_types() -> Vec<Option<CustomType>> {
108        let mut types = vec![];
109        types.extend(T1::schema_types());
110        types.extend(T2::schema_types());
111        types.extend(T3::schema_types());
112        types
113    }
114}
115
116impl SchemaCustomTypes for Key {}
117
118impl<const COUNT: usize> SchemaCustomTypes for [u8; COUNT] {}
119
120impl<T: SchemaCustomTypes> SchemaCustomTypes for Maybe<T> {
121    fn schema_types() -> Vec<Option<CustomType>> {
122        T::schema_types()
123    }
124}
125
126impl<M: SchemaErrors> SchemaErrors for SubModule<M> {
127    fn schema_errors() -> Vec<casper_contract_schema::UserError> {
128        M::schema_errors()
129    }
130}
131
132impl_schema_errors!(
133    u8,
134    u32,
135    u64,
136    i32,
137    i64,
138    U128,
139    U256,
140    U512,
141    Address,
142    bool,
143    String,
144    (),
145    PublicKey,
146    Bytes
147);
148
149impl<T: CLTyped> SchemaErrors for Option<T> {}
150
151impl<T: CLTyped> SchemaErrors for Vec<T> {}
152
153impl<T: CLTyped, E: CLTyped> SchemaErrors for Result<T, E> {}
154
155impl<T: CLTyped, E: CLTyped> SchemaErrors for BTreeMap<T, E> {}
156
157impl<T1: CLTyped> SchemaErrors for (T1,) {}
158
159impl<T1: CLTyped, T2: CLTyped> SchemaErrors for (T1, T2) {}
160
161impl<T1: CLTyped, T2: CLTyped, T3: CLTyped> SchemaErrors for (T1, T2, T3) {}
162
163impl<const COUNT: usize> SchemaErrors for [u8; COUNT] {}
164
165impl<T: SchemaErrors> SchemaErrors for Maybe<T> {}
166
167impl_schema_events!(
168    u8,
169    u32,
170    u64,
171    i32,
172    i64,
173    U128,
174    U256,
175    U512,
176    Address,
177    bool,
178    String,
179    (),
180    PublicKey,
181    Bytes
182);
183
184impl<T: SchemaCustomTypes> SchemaEvents for Option<T> {
185    fn custom_types() -> Vec<Option<CustomType>> {
186        T::schema_types()
187    }
188}
189
190impl<T: SchemaCustomTypes> SchemaEvents for Vec<T> {
191    fn custom_types() -> Vec<Option<CustomType>> {
192        T::schema_types()
193    }
194}
195
196impl<T: SchemaCustomTypes, E: SchemaCustomTypes> SchemaEvents for Result<T, E> {
197    fn custom_types() -> Vec<Option<CustomType>> {
198        let mut types = vec![];
199        types.extend(T::schema_types());
200        types.extend(E::schema_types());
201        types
202    }
203}
204
205impl<T: SchemaCustomTypes, E: SchemaCustomTypes> SchemaEvents for BTreeMap<T, E> {
206    fn custom_types() -> Vec<Option<CustomType>> {
207        let mut types = vec![];
208        types.extend(T::schema_types());
209        types.extend(E::schema_types());
210        types
211    }
212}
213
214impl<T1: SchemaCustomTypes> SchemaEvents for (T1,) {
215    fn custom_types() -> Vec<Option<CustomType>> {
216        T1::schema_types()
217    }
218}
219
220impl<T1: SchemaCustomTypes, T2: SchemaCustomTypes> SchemaEvents for (T1, T2) {
221    fn custom_types() -> Vec<Option<CustomType>> {
222        let mut types = vec![];
223        types.extend(T1::schema_types());
224        types.extend(T2::schema_types());
225        types
226    }
227}
228
229impl<T1: SchemaCustomTypes, T2: SchemaCustomTypes, T3: SchemaCustomTypes> SchemaEvents
230    for (T1, T2, T3)
231{
232    fn custom_types() -> Vec<Option<CustomType>> {
233        let mut types = vec![];
234        types.extend(T1::schema_types());
235        types.extend(T2::schema_types());
236        types.extend(T3::schema_types());
237        types
238    }
239}
240
241impl<const COUNT: usize> SchemaEvents for [u8; COUNT] {}
242
243impl<T: SchemaEvents> SchemaEvents for Maybe<T> {}
244
245// I don't like it, but it's the only way to make it work
246// If it was implemented it `core`, SchemaEvents would be implemented for ModulePrimitive
247impl<M: SchemaErrors + ContractRef> SchemaErrors for External<M> {}
248impl<M: SchemaErrors> SchemaErrors for Var<M> {}
249impl<K: SchemaErrors, V> SchemaErrors for Mapping<K, V> {}
250impl<V: SchemaErrors> SchemaErrors for List<V> {}
251impl<T: Num + One + Zero + Default + Copy + ToBytes + FromBytes + CLTyped> SchemaErrors
252    for Sequence<T>
253{
254}
255
256impl<M: SchemaEvents> SchemaEvents for SubModule<M> {
257    fn schema_events() -> Vec<casper_contract_schema::Event> {
258        M::schema_events()
259    }
260
261    fn custom_types() -> Vec<Option<CustomType>> {
262        M::custom_types()
263    }
264}
265// I don't like it, but it's the only way to make it work
266// If it was implemented it `core`, SchemaEvents would be implemented for ModulePrimitive
267impl<M: SchemaEvents + ContractRef> SchemaEvents for External<M> {}
268impl<M: SchemaEvents> SchemaEvents for Var<M> {}
269impl<K: SchemaEvents, V> SchemaEvents for Mapping<K, V> {}
270impl<V: SchemaEvents> SchemaEvents for List<V> {}
271impl<T: Num + One + Zero + Default + Copy + ToBytes + FromBytes + CLTyped> SchemaEvents
272    for Sequence<T>
273{
274}