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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
//! Types related to contract schemas.
//! These are optional annotations in modules that allow
//! the users of smart contracts to interact with them in
//! a way that is better than constructing raw bytes as parameters.

use crate::{impls::*, traits::*, types::*};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::{collections, string::String, vec::Vec};
use collections::{BTreeMap, BTreeSet};
#[cfg(not(feature = "std"))]
use core::{
    convert::{TryFrom, TryInto},
    num::TryFromIntError,
};
/// Contract schema related types
#[cfg(feature = "std")]
use std::{
    collections,
    convert::{TryFrom, TryInto},
    num::TryFromIntError,
};

/// The `SchemaType` trait provides means to generate a schema for structures.
/// Schemas are used to make structures human readable and to avoid dealing
/// directly with bytes, such as the contract state or parameters for contract
/// interaction.
///
/// Can be derived using `#[derive(SchemaType)]` for most cases of structs and
/// enums.
pub trait SchemaType {
    fn get_type() -> crate::schema::Type;
}

/// Contains all the contract schemas for a smart contract module V0.
///
/// Older versions of smart contracts might have this embedded in the custom
/// section labelled `concordium-schema-v1`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleV0 {
    pub contracts: BTreeMap<String, ContractV0>,
}

/// Contains all the contract schemas for a smart contract module V1.
///
/// Older versions of smart contracts might have this embedded in the custom
/// section labelled `concordium-schema-v2`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleV1 {
    pub contracts: BTreeMap<String, ContractV1>,
}

/// Contains all the contract schemas for a smart contract module V1 with a V2
/// schema.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleV2 {
    pub contracts: BTreeMap<String, ContractV2>,
}

/// Contains all the contract schemas for a smart contract module V1 with a V3
/// schema.
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleV3 {
    pub contracts: BTreeMap<String, ContractV3>,
}

/// Represents the different schema versions.
///
/// The serialization of this type includes the versioning information. The
/// serialization of this is always prefixed with two 255u8 in order to
/// distinguish this versioned schema from the unversioned.
///
/// When embedded into a smart contract module, name the custom section
/// `concordium-schema`.
#[derive(Debug, Clone)]
pub enum VersionedModuleSchema {
    /// Version 0 schema, only supported by V0 smart contracts.
    V0(ModuleV0),
    /// Version 1 schema, only supported by V1 smart contracts.
    V1(ModuleV1),
    /// Version 2 schema, only supported by V1 smart contracts.
    V2(ModuleV2),
    /// Version 3 schema, only supported by V1 smart contracts.
    V3(ModuleV3),
}

/// Describes all the schemas of a V0 smart contract.
/// The [`Default`] instance produces an empty schema.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ContractV0 {
    pub state:   Option<Type>,
    pub init:    Option<Type>,
    pub receive: BTreeMap<String, Type>,
}

/// Describes all the schemas of a V1 smart contract.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
/// The [`Default`] instance produces an empty schema.
pub struct ContractV1 {
    pub init:    Option<FunctionV1>,
    pub receive: BTreeMap<String, FunctionV1>,
}

/// Describes all the schemas of a V1 smart contract with a V2 schema.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
/// The [`Default`] instance produces an empty schema.
pub struct ContractV2 {
    pub init:    Option<FunctionV2>,
    pub receive: BTreeMap<String, FunctionV2>,
}

/// Describes all the schemas of a V1 smart contract with a V3 schema.
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
/// The [`Default`] instance produces an empty schema.
pub struct ContractV3 {
    pub init:    Option<FunctionV2>,
    pub receive: BTreeMap<String, FunctionV2>,
    pub event:   Option<Type>,
}

impl ContractV3 {
    /// Extract the event schema if it exists.
    pub fn event(&self) -> Option<&Type> { self.event.as_ref() }
}

/// Describes the schema of an init or a receive function for V1 contracts with
/// V1 schemas.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FunctionV1 {
    Parameter(Type),
    ReturnValue(Type),
    Both {
        parameter:    Type,
        return_value: Type,
    },
}

impl FunctionV1 {
    /// Extract the parameter schema if it exists.
    pub fn parameter(&self) -> Option<&Type> {
        match self {
            FunctionV1::Parameter(ty) => Some(ty),
            FunctionV1::ReturnValue(_) => None,
            FunctionV1::Both {
                parameter,
                ..
            } => Some(parameter),
        }
    }

    /// Extract the return value schema if it exists.
    pub fn return_value(&self) -> Option<&Type> {
        match self {
            FunctionV1::Parameter(_) => None,
            FunctionV1::ReturnValue(rv) => Some(rv),
            FunctionV1::Both {
                return_value,
                ..
            } => Some(return_value),
        }
    }
}

/// Describes the schema of an init or a receive function for V1 contracts with
/// V3 schemas. Differs from [`FunctionV1`] in that a schema for the error can
/// be included.
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionV2 {
    pub parameter:    Option<Type>,
    pub return_value: Option<Type>,
    pub error:        Option<Type>,
}

impl FunctionV2 {
    /// Extract the parameter schema if it exists.
    pub fn parameter(&self) -> Option<&Type> { self.parameter.as_ref() }

    /// Extract the return value schema if it exists.
    pub fn return_value(&self) -> Option<&Type> { self.return_value.as_ref() }

    /// Extract the error schema if it exists.
    pub fn error(&self) -> Option<&Type> { self.error.as_ref() }
}

