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 {
@@ -35,13 +74,52 @@ mod tests {
3574 use syn:: parse_quote;
3675
3776 #[ 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" ) ]
40103 pub struct InstantiateMsg {
41104 pub verifier: String ,
42105 pub beneficiary: String ,
43106 }
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+ )
45123 . unwrap ( ) ;
46124
47125 let expected = parse_quote ! {
@@ -54,8 +132,8 @@ mod tests {
54132 :: cosmwasm_schema:: schemars:: JsonSchema
55133 ) ]
56134 #[ 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" ) ]
59137 pub struct InstantiateMsg {
60138 pub verifier: String ,
61139 pub beneficiary: String ,
@@ -67,9 +145,12 @@ mod tests {
67145
68146 #[ test]
69147 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+ )
73154 . unwrap ( ) ;
74155
75156 let expected = parse_quote ! {
@@ -82,8 +163,8 @@ mod tests {
82163 :: cosmwasm_schema:: schemars:: JsonSchema
83164 ) ]
84165 #[ 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" ) ]
87168 pub struct InstantiateMsg { }
88169 } ;
89170
@@ -92,14 +173,17 @@ mod tests {
92173
93174 #[ test]
94175 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+ )
103187 . unwrap ( ) ;
104188
105189 let expected = parse_quote ! {
@@ -112,8 +196,8 @@ mod tests {
112196 :: cosmwasm_schema:: schemars:: JsonSchema
113197 ) ]
114198 #[ 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" ) ]
117201 #[ serde( rename_all = "snake_case" ) ]
118202 pub enum SudoMsg {
119203 StealFunds {
@@ -129,12 +213,15 @@ mod tests {
129213 #[ test]
130214 #[ should_panic( expected = "unions are not supported" ) ]
131215 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+ )
138225 . unwrap ( ) ;
139226 }
140227}
0 commit comments