Skip to content

Commit 90c726e

Browse files
committed
Make crate for QueryResponses configurable, fix generate_api macro
1 parent 30b4627 commit 90c726e

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

packages/schema-derive/src/generate_api.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ impl Parse for Options {
171171
let instantiate = match map.remove(&parse_quote!(instantiate)) {
172172
Some(ty) => {
173173
let ty = ty.get_type()?;
174-
quote! {Some(::cosmwasm_schema::schema_for!(#ty))}
174+
quote! {Some(#crate_name::schema_for!(#ty))}
175175
}
176176
None => quote! { None },
177177
};
178178

179179
let execute = match map.remove(&parse_quote!(execute)) {
180180
Some(ty) => {
181181
let ty = ty.get_type()?;
182-
quote! {Some(::cosmwasm_schema::schema_for!(#ty))}
182+
quote! {Some(#crate_name::schema_for!(#ty))}
183183
}
184184
None => quote! { None },
185185
};
@@ -188,8 +188,8 @@ impl Parse for Options {
188188
Some(ty) => {
189189
let ty = ty.get_type()?;
190190
(
191-
quote! {Some(::cosmwasm_schema::schema_for!(#ty))},
192-
quote! { Some(<#ty as ::cosmwasm_schema::QueryResponses>::response_schemas().unwrap()) },
191+
quote! {Some(#crate_name::schema_for!(#ty))},
192+
quote! { Some(<#ty as #crate_name::QueryResponses>::response_schemas().unwrap()) },
193193
)
194194
}
195195
None => (quote! { None }, quote! { None }),
@@ -198,15 +198,15 @@ impl Parse for Options {
198198
let migrate = match map.remove(&parse_quote!(migrate)) {
199199
Some(ty) => {
200200
let ty = ty.get_type()?;
201-
quote! {Some(::cosmwasm_schema::schema_for!(#ty))}
201+
quote! {Some(#crate_name::schema_for!(#ty))}
202202
}
203203
None => quote! { None },
204204
};
205205

206206
let sudo = match map.remove(&parse_quote!(sudo)) {
207207
Some(ty) => {
208208
let ty = ty.get_type()?;
209-
quote! {Some(::cosmwasm_schema::schema_for!(#ty))}
209+
quote! {Some(#crate_name::schema_for!(#ty))}
210210
}
211211
None => quote! { None },
212212
};

packages/schema-derive/src/query_responses.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

127131
fn parse_tuple((q, r): (String, Expr)) -> ExprTuple {
@@ -144,10 +148,20 @@ fn to_snake_case(input: &str) -> String {
144148

145149
#[cfg(test)]
146150
mod 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
}

packages/schema-derive/src/query_responses/context.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::collections::HashSet;
22

33
use crate::error::bail;
4-
use syn::{Ident, ItemEnum};
4+
use syn::{parse_quote, Ident, ItemEnum, LitStr};
55

66
const ATTR_PATH: &str = "query_responses";
77

88
pub struct Context {
9+
/// Name of the crate referenced in the macro expansions
10+
pub crate_name: syn::Path,
911
/// If the enum we're trying to derive QueryResponses for collects other QueryMsgs,
1012
/// setting this flag will derive the implementation appropriately, collecting all
1113
/// KV pairs from the nested enums rather than expecting `#[return]` annotations.
@@ -16,6 +18,7 @@ pub struct Context {
1618

1719
pub fn get_context(input: &ItemEnum) -> syn::Result<Context> {
1820
let mut ctx = Context {
21+
crate_name: parse_quote!(::cosmwasm_schema),
1922
is_nested: false,
2023
no_bounds_for: HashSet::new(),
2124
};
@@ -43,6 +46,9 @@ pub fn get_context(input: &ItemEnum) -> syn::Result<Context> {
4346
})?;
4447
} else if param.path.is_ident("nested") {
4548
ctx.is_nested = true;
49+
} else if param.path.is_ident("crate") {
50+
let crate_name_str: LitStr = param.input.parse()?;
51+
ctx.crate_name = crate_name_str.parse()?;
4652
} else {
4753
bail!(param.path, "unrecognized QueryResponses param");
4854
}

0 commit comments

Comments
 (0)