1
1
use crate :: error:: bail;
2
2
use quote:: { quote, ToTokens } ;
3
- use syn:: DeriveInput ;
3
+ use syn:: {
4
+ parse:: { Parse , ParseStream } ,
5
+ parse_quote,
6
+ punctuated:: Punctuated ,
7
+ DeriveInput , MetaNameValue , Token ,
8
+ } ;
9
+
10
+ pub struct Options {
11
+ crate_path : syn:: Path ,
12
+ }
13
+
14
+ impl Default for Options {
15
+ fn default ( ) -> Self {
16
+ Self {
17
+ crate_path : parse_quote ! ( :: cosmwasm_schema) ,
18
+ }
19
+ }
20
+ }
21
+
22
+ impl Parse for Options {
23
+ fn parse ( input : ParseStream ) -> syn:: Result < Self > {
24
+ let mut acc = Self :: default ( ) ;
25
+ let params = Punctuated :: < MetaNameValue , Token ! [ , ] > :: parse_terminated ( input) ?;
26
+ for param in params {
27
+ if param. path . is_ident ( "crate" ) {
28
+ let path_as_string: syn:: LitStr = syn:: parse2 ( param. value . to_token_stream ( ) ) ?;
29
+ acc. crate_path = path_as_string. parse ( ) ?
30
+ } else {
31
+ bail ! ( param, "unknown option" ) ;
32
+ }
33
+ }
34
+
35
+ Ok ( acc)
36
+ }
37
+ }
38
+
39
+ pub fn cw_serde_impl ( options : Options , input : DeriveInput ) -> syn:: Result < DeriveInput > {
40
+ let crate_path = & options. crate_path ;
41
+ let crate_path_displayable = crate_path. to_token_stream ( ) ;
42
+ let serde_path = format ! ( "{crate_path_displayable}::serde" ) ;
43
+ let schemars_path = format ! ( "{crate_path_displayable}::schemars" ) ;
4
44
5
- pub fn cw_serde_impl ( input : DeriveInput ) -> syn:: Result < DeriveInput > {
6
45
let mut stream = quote ! {
7
46
#[ derive(
8
- :: cosmwasm_schema :: serde:: Serialize ,
9
- :: cosmwasm_schema :: serde:: Deserialize ,
47
+ #crate_path :: serde:: Serialize ,
48
+ #crate_path :: serde:: Deserialize ,
10
49
:: std:: clone:: Clone ,
11
50
:: std:: fmt:: Debug ,
12
51
:: std:: cmp:: PartialEq ,
13
- :: cosmwasm_schema :: schemars:: JsonSchema
52
+ #crate_path :: schemars:: JsonSchema
14
53
) ]
15
54
#[ allow( clippy:: derive_partial_eq_without_eq) ] // Allow users of `#[cw_serde]` to not implement Eq without clippy complaining
16
- #[ serde( deny_unknown_fields, crate = "::cosmwasm_schema::serde" ) ]
17
- #[ schemars( crate = "::cosmwasm_schema::schemars" ) ]
55
+ #[ serde( deny_unknown_fields, crate = #serde_path ) ]
56
+ #[ schemars( crate = #schemars_path ) ]
18
57
} ;
19
58
20
59
match input. data {
@@ -35,13 +74,52 @@ mod tests {
35
74
use syn:: parse_quote;
36
75
37
76
#[ test]
38
- fn structs ( ) {
39
- let expanded = cw_serde_impl ( parse_quote ! {
77
+ fn crate_rename ( ) {
78
+ let expanded = cw_serde_impl (
79
+ Options {
80
+ crate_path : parse_quote ! ( :: my_crate:: cw_schema) ,
81
+ } ,
82
+ parse_quote ! {
83
+ pub struct InstantiateMsg {
84
+ pub verifier: String ,
85
+ pub beneficiary: String ,
86
+ }
87
+ } ,
88
+ )
89
+ . unwrap ( ) ;
90
+
91
+ let expected = parse_quote ! {
92
+ #[ derive(
93
+ :: my_crate:: cw_schema:: serde:: Serialize ,
94
+ :: my_crate:: cw_schema:: serde:: Deserialize ,
95
+ :: std:: clone:: Clone ,
96
+ :: std:: fmt:: Debug ,
97
+ :: std:: cmp:: PartialEq ,
98
+ :: my_crate:: cw_schema:: schemars:: JsonSchema
99
+ ) ]
100
+ #[ allow( clippy:: derive_partial_eq_without_eq) ]
101
+ #[ serde( deny_unknown_fields, crate = ":: my_crate :: cw_schema::serde" ) ]
102
+ #[ schemars( crate = ":: my_crate :: cw_schema::schemars" ) ]
40
103
pub struct InstantiateMsg {
41
104
pub verifier: String ,
42
105
pub beneficiary: String ,
43
106
}
44
- } )
107
+ } ;
108
+
109
+ assert_eq ! ( expanded, expected) ;
110
+ }
111
+
112
+ #[ test]
113
+ fn structs ( ) {
114
+ let expanded = cw_serde_impl (
115
+ Options :: default ( ) ,
116
+ parse_quote ! {
117
+ pub struct InstantiateMsg {
118
+ pub verifier: String ,
119
+ pub beneficiary: String ,
120
+ }
121
+ } ,
122
+ )
45
123
. unwrap ( ) ;
46
124
47
125
let expected = parse_quote ! {
@@ -54,8 +132,8 @@ mod tests {
54
132
:: cosmwasm_schema:: schemars:: JsonSchema
55
133
) ]
56
134
#[ allow( clippy:: derive_partial_eq_without_eq) ]
57
- #[ serde( deny_unknown_fields, crate = "::cosmwasm_schema::serde" ) ]
58
- #[ schemars( crate = "::cosmwasm_schema::schemars" ) ]
135
+ #[ serde( deny_unknown_fields, crate = ":: cosmwasm_schema::serde" ) ]
136
+ #[ schemars( crate = ":: cosmwasm_schema::schemars" ) ]
59
137
pub struct InstantiateMsg {
60
138
pub verifier: String ,
61
139
pub beneficiary: String ,
@@ -67,9 +145,12 @@ mod tests {
67
145
68
146
#[ test]
69
147
fn empty_struct ( ) {
70
- let expanded = cw_serde_impl ( parse_quote ! {
71
- pub struct InstantiateMsg { }
72
- } )
148
+ let expanded = cw_serde_impl (
149
+ Options :: default ( ) ,
150
+ parse_quote ! {
151
+ pub struct InstantiateMsg { }
152
+ } ,
153
+ )
73
154
. unwrap ( ) ;
74
155
75
156
let expected = parse_quote ! {
@@ -82,8 +163,8 @@ mod tests {
82
163
:: cosmwasm_schema:: schemars:: JsonSchema
83
164
) ]
84
165
#[ allow( clippy:: derive_partial_eq_without_eq) ]
85
- #[ serde( deny_unknown_fields, crate = "::cosmwasm_schema::serde" ) ]
86
- #[ schemars( crate = "::cosmwasm_schema::schemars" ) ]
166
+ #[ serde( deny_unknown_fields, crate = ":: cosmwasm_schema::serde" ) ]
167
+ #[ schemars( crate = ":: cosmwasm_schema::schemars" ) ]
87
168
pub struct InstantiateMsg { }
88
169
} ;
89
170
@@ -92,14 +173,17 @@ mod tests {
92
173
93
174
#[ test]
94
175
fn enums ( ) {
95
- let expanded = cw_serde_impl ( parse_quote ! {
96
- pub enum SudoMsg {
97
- StealFunds {
98
- recipient: String ,
99
- amount: Vec <Coin >,
100
- } ,
101
- }
102
- } )
176
+ let expanded = cw_serde_impl (
177
+ Options :: default ( ) ,
178
+ parse_quote ! {
179
+ pub enum SudoMsg {
180
+ StealFunds {
181
+ recipient: String ,
182
+ amount: Vec <Coin >,
183
+ } ,
184
+ }
185
+ } ,
186
+ )
103
187
. unwrap ( ) ;
104
188
105
189
let expected = parse_quote ! {
@@ -112,8 +196,8 @@ mod tests {
112
196
:: cosmwasm_schema:: schemars:: JsonSchema
113
197
) ]
114
198
#[ allow( clippy:: derive_partial_eq_without_eq) ]
115
- #[ serde( deny_unknown_fields, crate = "::cosmwasm_schema::serde" ) ]
116
- #[ schemars( crate = "::cosmwasm_schema::schemars" ) ]
199
+ #[ serde( deny_unknown_fields, crate = ":: cosmwasm_schema::serde" ) ]
200
+ #[ schemars( crate = ":: cosmwasm_schema::schemars" ) ]
117
201
#[ serde( rename_all = "snake_case" ) ]
118
202
pub enum SudoMsg {
119
203
StealFunds {
@@ -129,12 +213,15 @@ mod tests {
129
213
#[ test]
130
214
#[ should_panic( expected = "unions are not supported" ) ]
131
215
fn unions ( ) {
132
- cw_serde_impl ( parse_quote ! {
133
- pub union SudoMsg {
134
- x: u32 ,
135
- y: u32 ,
136
- }
137
- } )
216
+ cw_serde_impl (
217
+ Options :: default ( ) ,
218
+ parse_quote ! {
219
+ pub union SudoMsg {
220
+ x: u32 ,
221
+ y: u32 ,
222
+ }
223
+ } ,
224
+ )
138
225
. unwrap ( ) ;
139
226
}
140
227
}
0 commit comments