/// Schema for the fields of a struct or some enum variant.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub enum Fields {
    /// Named fields, e.g., `struct Foo {x: u64, y: u32}`.
    Named(Vec<(String, Type)>),
    /// Unnamed fields, e.g., `struct Foo(u64, u32)`
    Unnamed(Vec<Type>),
    /// No fields. Note that this is distinct from an empty set of named or
    /// unnamed fields. That is, in Rust there is a (albeit trivial) difference
    /// between `struct Foo {}`, `struct Foo`, and `struct Foo()`, all of which
    /// are valid, but will have different representations.
    None,
}

/// Type of the variable used to encode the length of Sets, List, Maps
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub enum SizeLength {
    U8,
    U16,
    U32,
    U64,
}

/// Schema type used to describe the different types in a smart contract, their
/// serialization and how to represent the types in JSON.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub enum Type {
    /// A type with no serialization.
    Unit,
    /// Boolean. Serialized as a byte, where the value 0 is false and 1 is true.
    Bool,
    /// Unsigned 8-bit integer.
    U8,
    /// Unsigned 16-bit integer. Serialized as little endian.
    U16,
    /// Unsigned 32-bit integer. Serialized as little endian.
    U32,
    /// Unsigned 64-bit integer. Serialized as little endian.
    U64,
    /// Unsigned 128-bit integer. Serialized as little endian.
    U128,
    /// Signed 8-bit integer. Serialized as little endian.
    I8,
    /// Signed 16-bit integer. Serialized as little endian.
    I16,
    /// Signed 32-bit integer. Serialized as little endian.
    I32,
    /// Signed 64-bit integer. Serialized as little endian.
    I64,
    /// Signed 128-bit integer. Serialized as little endian.
    I128,
    /// An amount of CCD. Serialized as 64-bit unsigned integer little endian.
    Amount,
    /// An account address.
    AccountAddress,
    /// A contract address.
    ContractAddress,
    /// A timestamp. Represented as milliseconds since Unix epoch. Serialized as
    /// a 64-bit unsigned integer little endian.
    Timestamp,
    /// A duration of milliseconds, cannot be negative. Serialized as a 64-bit
    /// unsigned integer little endian.
    Duration,
    /// A pair.
    Pair(Box<Type>, Box<Type>),
    /// A list. It is serialized with the length first followed by the list
    /// items.
    List(SizeLength, Box<Type>),
    /// A Set. It is serialized with the length first followed by the list
    /// items.
    Set(SizeLength, Box<Type>),
    /// A Map. It is serialized with the length first followed by key-value
    /// pairs of the entries.
    Map(SizeLength, Box<Type>, Box<Type>),
    /// A fixed sized list.
    Array(u32, Box<Type>),
    /// A structure type with fields.
    Struct(Fields),
    /// A sum type.
    Enum(Vec<(String, Fields)>),
    /// A UTF8 String. It is serialized with the length first followed by the
    /// encoding of the string.
    String(SizeLength),
    /// A smart contract name. It is serialized with the length first followed
    /// by the ASCII encoding of the name.
    ContractName(SizeLength),
    /// A smart contract receive function name. It is serialized with the length
    /// first followed by the ASCII encoding of the name.
    ReceiveName(SizeLength),
    /// An unsigned integer encoded using LEB128 with the addition of a
    /// constraint on the maximum number of bytes to use for an encoding.
    ULeb128(u32),
    /// A signed integer encoded using LEB128 with the addition of a constraint
    /// on the maximum number of bytes to use for an encoding.
    ILeb128(u32),
    /// A list of bytes. It is serialized with the length first followed by the
    /// bytes.
    ByteList(SizeLength),
    /// A fixed sized list of bytes.
    ByteArray(u32),
    /// An enum with a tag.
    TaggedEnum(BTreeMap<u8, (String, Fields)>),
}

impl Type {
    #[doc(hidden)]
    /// Sets the size_length of schema types, with variable size otherwise
    /// it is a noop. Used when deriving SchemaType.
    pub fn set_size_length(self, size_len: SizeLength) -> Type {
        match self {
            Type::List(_, ty) => Type::List(size_len, ty),
            Type::Set(_, ty) => Type::Set(size_len, ty),
            Type::Map(_, key_ty, val_ty) => Type::Map(size_len, key_ty, val_ty),
            Type::String(_) => Type::String(size_len),
            Type::ByteList(_) => Type::ByteList(size_len),
            t => t,
        }
    }
}

