@@ -2,28 +2,65 @@ use cw_schema::Schemaifier;
2
2
use serde:: { Deserialize , Serialize } ;
3
3
use std:: io:: Write ;
4
4
5
- #[ derive( Schemaifier , Serialize , Deserialize ) ]
5
+ /// This is a struct level documentation for enum type
6
+ #[ derive( Schemaifier , Serialize , Deserialize , PartialEq , Debug ) ]
6
7
pub enum SomeEnum {
8
+ /// Field1 docs
7
9
Field1 ,
10
+
11
+ /// Field2 docs
8
12
Field2 ( u32 , u32 ) ,
9
- Field3 { a : String , b : u32 } ,
10
- // Field4(Box<SomeEnum>), // TODO tkulik: Do we want to support Box<T> ?
11
- // Field5 { a: Box<SomeEnum> },
13
+
14
+ /// Field3 docs
15
+ Field3 {
16
+ /// `a` field docs
17
+ a : String ,
18
+
19
+ /// `b` field docs
20
+ b : u32 ,
21
+ } ,
12
22
}
13
23
14
- #[ derive( Schemaifier , Serialize , Deserialize ) ]
24
+ /// This is a struct level documentation for unit struct
25
+ #[ derive( Schemaifier , Serialize , Deserialize , PartialEq , Debug ) ]
15
26
pub struct UnitStructure ;
16
27
17
- #[ derive( Schemaifier , Serialize , Deserialize ) ]
28
+ /// This is a struct level documentation for tuple
29
+ #[ derive( Schemaifier , Serialize , Deserialize , PartialEq , Debug ) ]
18
30
pub struct TupleStructure ( u32 , String , u128 ) ;
19
31
20
- #[ derive( Schemaifier , Serialize , Deserialize ) ]
32
+ /// This is a struct level documentation for named structure
33
+ #[ derive( Schemaifier , Serialize , Deserialize , PartialEq , Debug ) ]
21
34
pub struct NamedStructure {
35
+ /// `a` field docs
22
36
a : String ,
37
+
38
+ /// `b` field docs
23
39
b : u8 ,
40
+
41
+ /// `c` field docs
24
42
c : SomeEnum ,
25
43
}
26
44
45
+ #[ derive( Schemaifier , Serialize , Deserialize , PartialEq , Debug ) ]
46
+ pub struct AllSimpleTypesAndDocs {
47
+ array_field : Vec < String > ,
48
+ float_field : f32 ,
49
+ double_field : f64 ,
50
+ bool_field : bool ,
51
+ string_field : String ,
52
+ int_field : i64 ,
53
+ bytes_field : cosmwasm_std:: Binary ,
54
+ opt_field : Option < String > ,
55
+ byte_field : u8 ,
56
+ decimal_field : cosmwasm_std:: Decimal ,
57
+ address_field : cosmwasm_std:: Addr ,
58
+ checksum_field : cosmwasm_std:: Checksum ,
59
+ hexbinary_field : cosmwasm_std:: HexBinary ,
60
+ timestamp_field : cosmwasm_std:: Timestamp ,
61
+ unit_field : ( ) ,
62
+ }
63
+
27
64
#[ test]
28
65
fn simple_enum ( ) {
29
66
// generate the schemas for each of the above types
@@ -55,61 +92,63 @@ fn simple_enum() {
55
92
}
56
93
57
94
macro_rules! validator {
58
- ( $typ: ty) => { {
59
- let a: Box <dyn FnOnce ( & str ) > = Box :: new( |output| {
60
- serde_json:: from_str:: <$typ>( output) . unwrap( ) ;
61
- } ) ;
62
- a
95
+ ( $typ: ty, $example: expr) => { {
96
+ (
97
+ stringify!( $typ) ,
98
+ cw_schema:: schema_of:: <$typ>( ) ,
99
+ serde_json:: to_string( & $example) . unwrap( ) ,
100
+ {
101
+ let a: Box <dyn FnOnce ( & str ) > = Box :: new( |output| {
102
+ let result = serde_json:: from_str:: <$typ>( output) . unwrap( ) ;
103
+ assert_eq!( result, $example) ;
104
+ } ) ;
105
+ a
106
+ } ,
107
+ )
63
108
} } ;
64
109
}
65
110
66
111
#[ test]
67
112
fn assert_validity ( ) {
68
113
let schemas = [
69
- (
70
- "SomeEnum" ,
71
- cw_schema:: schema_of :: < SomeEnum > ( ) ,
72
- serde_json:: to_string ( & SomeEnum :: Field1 ) . unwrap ( ) ,
73
- validator ! ( SomeEnum ) ,
74
- ) ,
75
- (
76
- "SomeEnum" ,
77
- cw_schema:: schema_of :: < SomeEnum > ( ) ,
78
- serde_json:: to_string ( & SomeEnum :: Field2 ( 10 , 23 ) ) . unwrap ( ) ,
79
- validator ! ( SomeEnum ) ,
80
- ) ,
81
- (
82
- "SomeEnum" ,
83
- cw_schema:: schema_of :: < SomeEnum > ( ) ,
84
- serde_json:: to_string ( & SomeEnum :: Field3 {
114
+ validator ! ( SomeEnum , SomeEnum :: Field1 ) ,
115
+ validator ! ( SomeEnum , SomeEnum :: Field2 ( 10 , 23 ) ) ,
116
+ validator ! (
117
+ SomeEnum ,
118
+ SomeEnum :: Field3 {
85
119
a: "sdf" . to_string( ) ,
86
120
b: 12 ,
87
- } )
88
- . unwrap ( ) ,
89
- validator ! ( SomeEnum ) ,
90
- ) ,
91
- (
92
- "UnitStructure" ,
93
- cw_schema:: schema_of :: < UnitStructure > ( ) ,
94
- serde_json:: to_string ( & UnitStructure { } ) . unwrap ( ) ,
95
- validator ! ( UnitStructure ) ,
121
+ }
96
122
) ,
97
- (
98
- "TupleStructure" ,
99
- cw_schema:: schema_of :: < TupleStructure > ( ) ,
100
- serde_json:: to_string ( & TupleStructure ( 10 , "aasdf" . to_string ( ) , 2 ) ) . unwrap ( ) ,
101
- validator ! ( TupleStructure ) ,
102
- ) ,
103
- (
104
- "NamedStructure" ,
105
- cw_schema:: schema_of :: < NamedStructure > ( ) ,
106
- serde_json:: to_string ( & NamedStructure {
123
+ validator ! ( UnitStructure , UnitStructure { } ) ,
124
+ validator ! ( TupleStructure , TupleStructure ( 10 , "aasdf" . to_string( ) , 2 ) ) ,
125
+ validator ! (
126
+ NamedStructure ,
127
+ NamedStructure {
107
128
a: "awer" . to_string( ) ,
108
129
b: 4 ,
109
130
c: SomeEnum :: Field1 ,
110
- } )
111
- . unwrap ( ) ,
112
- validator ! ( NamedStructure ) ,
131
+ }
132
+ ) ,
133
+ validator ! (
134
+ AllSimpleTypesAndDocs ,
135
+ AllSimpleTypesAndDocs {
136
+ array_field: vec![ "abc" . to_string( ) , "def" . to_string( ) ] ,
137
+ float_field: 10.2 ,
138
+ double_field: 10.232323 ,
139
+ bool_field: true ,
140
+ string_field: "sdfsdf" . to_string( ) ,
141
+ int_field: -10 ,
142
+ bytes_field: cosmwasm_std:: Binary :: new( vec![ 0x1 , 0x2 , 0x3 ] ) ,
143
+ opt_field: Some ( "sdfsdfwer" . to_string( ) ) ,
144
+ byte_field: 9 ,
145
+ decimal_field: cosmwasm_std:: Decimal :: one( ) ,
146
+ address_field: cosmwasm_std:: Addr :: unchecked( "some_address" ) ,
147
+ checksum_field: cosmwasm_std:: Checksum :: generate( & [ 0x10 ] ) ,
148
+ hexbinary_field: cosmwasm_std:: HexBinary :: from_hex( "FAFAFA" ) . unwrap( ) ,
149
+ timestamp_field: cosmwasm_std:: Timestamp :: from_seconds( 100 ) ,
150
+ unit_field: ( ) ,
151
+ }
113
152
) ,
114
153
] ;
115
154
0 commit comments