Skip to content

Commit

Permalink
Back to rewriting the error argument
Browse files Browse the repository at this point in the history
  • Loading branch information
MOZGIII committed Jan 20, 2023
1 parent 648438b commit 99fe8f9
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions proc-macros/src/render_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ impl RpcDescription {
}

/// Verify and rewrite the return type (for methods).
fn return_result_type(&self, ty: &syn::Type) -> TokenStream2 {
fn return_result_type(&self, mut ty: &syn::Type) -> TokenStream2 {
// We expect a valid type path.
let syn::Type::Path(type_path) = ty else {
let syn::Type::Path(mut type_path) = ty else {
return quote_spanned!(ty.span() => compile_error!("Expecting something like 'Result<Foo, Err>' here. (1)"));
};

// The path (eg std::result::Result) should have a final segment like 'Result'.
let Some(type_name) = type_path.path.segments.last() else {
let Some(mut type_name) = type_path.path.segments.last() else {
return quote_spanned!(ty.span() => compile_error!("Expecting this path to end in something like 'Result<Foo, Err>'"));
};

// Get the generic args eg the <T, E> in Result<T, E>.
let PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) = &type_name.arguments else {
let PathArguments::AngleBracketed(AngleBracketedGenericArguments { mut args, .. }) = &type_name.arguments else {
return quote_spanned!(ty.span() => compile_error!("Expecting something like 'Result<Foo, Err>' here, but got no generic args (eg no '<Foo,Err>')."));
};

Expand All @@ -92,11 +92,11 @@ impl RpcDescription {
return quote_spanned!(args.span() => compile_error!("Expecting two generic args here."));
}

// The first generic arg is our custom return type.
// Return a new Result with that custom type and jsonrpsee::core::Error:
let custom_return_type = args.first().unwrap();
let error_type = self.jrps_client_item(quote! { core::Error });
quote!(std::result::Result<#custom_return_type, #error_type>)
// Force the last argument to be `jsonrpsee::core::Error`:
let error_arg = args.last_mut().unwrap();
*error_arg = syn::GenericArgument::Type(syn::Type::Verbatim(self.jrps_client_item(quote! { core::Error })));

quote!(#ty)
} else if type_name.ident == "RpcResult" {
// RpcResult<T> (an alias we export) should have 1 generic arg.
if args.len() != 1 {
Expand Down

0 comments on commit 99fe8f9

Please sign in to comment.