@@ -11,40 +11,42 @@ pub fn query_responses_derive_impl(input: ItemEnum) -> syn::Result<ItemImpl> {
11
11
let ctx = context:: get_context ( & input) ?;
12
12
13
13
let item_impl = if ctx. is_nested {
14
+ let crate_name = & ctx. crate_name ;
14
15
let ident = input. ident ;
15
16
let subquery_calls = input
16
17
. variants
17
18
. into_iter ( )
18
- . map ( parse_subquery)
19
+ . map ( |variant| parse_subquery ( & ctx , variant ) )
19
20
. collect :: < syn:: Result < Vec < _ > > > ( ) ?;
20
21
21
22
// Handle generics if the type has any
22
23
let ( _, type_generics, where_clause) = input. generics . split_for_impl ( ) ;
23
24
let impl_generics = impl_generics (
24
25
& ctx,
25
26
& input. generics ,
26
- & [ parse_quote ! { :: cosmwasm_schema :: QueryResponses } ] ,
27
+ & [ parse_quote ! { #crate_name :: QueryResponses } ] ,
27
28
) ;
28
29
29
30
let subquery_len = subquery_calls. len ( ) ;
30
31
parse_quote ! {
31
32
#[ automatically_derived]
32
33
#[ cfg( not( target_arch = "wasm32" ) ) ]
33
- impl #impl_generics :: cosmwasm_schema :: QueryResponses for #ident #type_generics #where_clause {
34
- fn response_schemas_impl( ) -> :: std:: collections:: BTreeMap <String , :: cosmwasm_schema :: schemars:: schema:: RootSchema > {
34
+ impl #impl_generics #crate_name :: QueryResponses for #ident #type_generics #where_clause {
35
+ fn response_schemas_impl( ) -> :: std:: collections:: BTreeMap <String , #crate_name :: schemars:: schema:: RootSchema > {
35
36
let subqueries = [
36
37
#( #subquery_calls, ) *
37
38
] ;
38
- :: cosmwasm_schema :: combine_subqueries:: <#subquery_len, #ident #type_generics>( subqueries)
39
+ #crate_name :: combine_subqueries:: <#subquery_len, #ident #type_generics>( subqueries)
39
40
}
40
41
}
41
42
}
42
43
} else {
44
+ let crate_name = & ctx. crate_name ;
43
45
let ident = input. ident ;
44
46
let mappings = input
45
47
. variants
46
48
. into_iter ( )
47
- . map ( parse_query)
49
+ . map ( |variant| parse_query ( & ctx , variant ) )
48
50
. collect :: < syn:: Result < Vec < _ > > > ( ) ?;
49
51
50
52
let mut queries: Vec < _ > = mappings. clone ( ) . into_iter ( ) . map ( |( q, _) | q) . collect ( ) ;
@@ -58,8 +60,8 @@ pub fn query_responses_derive_impl(input: ItemEnum) -> syn::Result<ItemImpl> {
58
60
parse_quote ! {
59
61
#[ automatically_derived]
60
62
#[ cfg( not( target_arch = "wasm32" ) ) ]
61
- impl #impl_generics :: cosmwasm_schema :: QueryResponses for #ident #type_generics #where_clause {
62
- fn response_schemas_impl( ) -> :: std:: collections:: BTreeMap <String , :: cosmwasm_schema :: schemars:: schema:: RootSchema > {
63
+ impl #impl_generics #crate_name :: QueryResponses for #ident #type_generics #where_clause {
64
+ fn response_schemas_impl( ) -> :: std:: collections:: BTreeMap <String , #crate_name :: schemars:: schema:: RootSchema > {
63
65
:: std:: collections:: BTreeMap :: from( [
64
66
#( #mappings, ) *
65
67
] )
@@ -80,9 +82,12 @@ fn impl_generics(ctx: &Context, generics: &Generics, bounds: &[TypeParamBound])
80
82
param. default = None ;
81
83
82
84
if !ctx. no_bounds_for . contains ( & param. ident ) {
85
+ let crate_name = & ctx. crate_name ;
86
+
83
87
param
84
88
. bounds
85
- . push ( parse_quote ! { :: cosmwasm_schema:: schemars:: JsonSchema } ) ;
89
+ . push ( parse_quote ! { #crate_name:: schemars:: JsonSchema } ) ;
90
+
86
91
param. bounds . extend ( bounds. to_owned ( ) ) ;
87
92
}
88
93
}
@@ -91,7 +96,8 @@ fn impl_generics(ctx: &Context, generics: &Generics, bounds: &[TypeParamBound])
91
96
}
92
97
93
98
/// Extract the query -> response mapping out of an enum variant.
94
- fn parse_query ( v : Variant ) -> syn:: Result < ( String , Expr ) > {
99
+ fn parse_query ( ctx : & Context , v : Variant ) -> syn:: Result < ( String , Expr ) > {
100
+ let crate_name = & ctx. crate_name ;
95
101
let query = to_snake_case ( & v. ident . to_string ( ) ) ;
96
102
let response_ty: Type = v
97
103
. attrs
@@ -101,14 +107,12 @@ fn parse_query(v: Variant) -> syn::Result<(String, Expr)> {
101
107
. parse_args ( )
102
108
. map_err ( |e| error_message ! ( e. span( ) , "return must be a type" ) ) ?;
103
109
104
- Ok ( (
105
- query,
106
- parse_quote ! ( :: cosmwasm_schema:: schema_for!( #response_ty) ) ,
107
- ) )
110
+ Ok ( ( query, parse_quote ! ( #crate_name:: schema_for!( #response_ty) ) ) )
108
111
}
109
112
110
113
/// Extract the nested query -> response mapping out of an enum variant.
111
- fn parse_subquery ( v : Variant ) -> syn:: Result < Expr > {
114
+ fn parse_subquery ( ctx : & Context , v : Variant ) -> syn:: Result < Expr > {
115
+ let crate_name = & ctx. crate_name ;
112
116
let submsg = match v. fields {
113
117
syn:: Fields :: Named ( _) => bail ! ( v, "a struct variant is not a valid subquery" ) ,
114
118
syn:: Fields :: Unnamed ( fields) => {
@@ -121,7 +125,7 @@ fn parse_subquery(v: Variant) -> syn::Result<Expr> {
121
125
syn:: Fields :: Unit => bail ! ( v, "a unit variant is not a valid subquery" ) ,
122
126
} ;
123
127
124
- Ok ( parse_quote ! ( <#submsg as :: cosmwasm_schema :: QueryResponses >:: response_schemas_impl( ) ) )
128
+ Ok ( parse_quote ! ( <#submsg as #crate_name :: QueryResponses >:: response_schemas_impl( ) ) )
125
129
}
126
130
127
131
fn parse_tuple ( ( q, r) : ( String , Expr ) ) -> ExprTuple {
@@ -144,10 +148,20 @@ fn to_snake_case(input: &str) -> String {
144
148
145
149
#[ cfg( test) ]
146
150
mod tests {
151
+ use std:: collections:: HashSet ;
152
+
147
153
use syn:: parse_quote;
148
154
149
155
use super :: * ;
150
156
157
+ fn test_context ( ) -> Context {
158
+ Context {
159
+ crate_name : parse_quote ! ( :: cosmwasm_schema) ,
160
+ is_nested : false ,
161
+ no_bounds_for : HashSet :: new ( ) ,
162
+ }
163
+ }
164
+
151
165
#[ test]
152
166
fn happy_path ( ) {
153
167
let input: ItemEnum = parse_quote ! {
@@ -330,7 +344,7 @@ mod tests {
330
344
} ;
331
345
332
346
assert_eq ! (
333
- parse_tuple( parse_query( variant) . unwrap( ) ) ,
347
+ parse_tuple( parse_query( & test_context ( ) , variant) . unwrap( ) ) ,
334
348
parse_quote! {
335
349
( "get_foo" . to_string( ) , :: cosmwasm_schema:: schema_for!( Foo ) )
336
350
}
@@ -342,7 +356,7 @@ mod tests {
342
356
} ;
343
357
344
358
assert_eq ! (
345
- parse_tuple( parse_query( variant) . unwrap( ) ) ,
359
+ parse_tuple( parse_query( & test_context ( ) , variant) . unwrap( ) ) ,
346
360
parse_quote! { ( "get_foo" . to_string( ) , :: cosmwasm_schema:: schema_for!( some_crate:: Foo ) ) }
347
361
) ;
348
362
}
0 commit comments