6
6
// option. This file may not be copied, modified, or distributed
7
7
// except according to those terms.
8
8
9
- use crate :: { errors, operation:: Operation , Constructor , Error , Event , Function } ;
9
+ use alloc:: collections:: { btree_map:: Values , BTreeMap } ;
10
+ #[ cfg( feature = "full-serde" ) ]
11
+ use core:: fmt;
12
+ use core:: iter:: Flatten ;
13
+ #[ cfg( feature = "full-serde" ) ]
14
+ use std:: io;
15
+
16
+ #[ cfg( feature = "full-serde" ) ]
10
17
use serde:: {
11
18
de:: { SeqAccess , Visitor } ,
12
19
ser:: SerializeSeq ,
13
20
Deserialize , Deserializer , Serialize , Serializer ,
14
21
} ;
15
- use std:: {
16
- collections:: { hash_map:: Values , HashMap } ,
17
- fmt, io,
18
- iter:: Flatten ,
19
- } ;
22
+
23
+ #[ cfg( not( feature = "std" ) ) ]
24
+ use crate :: no_std_prelude:: * ;
25
+ #[ cfg( feature = "full-serde" ) ]
26
+ use crate :: operation:: Operation ;
27
+ use crate :: { errors, Constructor , Error , Event , Function } ;
20
28
21
29
/// API building calls to contracts ABI.
22
30
#[ derive( Clone , Debug , Default , PartialEq ) ]
23
31
pub struct Contract {
24
32
/// Contract constructor.
25
33
pub constructor : Option < Constructor > ,
26
34
/// Contract functions.
27
- pub functions : HashMap < String , Vec < Function > > ,
35
+ pub functions : BTreeMap < String , Vec < Function > > ,
28
36
/// Contract events, maps signature to event.
29
- pub events : HashMap < String , Vec < Event > > ,
37
+ pub events : BTreeMap < String , Vec < Event > > ,
30
38
/// Contract has receive function.
31
39
pub receive : bool ,
32
40
/// Contract has fallback function.
33
41
pub fallback : bool ,
34
42
}
35
43
44
+ #[ cfg( feature = "full-serde" ) ]
36
45
impl < ' a > Deserialize < ' a > for Contract {
37
46
fn deserialize < D > ( deserializer : D ) -> Result < Contract , D :: Error >
38
47
where
@@ -42,8 +51,10 @@ impl<'a> Deserialize<'a> for Contract {
42
51
}
43
52
}
44
53
54
+ #[ cfg( feature = "full-serde" ) ]
45
55
struct ContractVisitor ;
46
56
57
+ #[ cfg( feature = "full-serde" ) ]
47
58
impl < ' a > Visitor < ' a > for ContractVisitor {
48
59
type Value = Contract ;
49
60
@@ -80,6 +91,7 @@ impl<'a> Visitor<'a> for ContractVisitor {
80
91
}
81
92
}
82
93
94
+ #[ cfg( feature = "full-serde" ) ]
83
95
impl Serialize for Contract {
84
96
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
85
97
where
@@ -137,6 +149,7 @@ impl Serialize for Contract {
137
149
138
150
impl Contract {
139
151
/// Loads contract from json.
152
+ #[ cfg( feature = "full-serde" ) ]
140
153
pub fn load < T : io:: Read > ( reader : T ) -> errors:: Result < Self > {
141
154
serde_json:: from_reader ( reader) . map_err ( From :: from)
142
155
}
@@ -200,11 +213,11 @@ impl<'a> Iterator for Events<'a> {
200
213
}
201
214
}
202
215
203
- #[ cfg( test) ]
216
+ #[ cfg( all ( test, feature = "full-serde" ) ) ]
204
217
#[ allow( deprecated) ]
205
218
mod test {
206
219
use crate :: { tests:: assert_ser_de, Constructor , Contract , Event , EventParam , Function , Param , ParamType } ;
207
- use std:: { collections:: HashMap , iter:: FromIterator } ;
220
+ use std:: { collections:: BTreeMap , iter:: FromIterator } ;
208
221
209
222
#[ test]
210
223
fn empty ( ) {
@@ -216,8 +229,8 @@ mod test {
216
229
deserialized,
217
230
Contract {
218
231
constructor: None ,
219
- functions: HashMap :: new( ) ,
220
- events: HashMap :: new( ) ,
232
+ functions: BTreeMap :: new( ) ,
233
+ events: BTreeMap :: new( ) ,
221
234
receive: false ,
222
235
fallback: false ,
223
236
}
@@ -250,8 +263,8 @@ mod test {
250
263
constructor: Some ( Constructor {
251
264
inputs: vec![ Param { name: "a" . to_string( ) , kind: ParamType :: Address , internal_type: None } ]
252
265
} ) ,
253
- functions: HashMap :: new( ) ,
254
- events: HashMap :: new( ) ,
266
+ functions: BTreeMap :: new( ) ,
267
+ events: BTreeMap :: new( ) ,
255
268
receive: false ,
256
269
fallback: false ,
257
270
}
@@ -295,7 +308,7 @@ mod test {
295
308
deserialized,
296
309
Contract {
297
310
constructor: None ,
298
- functions: HashMap :: from_iter( vec![
311
+ functions: BTreeMap :: from_iter( vec![
299
312
(
300
313
"foo" . to_string( ) ,
301
314
vec![ Function {
@@ -325,7 +338,7 @@ mod test {
325
338
} ]
326
339
)
327
340
] ) ,
328
- events: HashMap :: new( ) ,
341
+ events: BTreeMap :: new( ) ,
329
342
receive: false ,
330
343
fallback: false ,
331
344
}
@@ -369,7 +382,7 @@ mod test {
369
382
deserialized,
370
383
Contract {
371
384
constructor: None ,
372
- functions: HashMap :: from_iter( vec![ (
385
+ functions: BTreeMap :: from_iter( vec![ (
373
386
"foo" . to_string( ) ,
374
387
vec![
375
388
Function {
@@ -396,7 +409,7 @@ mod test {
396
409
}
397
410
]
398
411
) ] ) ,
399
- events: HashMap :: new( ) ,
412
+ events: BTreeMap :: new( ) ,
400
413
receive: false ,
401
414
fallback: false ,
402
415
}
@@ -441,8 +454,8 @@ mod test {
441
454
deserialized,
442
455
Contract {
443
456
constructor: None ,
444
- functions: HashMap :: new( ) ,
445
- events: HashMap :: from_iter( vec![
457
+ functions: BTreeMap :: new( ) ,
458
+ events: BTreeMap :: from_iter( vec![
446
459
(
447
460
"foo" . to_string( ) ,
448
461
vec![ Event {
@@ -508,8 +521,8 @@ mod test {
508
521
deserialized,
509
522
Contract {
510
523
constructor: None ,
511
- functions: HashMap :: new( ) ,
512
- events: HashMap :: from_iter( vec![ (
524
+ functions: BTreeMap :: new( ) ,
525
+ events: BTreeMap :: from_iter( vec![ (
513
526
"foo" . to_string( ) ,
514
527
vec![
515
528
Event {
@@ -550,8 +563,8 @@ mod test {
550
563
deserialized,
551
564
Contract {
552
565
constructor: None ,
553
- functions: HashMap :: new( ) ,
554
- events: HashMap :: new( ) ,
566
+ functions: BTreeMap :: new( ) ,
567
+ events: BTreeMap :: new( ) ,
555
568
receive: true ,
556
569
fallback: false ,
557
570
}
@@ -574,8 +587,8 @@ mod test {
574
587
deserialized,
575
588
Contract {
576
589
constructor: None ,
577
- functions: HashMap :: new( ) ,
578
- events: HashMap :: new( ) ,
590
+ functions: BTreeMap :: new( ) ,
591
+ events: BTreeMap :: new( ) ,
579
592
receive: false ,
580
593
fallback: true ,
581
594
}
0 commit comments