Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Jan 14, 2025
1 parent 4b30bbb commit 590e634
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions crates/sol-macro-expander/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS

let returns = returns.as_ref().map(|r| &r.returns).unwrap_or_default();

let is_singular_noname = returns.len() == 1;
let is_tuple_noname = returns.len() > 1;

cx.assert_resolved(parameters)?;
if !returns.is_empty() {
cx.assert_resolved(returns)?;
Expand Down Expand Up @@ -105,35 +102,40 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS

let alloy_sol_types = &cx.crates.sol_types;

let return_type = if is_singular_noname {
cx.expand_rust_type(&returns.first().unwrap().ty)
} else if is_tuple_noname {
let return_rust_tys = expand_types(returns, cx);
quote!((#(#return_rust_tys),*))
} else {
quote!(#return_name)
};

let tuple_ret = generate_return_tuple(returns);
let decode_returns = if is_singular_noname {
let name = anon_name((0, returns.first().unwrap().name.as_ref()));
quote! {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data, validate)
.map(Into::into)
.map(|r: #return_name| r.#name)
}
} else if is_tuple_noname {
quote! {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data, validate)
.map(Into::into)
.map(|r: #return_name| {
#tuple_ret
})
let (return_type, decode_returns) = match returns.len() {
0 => (
quote!(#return_name),
quote! {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data, validate)
.map(Into::into)
},
),
1 => {
let name = anon_name((0, returns.first().unwrap().name.as_ref()));
let ty = cx.expand_rust_type(&returns.first().unwrap().ty);

(
ty,
quote! {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data, validate)
.map(Into::into)
.map(|r: #return_name| r.#name)
},
)
}
} else {
quote! {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data, validate)
.map(Into::into)
_ => {
let return_tys = expand_types(returns, cx);
let tuple_ret = generate_return_tuple(returns);
(
quote!((#(#return_tys),*)),
quote! {
<Self::ReturnTuple<'_> as alloy_sol_types::SolType>::abi_decode_sequence(data, validate)
.map(Into::into)
.map(|r: #return_name| {
#tuple_ret
})
},
)
}
};

Expand Down

0 comments on commit 590e634

Please sign in to comment.