@@ -11,40 +11,42 @@ pub fn query_responses_derive_impl(input: ItemEnum) -> syn::Result<ItemImpl> {
1111 let ctx = context:: get_context ( & input) ?;
1212
1313 let item_impl = if ctx. is_nested {
14+ let crate_name = & ctx. crate_name ;
1415 let ident = input. ident ;
1516 let subquery_calls = input
1617 . variants
1718 . into_iter ( )
18- . map ( parse_subquery)
19+ . map ( |variant| parse_subquery ( & ctx , variant ) )
1920 . collect :: < syn:: Result < Vec < _ > > > ( ) ?;
2021
2122 // Handle generics if the type has any
2223 let ( _, type_generics, where_clause) = input. generics . split_for_impl ( ) ;
2324 let impl_generics = impl_generics (
2425 & ctx,
2526 & input. generics ,
26- & [ parse_quote ! { :: cosmwasm_schema :: QueryResponses } ] ,
27+ & [ parse_quote ! { #crate_name :: QueryResponses } ] ,
2728 ) ;
2829
2930 let subquery_len = subquery_calls. len ( ) ;
3031 parse_quote ! {
3132 #[ automatically_derived]
3233 #[ 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 > {
3536 let subqueries = [
3637 #( #subquery_calls, ) *
3738 ] ;
38- :: cosmwasm_schema :: combine_subqueries:: <#subquery_len, #ident #type_generics>( subqueries)
39+ #crate_name :: combine_subqueries:: <#subquery_len, #ident #type_generics>( subqueries)
3940 }
4041 }
4142 }
4243 } else {
44+ let crate_name = & ctx. crate_name ;
4345 let ident = input. ident ;
4446 let mappings = input
4547 . variants
4648 . into_iter ( )
47- . map ( parse_query)
49+ . map ( |variant| parse_query ( & ctx , variant ) )
4850 . collect :: < syn:: Result < Vec < _ > > > ( ) ?;
4951
5052 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> {
5860 parse_quote ! {
5961 #[ automatically_derived]
6062 #[ 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 > {
6365 :: std:: collections:: BTreeMap :: from( [
6466 #( #mappings, ) *
6567 ] )
@@ -80,9 +82,12 @@ fn impl_generics(ctx: &Context, generics: &Generics, bounds: &[TypeParamBound])
8082 param. default = None ;
8183
8284 if !ctx. no_bounds_for . contains ( & param. ident ) {
85+ let crate_name = & ctx. crate_name ;
86+
8387 param
8488 . bounds
85- . push ( parse_quote ! { :: cosmwasm_schema:: schemars:: JsonSchema } ) ;
89+ . push ( parse_quote ! { #crate_name:: schemars:: JsonSchema } ) ;
90+
8691 param. bounds . extend ( bounds. to_owned ( ) ) ;
8792 }
8893 }
@@ -91,7 +96,8 @@ fn impl_generics(ctx: &Context, generics: &Generics, bounds: &[TypeParamBound])
9196}
9297
9398/// 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 ;
95101 let query = to_snake_case ( & v. ident . to_string ( ) ) ;
96102 let response_ty: Type = v
97103 . attrs
@@ -101,14 +107,12 @@ fn parse_query(v: Variant) -> syn::Result<(String, Expr)> {
101107 . parse_args ( )
102108 . map_err ( |e| error_message ! ( e. span( ) , "return must be a type" ) ) ?;
103109
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) ) ) )
108111}
109112
110113/// 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 ;
112116 let submsg = match v. fields {
113117 syn:: Fields :: Named ( _) => bail ! ( v, "a struct variant is not a valid subquery" ) ,
114118 syn:: Fields :: Unnamed ( fields) => {
@@ -121,7 +125,7 @@ fn parse_subquery(v: Variant) -> syn::Result<Expr> {
121125 syn:: Fields :: Unit => bail ! ( v, "a unit variant is not a valid subquery" ) ,
122126 } ;
123127
124- Ok ( parse_quote ! ( <#submsg as :: cosmwasm_schema :: QueryResponses >:: response_schemas_impl( ) ) )
128+ Ok ( parse_quote ! ( <#submsg as #crate_name :: QueryResponses >:: response_schemas_impl( ) ) )
125129}
126130
127131fn parse_tuple ( ( q, r) : ( String , Expr ) ) -> ExprTuple {
@@ -144,10 +148,20 @@ fn to_snake_case(input: &str) -> String {
144148
145149#[ cfg( test) ]
146150mod tests {
151+ use std:: collections:: HashSet ;
152+
147153 use syn:: parse_quote;
148154
149155 use super :: * ;
150156
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+
151165 #[ test]
152166 fn happy_path ( ) {
153167 let input: ItemEnum = parse_quote ! {
@@ -330,7 +344,7 @@ mod tests {
330344 } ;
331345
332346 assert_eq ! (
333- parse_tuple( parse_query( variant) . unwrap( ) ) ,
347+ parse_tuple( parse_query( & test_context ( ) , variant) . unwrap( ) ) ,
334348 parse_quote! {
335349 ( "get_foo" . to_string( ) , :: cosmwasm_schema:: schema_for!( Foo ) )
336350 }
@@ -342,7 +356,7 @@ mod tests {
342356 } ;
343357
344358 assert_eq ! (
345- parse_tuple( parse_query( variant) . unwrap( ) ) ,
359+ parse_tuple( parse_query( & test_context ( ) , variant) . unwrap( ) ) ,
346360 parse_quote! { ( "get_foo" . to_string( ) , :: cosmwasm_schema:: schema_for!( some_crate:: Foo ) ) }
347361 ) ;
348362 }
0 commit comments