impl SchemaType for () {
    fn get_type() -> Type { Type::Unit }
}
impl SchemaType for bool {
    fn get_type() -> Type { Type::Bool }
}
impl SchemaType for u8 {
    fn get_type() -> Type { Type::U8 }
}
impl SchemaType for u16 {
    fn get_type() -> Type { Type::U16 }
}
impl SchemaType for u32 {
    fn get_type() -> Type { Type::U32 }
}
impl SchemaType for u64 {
    fn get_type() -> Type { Type::U64 }
}
impl SchemaType for u128 {
    fn get_type() -> Type { Type::U128 }
}
impl SchemaType for i8 {
    fn get_type() -> Type { Type::I8 }
}
impl SchemaType for i16 {
    fn get_type() -> Type { Type::I16 }
}
impl SchemaType for i32 {
    fn get_type() -> Type { Type::I32 }
}
impl SchemaType for i64 {
    fn get_type() -> Type { Type::I64 }
}
impl SchemaType for i128 {
    fn get_type() -> Type { Type::I128 }
}
impl SchemaType for Amount {
    fn get_type() -> Type { Type::Amount }
}
impl SchemaType for ModuleReference {
    fn get_type() -> Type { Type::ByteArray(32) }
}
impl SchemaType for AccountAddress {
    fn get_type() -> Type { Type::AccountAddress }
}
impl SchemaType for ContractAddress {
    fn get_type() -> Type { Type::ContractAddress }
}
impl SchemaType for Address {
    fn get_type() -> Type {
        Type::Enum(Vec::from([
            (String::from("Account"), Fields::Unnamed(Vec::from([Type::AccountAddress]))),
            (String::from("Contract"), Fields::Unnamed(Vec::from([Type::ContractAddress]))),
        ]))
    }
}
impl SchemaType for Timestamp {
    fn get_type() -> Type { Type::Timestamp }
}
impl SchemaType for Duration {
    fn get_type() -> Type { Type::Duration }
}
impl<T: SchemaType> SchemaType for Option<T> {
    fn get_type() -> Type {
        Type::Enum(Vec::from([
            (String::from("None"), Fields::None),
            (String::from("Some"), Fields::Unnamed(Vec::from([T::get_type()]))),
        ]))
    }
}
impl<L: SchemaType, R: SchemaType> SchemaType for (L, R) {
    fn get_type() -> Type { Type::Pair(Box::new(L::get_type()), Box::new(R::get_type())) }
}
impl<T: SchemaType> SchemaType for Vec<T> {
    fn get_type() -> Type { Type::List(SizeLength::U32, Box::new(T::get_type())) }
}
impl<T: SchemaType> SchemaType for BTreeSet<T> {
    fn get_type() -> Type { Type::Set(SizeLength::U32, Box::new(T::get_type())) }
}
impl<K: SchemaType, V: SchemaType> SchemaType for BTreeMap<K, V> {
    fn get_type() -> Type {
        Type::Map(SizeLength::U32, Box::new(K::get_type()), Box::new(V::get_type()))
    }
}
impl<T: SchemaType> SchemaType for HashSet<T> {
    fn get_type() -> Type { Type::Set(SizeLength::U32, Box::new(T::get_type())) }
}
impl<K: SchemaType, V: SchemaType> SchemaType for HashMap<K, V> {
    fn get_type() -> Type {
        Type::Map(SizeLength::U32, Box::new(K::get_type()), Box::new(V::get_type()))
    }
}
impl SchemaType for [u8] {
    fn get_type() -> Type { Type::ByteList(SizeLength::U32) }
}

impl SchemaType for String {
    fn get_type() -> Type { Type::String(SizeLength::U32) }
}

impl SchemaType for OwnedContractName {
    fn get_type() -> Type { Type::ContractName(SizeLength::U16) }
}

impl SchemaType for OwnedReceiveName {
    fn get_type() -> Type { Type::ReceiveName(SizeLength::U16) }
}

impl SchemaType for OwnedEntrypointName {
    fn get_type() -> Type { Type::String(SizeLength::U16) }
}

impl SchemaType for OwnedParameter {
    fn get_type() -> Type { Type::ByteList(SizeLength::U16) }
}

impl<A: SchemaType, const N: usize> SchemaType for [A; N] {
    fn get_type() -> Type { Type::Array(N.try_into().unwrap(), Box::new(A::get_type())) }
}

impl Serial for Fields {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        match self {
            Fields::Named(fields) => {
                out.write_u8(0)?;
                fields.serial(out)?;
            }
            Fields::Unnamed(fields) => {
                out.write_u8(1)?;
                fields.serial(out)?;
            }
            Fields::None => {
                out.write_u8(2)?;
            }
        }
        Ok(())
    }
}

impl Deserial for Fields {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let idx = source.read_u8()?;
        match idx {
            0 => Ok(Fields::Named(source.get()?)),
            1 => Ok(Fields::Unnamed(source.get()?)),
            2 => Ok(Fields::None),
            _ => Err(ParseError::default()),
        }
    }
}

impl Serial for ModuleV0 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.contracts.serial(out)?;
        Ok(())
    }
}

impl Serial for ModuleV1 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.contracts.serial(out)?;
        Ok(())
    }
}

impl Serial for ModuleV2 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.contracts.serial(out)?;
        Ok(())
    }
}

impl Serial for ModuleV3 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.contracts.serial(out)?;
        Ok(())
    }
}

impl Serial for VersionedModuleSchema {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        // Prefix for versioned module schema, used to distinquish from the unversioned.
        out.write_u16(u16::MAX)?;
        match self {
            VersionedModuleSchema::V0(module) => {
                out.write_u8(0)?;
                module.serial(out)?;
            }
            VersionedModuleSchema::V1(module) => {
                out.write_u8(1)?;
                module.serial(out)?;
            }
            VersionedModuleSchema::V2(module) => {
                out.write_u8(2)?;
                module.serial(out)?;
            }
            VersionedModuleSchema::V3(module) => {
                out.write_u8(3)?;
                module.serial(out)?;
            }
        }
        Ok(())
    }
}

impl Deserial for ModuleV0 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        let contracts = deserial_map_no_length_no_order_check(source, len as usize)?;
        Ok(ModuleV0 {
            contracts,
        })
    }
}

impl Deserial for ModuleV1 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        let contracts = deserial_map_no_length_no_order_check(source, len as usize)?;
        Ok(ModuleV1 {
            contracts,
        })
    }
}

impl Deserial for ModuleV2 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        let contracts = deserial_map_no_length_no_order_check(source, len as usize)?;
        Ok(ModuleV2 {
            contracts,
        })
    }
}

impl Deserial for ModuleV3 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        let contracts = deserial_map_no_length_no_order_check(source, len as usize)?;
        Ok(ModuleV3 {
            contracts,
        })
    }
}

