Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support #[handle_result] in ABI macro #858

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions near-sdk-macros/src/core_impl/abi/abi_generator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::core_impl::utils;
use crate::core_impl::{utils, AttrSigInfo};
use crate::core_impl::{BindgenArgType, SerializerType};
use crate::{ImplItemMethodInfo, MethodType};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::spanned::Spanned;
use syn::ReturnType;

impl ImplItemMethodInfo {
Expand Down Expand Up @@ -45,8 +46,8 @@ impl ImplItemMethodInfo {
&self.attr_signature_info.method_type,
&MethodType::Init | &MethodType::InitIgnoreState
);
let is_payable = self.attr_signature_info.is_payable;
let is_private = self.attr_signature_info.is_private;
let AttrSigInfo { is_payable, is_private, is_handles_result, .. } =
self.attr_signature_info;

let mut params = Vec::<TokenStream2>::new();
let mut callbacks = Vec::<TokenStream2>::new();
Expand Down Expand Up @@ -126,6 +127,34 @@ impl ImplItemMethodInfo {
None
}
}
ReturnType::Type(_, ty) if utils::type_is_result(ty) && is_handles_result => {
itegulov marked this conversation as resolved.
Show resolved Hide resolved
let ty = if let Some(ty) = utils::extract_ok_type(ty) {
ty
} else {
return syn::Error::new_spanned(
ty,
"Function marked with #[handle_result] should have return type Result<T, PromiseError>",
itegulov marked this conversation as resolved.
Show resolved Hide resolved
)
.into_compile_error();
};
let serialization_type =
abi_serialization_type(&self.attr_signature_info.result_serializer);
quote! {
Some(
near_sdk::__private::AbiType {
type_schema: gen.subschema_for::<#ty>(),
serialization_type: #serialization_type,
}
)
}
}
ReturnType::Type(_, ty) if is_handles_result => {
return syn::Error::new(
ty.span(),
"Method marked with #[handle_result] should return Result<T, E>.",
)
.to_compile_error();
}
ReturnType::Type(_, ty) => {
let serialization_type =
abi_serialization_type(&self.attr_signature_info.result_serializer);
Expand Down