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 {
@@ -36,12 +75,15 @@ mod tests {
36
75
37
76
#[ test]
38
77
fn structs ( ) {
39
- let expanded = cw_serde_impl ( parse_quote ! {
40
- pub struct InstantiateMsg {
41
- pub verifier: String ,
42
- pub beneficiary: String ,
43
- }
44
- } )
78
+ let expanded = cw_serde_impl (
79
+ Options :: default ( ) ,
80
+ parse_quote ! {
81
+ pub struct InstantiateMsg {
82
+ pub verifier: String ,
83
+ pub beneficiary: String ,
84
+ }
85
+ } ,
86
+ )
45
87
. unwrap ( ) ;
46
88
47
89
let expected = parse_quote ! {
@@ -67,9 +109,12 @@ mod tests {
67
109
68
110
#[ test]
69
111
fn empty_struct ( ) {
70
- let expanded = cw_serde_impl ( parse_quote ! {
71
- pub struct InstantiateMsg { }
72
- } )
112
+ let expanded = cw_serde_impl (
113
+ Options :: default ( ) ,
114
+ parse_quote ! {
115
+ pub struct InstantiateMsg { }
116
+ } ,
117
+ )
73
118
. unwrap ( ) ;
74
119
75
120
let expected = parse_quote ! {
@@ -92,14 +137,17 @@ mod tests {
92
137
93
138
#[ test]
94
139
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
- } )
140
+ let expanded = cw_serde_impl (
141
+ Options :: default ( ) ,
142
+ parse_quote ! {
143
+ pub enum SudoMsg {
144
+ StealFunds {
145
+ recipient: String ,
146
+ amount: Vec <Coin >,
147
+ } ,
148
+ }
149
+ } ,
150
+ )
103
151
. unwrap ( ) ;
104
152
105
153
let expected = parse_quote ! {
@@ -129,12 +177,15 @@ mod tests {
129
177
#[ test]
130
178
#[ should_panic( expected = "unions are not supported" ) ]
131
179
fn unions ( ) {
132
- cw_serde_impl ( parse_quote ! {
133
- pub union SudoMsg {
134
- x: u32 ,
135
- y: u32 ,
136
- }
137
- } )
180
+ cw_serde_impl (
181
+ Options :: default ( ) ,
182
+ parse_quote ! {
183
+ pub union SudoMsg {
184
+ x: u32 ,
185
+ y: u32 ,
186
+ }
187
+ } ,
188
+ )
138
189
. unwrap ( ) ;
139
190
}
140
191
}
0 commit comments