impl Deserial for VersionedModuleSchema {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        // First we ensure the prefix is correct.
        let prefix = source.read_u16()?;
        if prefix != u16::MAX {
            return Err(ParseError {});
        }
        let version = source.read_u8()?;
        match version {
            0 => {
                let module = source.get()?;
                Ok(VersionedModuleSchema::V0(module))
            }
            1 => {
                let module = source.get()?;
                Ok(VersionedModuleSchema::V1(module))
            }
            2 => {
                let module = source.get()?;
                Ok(VersionedModuleSchema::V2(module))
            }
            3 => {
                let module = source.get()?;
                Ok(VersionedModuleSchema::V3(module))
            }
            _ => Err(ParseError {}),
        }
    }
}

impl Serial for ContractV0 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.state.serial(out)?;
        self.init.serial(out)?;
        self.receive.serial(out)?;
        Ok(())
    }
}

impl Serial for ContractV1 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.init.serial(out)?;
        self.receive.serial(out)?;
        Ok(())
    }
}

impl Serial for ContractV2 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.init.serial(out)?;
        self.receive.serial(out)?;
        Ok(())
    }
}

impl Serial for ContractV3 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.init.serial(out)?;
        self.receive.serial(out)?;
        self.event.serial(out)?;
        Ok(())
    }
}

impl Deserial for ContractV0 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let state = source.get()?;
        let init = source.get()?;
        let len: u32 = source.get()?;
        let receive = deserial_map_no_length_no_order_check(source, len as usize)?;
        Ok(ContractV0 {
            state,
            init,
            receive,
        })
    }
}

impl Deserial for ContractV1 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let init = source.get()?;
        let len: u32 = source.get()?;
        let receive = deserial_map_no_length_no_order_check(source, len as usize)?;
        Ok(ContractV1 {
            init,
            receive,
        })
    }
}

impl Deserial for ContractV2 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let init = source.get()?;
        let len: u32 = source.get()?;
        let receive = deserial_map_no_length_no_order_check(source, len as usize)?;
        Ok(ContractV2 {
            init,
            receive,
        })
    }
}

impl Deserial for ContractV3 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let init = source.get()?;
        let len: u32 = source.get()?;
        let receive = deserial_map_no_length_no_order_check(source, len as usize)?;
        let event = source.get()?;
        Ok(ContractV3 {
            init,
            receive,
            event,
        })
    }
}

impl Serial for FunctionV1 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        match self {
            FunctionV1::Parameter(parameter) => {
                out.write_u8(0)?;
                parameter.serial(out)
            }
            FunctionV1::ReturnValue(return_value) => {
                out.write_u8(1)?;
                return_value.serial(out)
            }
            FunctionV1::Both {
                parameter,
                return_value,
            } => {
                out.write_u8(2)?;
                parameter.serial(out)?;
                return_value.serial(out)
            }
        }
    }
}

impl Deserial for FunctionV1 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let idx = source.read_u8()?;
        match idx {
            0 => Ok(FunctionV1::Parameter(source.get()?)),
            1 => Ok(FunctionV1::ReturnValue(source.get()?)),
            2 => Ok(FunctionV1::Both {
                parameter:    source.get()?,
                return_value: source.get()?,
            }),
            _ => Err(ParseError::default()),
        }
    }
}

impl Serial for FunctionV2 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let parameter = self.parameter.is_some();
        let return_value = self.return_value.is_some();
        let error = self.error.is_some();
        let tag: u8 = match (parameter, return_value, error) {
            // parameter
            (true, false, false) => 0,
            // return_value
            (false, true, false) => 1,
            // parameter + return_value
            (true, true, false) => 2,
            // error
            (false, false, true) => 3,
            // parameter + error
            (true, false, true) => 4,
            // return_value + error
            (false, true, true) => 5,
            // parameter + return_value + error
            (true, true, true) => 6,
            // no schema
            (false, false, false) => 7,
        };
        out.write_u8(tag)?;
        if let Some(p) = self.parameter.as_ref() {
            p.serial(out)?;
        }
        if let Some(rv) = self.return_value.as_ref() {
            rv.serial(out)?;
        }
        if let Some(err) = self.error.as_ref() {
            err.serial(out)?;
        }
        Ok(())
    }
}

impl Deserial for FunctionV2 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let idx = source.read_u8()?;
        let mut r = FunctionV2 {
            parameter:    None,
            return_value: None,
            error:        None,
        };
        if idx > 7 {
            return Err(ParseError::default());
        }
        if matches!(idx, 0 | 2 | 4 | 6) {
            let _ = r.parameter.insert(source.get()?);
        }
        if matches!(idx, 1 | 2 | 5 | 6) {
            let _ = r.return_value.insert(source.get()?);
        }
        if matches!(idx, 3 | 4 | 5 | 6) {
            let _ = r.error.insert(source.get()?);
        }
        Ok(r)
    }
}

impl Serial for SizeLength {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        match self {
            SizeLength::U8 => out.write_u8(0)?,
            SizeLength::U16 => out.write_u8(1)?,
            SizeLength::U32 => out.write_u8(2)?,
            SizeLength::U64 => out.write_u8(3)?,
        }
        Ok(())
    }
}

impl Deserial for SizeLength {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let idx = source.read_u8()?;
        match idx {
            0 => Ok(SizeLength::U8),
            1 => Ok(SizeLength::U16),
            2 => Ok(SizeLength::U32),
            3 => Ok(SizeLength::U64),
            _ => Err(ParseError::default()),
        }
    }
}

