11use crate :: error:: bail;
22use 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" ) ;
444
5- pub fn cw_serde_impl ( input : DeriveInput ) -> syn:: Result < DeriveInput > {
645 let mut stream = quote ! {
746 #[ derive(
8- :: cosmwasm_schema :: serde:: Serialize ,
9- :: cosmwasm_schema :: serde:: Deserialize ,
47+ #crate_path :: serde:: Serialize ,
48+ #crate_path :: serde:: Deserialize ,
1049 :: std:: clone:: Clone ,
1150 :: std:: fmt:: Debug ,
1251 :: std:: cmp:: PartialEq ,
13- :: cosmwasm_schema :: schemars:: JsonSchema
52+ #crate_path :: schemars:: JsonSchema
1453 ) ]
1554 #[ 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 ) ]
1857 } ;
1958
2059 match input. data {
@@ -36,12 +75,15 @@ mod tests {
3675
3776 #[ test]
3877 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+ )
4587 . unwrap ( ) ;
4688
4789 let expected = parse_quote ! {
@@ -67,9 +109,12 @@ mod tests {
67109
68110 #[ test]
69111 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+ )
73118 . unwrap ( ) ;
74119
75120 let expected = parse_quote ! {
@@ -92,14 +137,17 @@ mod tests {
92137
93138 #[ test]
94139 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+ )
103151 . unwrap ( ) ;
104152
105153 let expected = parse_quote ! {
@@ -129,12 +177,15 @@ mod tests {
129177 #[ test]
130178 #[ should_panic( expected = "unions are not supported" ) ]
131179 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+ )
138189 . unwrap ( ) ;
139190 }
140191}
0 commit comments