impl Serial for Type {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        match self {
            Type::Unit => out.write_u8(0),
            Type::Bool => out.write_u8(1),
            Type::U8 => out.write_u8(2),
            Type::U16 => out.write_u8(3),
            Type::U32 => out.write_u8(4),
            Type::U64 => out.write_u8(5),
            Type::I8 => out.write_u8(6),
            Type::I16 => out.write_u8(7),
            Type::I32 => out.write_u8(8),
            Type::I64 => out.write_u8(9),
            Type::Amount => out.write_u8(10),
            Type::AccountAddress => out.write_u8(11),
            Type::ContractAddress => out.write_u8(12),
            Type::Timestamp => out.write_u8(13),
            Type::Duration => out.write_u8(14),
            Type::Pair(left, right) => {
                out.write_u8(15)?;
                left.serial(out)?;
                right.serial(out)
            }
            Type::List(len_size, ty) => {
                out.write_u8(16)?;
                len_size.serial(out)?;
                ty.serial(out)
            }
            Type::Set(len_size, ty) => {
                out.write_u8(17)?;
                len_size.serial(out)?;
                ty.serial(out)
            }
            Type::Map(len_size, key, value) => {
                out.write_u8(18)?;
                len_size.serial(out)?;
                key.serial(out)?;
                value.serial(out)
            }
            Type::Array(len, ty) => {
                out.write_u8(19)?;
                len.serial(out)?;
                ty.serial(out)
            }
            Type::Struct(fields) => {
                out.write_u8(20)?;
                fields.serial(out)
            }
            Type::Enum(fields) => {
                out.write_u8(21)?;
                fields.serial(out)
            }
            Type::String(len) => {
                out.write_u8(22)?;
                len.serial(out)
            }
            Type::U128 => out.write_u8(23),
            Type::I128 => out.write_u8(24),
            Type::ContractName(len_size) => {
                out.write_u8(25)?;
                len_size.serial(out)
            }
            Type::ReceiveName(len_size) => {
                out.write_u8(26)?;
                len_size.serial(out)
            }
            Type::ULeb128(constraint) => {
                out.write_u8(27)?;
                constraint.serial(out)
            }
            Type::ILeb128(constraint) => {
                out.write_u8(28)?;
                constraint.serial(out)
            }
            Type::ByteList(len_size) => {
                out.write_u8(29)?;
                len_size.serial(out)
            }
            Type::ByteArray(len) => {
                out.write_u8(30)?;
                len.serial(out)
            }
            Type::TaggedEnum(fields) => {
                out.write_u8(31)?;
                fields.serial(out)
            }
        }
    }
}

impl Deserial for Type {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let idx = source.read_u8()?;
        match idx {
            0 => Ok(Type::Unit),
            1 => Ok(Type::Bool),
            2 => Ok(Type::U8),
            3 => Ok(Type::U16),
            4 => Ok(Type::U32),
            5 => Ok(Type::U64),
            6 => Ok(Type::I8),
            7 => Ok(Type::I16),
            8 => Ok(Type::I32),
            9 => Ok(Type::I64),
            10 => Ok(Type::Amount),
            11 => Ok(Type::AccountAddress),
            12 => Ok(Type::ContractAddress),
            13 => Ok(Type::Timestamp),
            14 => Ok(Type::Duration),
            15 => {
                let left = Type::deserial(source)?;
                let right = Type::deserial(source)?;
                Ok(Type::Pair(Box::new(left), Box::new(right)))
            }
            16 => {
                let len_size = SizeLength::deserial(source)?;
                let ty = Type::deserial(source)?;
                Ok(Type::List(len_size, Box::new(ty)))
            }
            17 => {
                let len_size = SizeLength::deserial(source)?;
                let ty = Type::deserial(source)?;
                Ok(Type::Set(len_size, Box::new(ty)))
            }
            18 => {
                let len_size = SizeLength::deserial(source)?;
                let key = Type::deserial(source)?;
                let value = Type::deserial(source)?;
                Ok(Type::Map(len_size, Box::new(key), Box::new(value)))
            }
            19 => {
                let len = u32::deserial(source)?;
                let ty = Type::deserial(source)?;
                Ok(Type::Array(len, Box::new(ty)))
            }
            20 => {
                let fields = source.get()?;
                Ok(Type::Struct(fields))
            }
            21 => {
                let variants = source.get()?;
                Ok(Type::Enum(variants))
            }
            22 => {
                let len_size = SizeLength::deserial(source)?;
                Ok(Type::String(len_size))
            }
            23 => Ok(Type::U128),
            24 => Ok(Type::I128),
            25 => {
                let len_size = SizeLength::deserial(source)?;
                Ok(Type::ContractName(len_size))
            }
            26 => {
                let len_size = SizeLength::deserial(source)?;
                Ok(Type::ReceiveName(len_size))
            }
            27 => {
                let constraint = u32::deserial(source)?;
                Ok(Type::ULeb128(constraint))
            }
            28 => {
                let constraint = u32::deserial(source)?;
                Ok(Type::ILeb128(constraint))
            }
            29 => {
                let len_size = SizeLength::deserial(source)?;
                Ok(Type::ByteList(len_size))
            }
            30 => {
                let len = u32::deserial(source)?;
                Ok(Type::ByteArray(len))
            }
            31 => {
                let variants = source.get()?;
                Ok(Type::TaggedEnum(variants))
            }

            _ => Err(ParseError::default()),
        }
    }
}

impl From<TryFromIntError> for ParseError {
    fn from(_: TryFromIntError) -> Self { ParseError::default() }
}

/// Try to convert the `len` to the provided size and serialize it.
pub fn serial_length<W: Write>(
    len: usize,
    size_len: SizeLength,
    out: &mut W,
) -> Result<(), W::Err> {
    let to_w_err = |_| W::Err::default();
    match size_len {
        SizeLength::U8 => u8::try_from(len).map_err(to_w_err)?.serial(out)?,
        SizeLength::U16 => u16::try_from(len).map_err(to_w_err)?.serial(out)?,
        SizeLength::U32 => u32::try_from(len).map_err(to_w_err)?.serial(out)?,
        SizeLength::U64 => u64::try_from(len).map_err(to_w_err)?.serial(out)?,
    }
    Ok(())
}

/// Deserialize a length of provided size.
pub fn deserial_length(source: &mut impl Read, size_len: SizeLength) -> ParseResult<usize> {
    let len: usize = match size_len {
        SizeLength::U8 => u8::deserial(source)?.into(),
        SizeLength::U16 => u16::deserial(source)?.into(),
        SizeLength::U32 => u32::deserial(source)?.try_into()?,
        SizeLength::U64 => u64::deserial(source)?.try_into()?,
    };
    Ok(len)
}

// Versioned schema helpers
#[cfg(feature = "derive-serde")]
mod impls {
    use crate::{from_bytes, schema::*};

    /// Useful for get_versioned_contract_schema(), but it's not currently used
    /// as input or output to any function, so it isn't public.
    enum VersionedContractSchema {
        V0(ContractV0),
        V1(ContractV1),
        V2(ContractV2),
        V3(ContractV3),
    }

    #[derive(Debug, thiserror::Error, Clone, Copy)]
    pub enum VersionedSchemaError {
        #[error("Parse error")]
        ParseError,
        #[error("Missing Schema Version")]
        MissingSchemaVersion,
        #[error("Invalid Schema Version")]
        InvalidSchemaVersion,
        #[error("Unable to find contract schema in module schema")]
        NoContractInModule,
        #[error("Receive function schema not found in contract schema")]
        NoReceiveInContract,
        #[error("Init function schema not found in contract schema")]
        NoInitInContract,
        #[error("Receive function schema does not contain a parameter schema")]
        NoParamsInReceive,
        #[error("Init function schema does not contain a parameter schema")]
        NoParamsInInit,
        #[error("Receive function schema not found in contract schema")]
        NoErrorInReceive,
        #[error("Init function schema does not contain an error schema")]
        NoErrorInInit,
        #[error("Errors not supported for this module version")]
        ErrorNotSupported,
        #[error("Receive function schema has no return value schema")]
        NoReturnValueInReceive,
        #[error("Return values not supported for this module version")]
        ReturnValueNotSupported,
    }

    impl From<ParseError> for VersionedSchemaError {
        fn from(_: ParseError) -> Self { VersionedSchemaError::ParseError }
    }

    /// Unpacks a versioned contract schema from a versioned module schema
    fn get_versioned_contract_schema(
        versioned_module_schema: &VersionedModuleSchema,
        contract_name: &str,
    ) -> Result<VersionedContractSchema, VersionedSchemaError> {
        let versioned_contract_schema: VersionedContractSchema = match versioned_module_schema {
            VersionedModuleSchema::V0(module_schema) => {
                let contract_schema = module_schema
                    .contracts
                    .get(contract_name)
                    .ok_or(VersionedSchemaError::NoContractInModule)?
                    .clone();
                VersionedContractSchema::V0(contract_schema)
            }
            VersionedModuleSchema::V1(module_schema) => {
                let contract_schema = module_schema
                    .contracts
                    .get(contract_name)
                    .ok_or(VersionedSchemaError::NoContractInModule)?
                    .clone();
                VersionedContractSchema::V1(contract_schema)
            }
            VersionedModuleSchema::V2(module_schema) => {
                let contract_schema = module_schema
                    .contracts
                    .get(contract_name)
                    .ok_or(VersionedSchemaError::NoContractInModule)?
                    .clone();
                VersionedContractSchema::V2(contract_schema)
            }
            VersionedModuleSchema::V3(module_schema) => {
                let contract_schema = module_schema
                    .contracts
                    .get(contract_name)
                    .ok_or(VersionedSchemaError::NoContractInModule)?
                    .clone();
                VersionedContractSchema::V3(contract_schema)
            }
        };

        Ok(versioned_contract_schema)
    }

    impl VersionedModuleSchema {
        /// Get a versioned module schema. First reads header to see if the
        /// version can be discerned, otherwise tries using provided
        /// schema_version.
        pub fn new(
            schema_bytes: &[u8],
            schema_version: &Option<u8>,
        ) -> Result<Self, VersionedSchemaError> {
            let versioned_module_schema = match from_bytes::<VersionedModuleSchema>(schema_bytes) {
                Ok(versioned) => versioned,
                Err(_) => match schema_version {
                    Some(0) => VersionedModuleSchema::V0(from_bytes(schema_bytes)?),
                    Some(1) => VersionedModuleSchema::V1(from_bytes(schema_bytes)?),
                    Some(2) => VersionedModuleSchema::V2(from_bytes(schema_bytes)?),
                    Some(3) => VersionedModuleSchema::V3(from_bytes(schema_bytes)?),
                    Some(_) => return Err(VersionedSchemaError::InvalidSchemaVersion),
                    None => return Err(VersionedSchemaError::MissingSchemaVersion),
                },
            };
            Ok(versioned_module_schema)
        }

        /// Returns a receive function's parameter schema from a versioned
        /// module schema
        pub fn get_receive_param_schema(
            &self,
            contract_name: &str,
            function_name: &str,
        ) -> Result<Type, VersionedSchemaError> {
            let versioned_contract_schema = get_versioned_contract_schema(self, contract_name)?;
            let param_schema = match versioned_contract_schema {
                VersionedContractSchema::V0(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .clone(),
                VersionedContractSchema::V1(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .parameter()
                    .ok_or(VersionedSchemaError::NoParamsInReceive)?
                    .clone(),
                VersionedContractSchema::V2(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .parameter()
                    .ok_or(VersionedSchemaError::NoParamsInReceive)?
                    .clone(),
                VersionedContractSchema::V3(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .parameter()
                    .ok_or(VersionedSchemaError::NoParamsInReceive)?
                    .clone(),
            };
            Ok(param_schema)
        }

        /// Returns an init function's parameter schema from a versioned module
        /// schema
        pub fn get_init_param_schema(
            &self,
            contract_name: &str,
        ) -> Result<Type, VersionedSchemaError> {
            let versioned_contract_schema = get_versioned_contract_schema(self, contract_name)?;
            let param_schema = match versioned_contract_schema {
                VersionedContractSchema::V0(contract_schema) => contract_schema
                    .init
                    .as_ref()
                    .ok_or(VersionedSchemaError::NoInitInContract)?
                    .clone(),
                VersionedContractSchema::V1(contract_schema) => contract_schema
                    .init
                    .as_ref()
                    .ok_or(VersionedSchemaError::NoInitInContract)?
                    .parameter()
                    .ok_or(VersionedSchemaError::NoParamsInInit)?
                    .clone(),
                VersionedContractSchema::V2(contract_schema) => contract_schema
                    .init
                    .as_ref()
                    .ok_or(VersionedSchemaError::NoInitInContract)?
                    .parameter()
                    .ok_or(VersionedSchemaError::NoParamsInInit)?
                    .clone(),
                VersionedContractSchema::V3(contract_schema) => contract_schema
                    .init
                    .as_ref()
                    .ok_or(VersionedSchemaError::NoInitInContract)?
                    .parameter()
                    .ok_or(VersionedSchemaError::NoParamsInInit)?
                    .clone(),
            };
            Ok(param_schema)
        }

        /// Returns a receive function's error schema from a versioned module
        /// schema
        pub fn get_receive_error_schema(
            &self,
            contract_name: &str,
            function_name: &str,
        ) -> Result<Type, VersionedSchemaError> {
            let versioned_contract_schema = get_versioned_contract_schema(self, contract_name)?;
            let param_schema = match versioned_contract_schema {
                VersionedContractSchema::V0(_) => {
                    return Err(VersionedSchemaError::ErrorNotSupported)
                }
                VersionedContractSchema::V1(_) => {
                    return Err(VersionedSchemaError::ErrorNotSupported)
                }
                VersionedContractSchema::V2(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .error()
                    .ok_or(VersionedSchemaError::NoErrorInReceive)?
                    .clone(),
                VersionedContractSchema::V3(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .error()
                    .ok_or(VersionedSchemaError::NoErrorInReceive)?
                    .clone(),
            };
            Ok(param_schema)
        }

        /// Returns an init function's error schema from a versioned module
        /// schema
        pub fn get_init_error_schema(
            &self,
            contract_name: &str,
        ) -> Result<Type, VersionedSchemaError> {
            let versioned_contract_schema = get_versioned_contract_schema(self, contract_name)?;
            let param_schema = match versioned_contract_schema {
                VersionedContractSchema::V0(_) => {
                    return Err(VersionedSchemaError::ErrorNotSupported)
                }
                VersionedContractSchema::V1(_) => {
                    return Err(VersionedSchemaError::ErrorNotSupported)
                }
                VersionedContractSchema::V2(contract_schema) => contract_schema
                    .init
                    .as_ref()
                    .ok_or(VersionedSchemaError::NoInitInContract)?
                    .error()
                    .ok_or(VersionedSchemaError::NoErrorInInit)?
                    .clone(),
                VersionedContractSchema::V3(contract_schema) => contract_schema
                    .init
                    .as_ref()
                    .ok_or(VersionedSchemaError::NoInitInContract)?
                    .error()
                    .ok_or(VersionedSchemaError::NoErrorInInit)?
                    .clone(),
            };
            Ok(param_schema)
        }

        /// Returns the return value schema from a versioned module schema.
        pub fn get_receive_return_value_schema(
            &self,
            contract_name: &str,
            function_name: &str,
        ) -> Result<Type, VersionedSchemaError> {
            let versioned_contract_schema = get_versioned_contract_schema(self, contract_name)?;
            let return_value_schema = match versioned_contract_schema {
                VersionedContractSchema::V0(_) => {
                    return Err(VersionedSchemaError::ReturnValueNotSupported)
                }
                VersionedContractSchema::V1(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .return_value()
                    .ok_or(VersionedSchemaError::NoReturnValueInReceive)?
                    .clone(),
                VersionedContractSchema::V2(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .return_value()
                    .ok_or(VersionedSchemaError::NoReturnValueInReceive)?
                    .clone(),
                VersionedContractSchema::V3(contract_schema) => contract_schema
                    .receive
                    .get(function_name)
                    .ok_or(VersionedSchemaError::NoReceiveInContract)?
                    .return_value()
                    .ok_or(VersionedSchemaError::NoReturnValueInReceive)?
                    .clone(),
            };

            Ok(return_value_schema)
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        fn module_schema() -> VersionedModuleSchema {
            let module_bytes = hex::decode(
                "ffff02010000000c00000054657374436f6e7472616374010402030100000010000000726563656976655f66756e6374696f6e06060807",
            )
            .unwrap();
            VersionedModuleSchema::new(&module_bytes, &None).unwrap()
        }

        #[test]
        fn test_getting_init_param_schema() {
            let extracted_type = module_schema().get_init_param_schema("TestContract").unwrap();
            assert_eq!(extracted_type, Type::U8)
        }

        #[test]
        fn test_getting_receive_param_schema() {
            let extracted_type = module_schema()
                .get_receive_param_schema("TestContract", "receive_function")
                .unwrap();
            assert_eq!(extracted_type, Type::I8)
        }

        #[test]
        fn test_getting_init_error_schema() {
            let extracted_type = module_schema().get_init_error_schema("TestContract").unwrap();
            assert_eq!(extracted_type, Type::U16)
        }

        #[test]
        fn test_getting_receive_error_schema() {
            let extracted_type = module_schema()
                .get_receive_error_schema("TestContract", "receive_function")
                .unwrap();
            assert_eq!(extracted_type, Type::I16)
        }

        #[test]
        fn test_getting_receive_return_value_schema() {
            let extracted_type = module_schema()
                .get_receive_return_value_schema("TestContract", "receive_function")
                .unwrap();
            assert_eq!(extracted_type, Type::I32)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arbitrary::*;

    #[test]
    fn test_schema_serial_deserial_is_id() {
        use rand::prelude::*;
        use rand_pcg::Pcg64;

        let seed: u64 = random();
        let mut rng = Pcg64::seed_from_u64(seed);
        let mut data = [0u8; 100000];
        rng.fill_bytes(&mut data);

        let mut unstructured = Unstructured::new(&data);

        for _ in 0..10000 {
            let schema = Type::arbitrary(&mut unstructured).unwrap();

            let res = from_bytes::<Type>(&to_bytes(&schema)).unwrap();
            assert_eq!(schema, res);
        }
    }

    /// Serialize and then deserialize the input.
    fn serial_deserial<T: Serialize>(t: &T) -> ParseResult<T> { from_bytes::<T>(&to_bytes(t)) }

    #[test]
    fn test_function_v1_serial_deserial_is_id() {
        let f1 = FunctionV1::Parameter(Type::String(SizeLength::U32));
        let f2 = FunctionV1::ReturnValue(Type::U128);
        let f3 = FunctionV1::Both {
            parameter:    Type::Set(SizeLength::U8, Box::new(Type::ByteArray(10))),
            return_value: Type::ILeb128(3),
        };

        assert_eq!(serial_deserial(&f1), Ok(f1));
        assert_eq!(serial_deserial(&f2), Ok(f2));
        assert_eq!(serial_deserial(&f3), Ok(f3));
    }

    #[test]
    fn test_function_v2_serial_deserial_is_id() {
        let f1 = FunctionV2 {
            parameter:    Some(Type::String(SizeLength::U32)),
            return_value: Some(Type::String(SizeLength::U32)),
            error:        Some(Type::String(SizeLength::U32)),
        };

        assert_eq!(serial_deserial(&f1), Ok(f1));
    }

    #[test]
    fn test_module_v0_serial_deserial_is_id() {
        let m = ModuleV0 {
            contracts: BTreeMap::from([("a".into(), ContractV0 {
                init:    Some(Type::U8),
                receive: BTreeMap::from([
                    ("b".into(), Type::String(SizeLength::U32)),
                    ("c".into(), Type::Bool),
                ]),
                state:   Some(Type::String(SizeLength::U64)),
            })]),
        };

        assert_eq!(serial_deserial(&m), Ok(m));
    }

    #[test]
    fn test_module_v1_serial_deserial_is_id() {
        let m = ModuleV1 {
            contracts: BTreeMap::from([("a".into(), ContractV1 {
                init:    Some(FunctionV1::Parameter(Type::U8)),
                receive: BTreeMap::from([
                    ("b".into(), FunctionV1::ReturnValue(Type::String(SizeLength::U32))),
                    ("c".into(), FunctionV1::Both {
                        parameter:    Type::U8,
                        return_value: Type::Bool,
                    }),
                ]),
            })]),
        };

        assert_eq!(serial_deserial(&m), Ok(m));
    }

    #[test]
    fn test_module_v2_serial_deserial_is_id() {
        let m = ModuleV2 {
            contracts: BTreeMap::from([("a".into(), ContractV2 {
                init:    Some(FunctionV2 {
                    parameter:    Some(Type::String(SizeLength::U32)),
                    return_value: Some(Type::String(SizeLength::U32)),
                    error:        Some(Type::String(SizeLength::U32)),
                }),
                receive: BTreeMap::from([
                    ("b".into(), FunctionV2 {
                        parameter:    Some(Type::String(SizeLength::U32)),
                        return_value: Some(Type::String(SizeLength::U32)),
                        error:        Some(Type::String(SizeLength::U32)),
                    }),
                    ("c".into(), FunctionV2 {
                        parameter:    Some(Type::String(SizeLength::U32)),
                        return_value: Some(Type::String(SizeLength::U32)),
                        error:        Some(Type::String(SizeLength::U32)),
                    }),
                ]),
            })]),
        };

        assert_eq!(serial_deserial(&m), Ok(m));
    }

    #[test]
    fn test_module_v3_schema_serial_deserial_is_id() {
        use rand::prelude::*;
        use rand_pcg::Pcg64;

        let seed: u64 = random();
        let mut rng = Pcg64::seed_from_u64(seed);
        let mut data = [0u8; 100000];
        rng.fill_bytes(&mut data);

        let mut unstructured = Unstructured::new(&data);

        for _ in 0..10000 {
            let schema = ModuleV3::arbitrary(&mut unstructured).unwrap();

            let res = from_bytes::<ModuleV3>(&to_bytes(&schema)).unwrap();
            assert_eq!(schema, res);
        }
    }
}