difftreelog
refactor external generation of call parsers
in: master
4 files changed
crates/evm-coder-macros/src/lib.rsdiffbeforeafterboth--- a/crates/evm-coder-macros/src/lib.rs
+++ b/crates/evm-coder-macros/src/lib.rs
@@ -5,10 +5,7 @@
use proc_macro::TokenStream;
use quote::quote;
use sha3::{Digest, Keccak256};
-use syn::{
- AttributeArgs, DeriveInput, GenericArgument, Ident, ItemTrait, Pat, Path, PathArguments,
- PathSegment, Type, parse_macro_input, spanned::Spanned,
-};
+use syn::{AttributeArgs, DeriveInput, GenericArgument, Ident, ItemImpl, Pat, Path, PathArguments, PathSegment, Type, parse_macro_input, spanned::Spanned};
mod solidity_interface;
mod to_log;
@@ -189,16 +186,21 @@
let args = parse_macro_input!(args as AttributeArgs);
let args = solidity_interface::InterfaceInfo::from_list(&args).unwrap();
- let input: ItemTrait = match syn::parse(stream) {
+ let input: ItemImpl = match syn::parse(stream) {
Ok(t) => t,
Err(e) => return e.to_compile_error().into(),
};
- match solidity_interface::SolidityInterface::try_from(args, &input) {
+ let expanded = match solidity_interface::SolidityInterface::try_from(args, &input) {
Ok(v) => v.expand(),
Err(e) => e.to_compile_error(),
- }
- .into()
+ };
+
+ (quote! {
+ #input
+
+ #expanded
+ }).into()
}
#[proc_macro_attribute]
crates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth--- a/crates/evm-coder-macros/src/solidity_interface.rs
+++ b/crates/evm-coder-macros/src/solidity_interface.rs
@@ -4,7 +4,10 @@
use darling::FromMeta;
use inflector::cases;
use std::fmt::Write;
-use syn::{FnArg, Ident, ItemTrait, Meta, NestedMeta, PatType, Path, ReturnType, TraitItem, TraitItemMethod, Type, Visibility, spanned::Spanned};
+use syn::{
+ FnArg, Ident, ImplItem, ImplItemMethod, ItemImpl, Meta, NestedMeta, PatType, Path, ReturnType,
+ Type, spanned::Spanned,
+};
use crate::{
fn_selector_str, parse_ident_from_pat, parse_ident_from_path, parse_ident_from_type,
@@ -51,21 +54,13 @@
fn expand_variant_call(&self) -> proc_macro2::TokenStream {
let name = &self.name;
- let snake_call_name = &self.snake_call_name;
+ let pascal_call_name = &self.pascal_call_name;
quote! {
- InternalCall::#name(call) => return self.#snake_call_name(Msg {
+ InternalCall::#name(call) => return <Self as ::evm_coder::Callable<#pascal_call_name>>::call(self, Msg {
call,
caller: c.caller,
value: c.value,
})
- }
- }
-
- fn expand_call_inner(&self) -> proc_macro2::TokenStream {
- let snake_call_name = &self.snake_call_name;
- let pascal_call_name = &self.pascal_call_name;
- quote! {
- fn #snake_call_name(&mut self, c: Msg<#pascal_call_name>) -> Result<::evm_coder::abi::AbiWriter>;
}
}
@@ -97,6 +92,7 @@
#[derive(FromMeta)]
pub struct InterfaceInfo {
+ name: Ident,
#[darling(default)]
is: IsList,
#[darling(default)]
@@ -192,7 +188,7 @@
result: Type,
}
impl Method {
- fn try_from(value: &TraitItemMethod) -> syn::Result<Self> {
+ fn try_from(value: &ImplItemMethod) -> syn::Result<Self> {
let mut info = MethodInfo {
rename_selector: None,
};
@@ -386,36 +382,31 @@
}
pub struct SolidityInterface {
- vis: Visibility,
- name: Ident,
+ name: Box<syn::Type>,
+ ident: Ident,
info: InterfaceInfo,
methods: Vec<Method>,
- items: Vec<TraitItem>,
}
impl SolidityInterface {
- pub fn try_from(info: InterfaceInfo, value: &ItemTrait) -> syn::Result<Self> {
+ pub fn try_from(info: InterfaceInfo, value: &ItemImpl) -> syn::Result<Self> {
let mut methods = Vec::new();
for item in &value.items {
- match item {
- TraitItem::Method(method) => methods.push(Method::try_from(method)?),
- _ => {}
+ if let ImplItem::Method(method) = item {
+ methods.push(Method::try_from(method)?)
}
}
Ok(Self {
- vis: value.vis.clone(),
- name: value.ident.clone(),
+ name: value.self_ty.clone(),
+ ident: parse_ident_from_type(&value.self_ty)?.clone(),
info,
methods,
- items: value.items.clone(),
})
}
pub fn expand(self) -> proc_macro2::TokenStream {
- let vis = self.vis;
let name = self.name;
- let items = self.items;
- let call_name = pascal_ident_to_call(&name);
+ let call_name = pascal_ident_to_call(&self.info.name);
let call_sub = self
.info
@@ -424,13 +415,6 @@
.iter()
.chain(self.info.is.0.iter())
.map(Is::expand_call_def);
- let call_inner = self
- .info
- .inline_is
- .0
- .iter()
- .chain(self.info.is.0.iter())
- .map(Is::expand_call_inner);
let call_parse = self
.info
.inline_is
@@ -459,7 +443,7 @@
quote! {
#[derive(Debug)]
- #vis enum #call_name {
+ pub enum #call_name {
#(
#calls,
)*
@@ -486,7 +470,7 @@
)
}
}
- impl ::evm_coder::Call for #call_name {
+ impl ::evm_coder::Call for #call_name {
fn parse(method_id: u32, reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Option<Self>> {
use ::evm_coder::abi::AbiRead;
match method_id {
@@ -500,17 +484,8 @@
)else*
return Ok(None);
}
- }
- #vis trait #name {
- #(
- #items
- )*
- #(
- #call_inner
- )*
}
- impl<T> ::evm_coder::Callable for T where T: #name {
- type Call = #call_name;
+ impl ::evm_coder::Callable<#call_name> for #name {
#[allow(unreachable_code)] // In case of no inner calls
fn call(&mut self, c: Msg<#call_name>) -> Result<::evm_coder::abi::AbiWriter> {
use ::evm_coder::abi::AbiWrite;
@@ -530,7 +505,7 @@
}
Ok(writer)
}
- }
+ }
}
}
}
crates/evm-coder/src/lib.rsdiffbeforeafterboth--- a/crates/evm-coder/src/lib.rs
+++ b/crates/evm-coder/src/lib.rs
@@ -57,9 +57,8 @@
fn parse(selector: u32, input: &mut AbiReader) -> execution::Result<Option<Self>>;
}
-pub trait Callable {
- type Call: Call;
- fn call(&mut self, call: types::Msg<Self::Call>) -> execution::Result<AbiWriter>;
+pub trait Callable<C: Call> {
+ fn call(&mut self, call: types::Msg<C>) -> execution::Result<AbiWriter>;
}
#[cfg(test)]
crates/evm-coder/tests/a.rsdiffbeforeafterboth3use evm_coder::{solidity_interface, types::*, ToLog, execution::Result};3use evm_coder::{solidity_interface, types::*, ToLog, execution::Result};4use evm_coder_macros::solidity;4use evm_coder_macros::solidity;56struct Impls;576#[solidity_interface]8#[solidity_interface(name = "OurInterface")]7trait OurInterface {9impl Impls {8 fn fn_a(&self, input: uint256) -> Result<bool>;10 fn fn_a(&self, input: uint256) -> Result<bool> {11 todo!()12 }9}13}101411#[solidity_interface]15#[solidity_interface(name = "OurInterface1")]12trait OurInterface1 {16impl Impls {13 fn fn_b(&self, input: uint128) -> Result<uint32>;17 fn fn_b(&self, input: uint128) -> Result<uint32> {18 todo!()19 }14}20}152116#[solidity_interface(is(OurInterface), inline_is(OurInterface1), events(ERC721Log))]22#[solidity_interface(name = "OurInterface2", is(OurInterface), inline_is(OurInterface1), events(ERC721Log))]17trait OurInterface2 {23impl Impls {18 #[solidity(rename_selector = "fnK")]24 #[solidity(rename_selector = "fnK")]19 fn fn_c(&self, input: uint32) -> Result<uint8>;25 fn fn_c(&self, input: uint32) -> Result<uint8> {26 todo!()27 }20 fn fn_d(&self, value: uint32) -> Result<uint32>;28 fn fn_d(&self, value: uint32) -> Result<uint32> {29 todo!()30 }213122 fn caller_sensitive(&self, caller: caller) -> Result<uint8>;32 fn caller_sensitive(&self, caller: caller) -> Result<uint8> {33 todo!()34 }23 fn payable(&mut self, value: value) -> Result<uint8>;35 fn payable(&mut self, value: value) -> Result<uint8> {36 todo!()37 }24}38}253926#[derive(ToLog)]40#[derive(ToLog)]39 },53 },40}54}5556struct ERC20;415742#[solidity_interface]58#[solidity_interface(name = "ERC20")]43trait ERC20 {59impl ERC20 {44 fn decimals(&self) -> Result<uint8>;60 fn decimals(&self) -> Result<uint8> {61 todo!()62 }45 fn balance_of(&self, owner: address) -> Result<uint256>;63 fn balance_of(&self, owner: address) -> Result<uint256> {64 todo!()65 }46 fn transfer(&mut self, caller: caller, to: address, value: uint256) -> Result<bool>;66 fn transfer(&mut self, caller: caller, to: address, value: uint256) -> Result<bool> {67 todo!()68 }47 fn transfer_from(69 fn transfer_from(48 &mut self,70 &mut self,49 caller: caller,71 caller: caller,50 from: address,72 from: address,51 to: address,73 to: address,52 value: uint256,74 value: uint256,53 ) -> Result<bool>;75 ) -> Result<bool> {76 todo!()77 }54 fn approve(&mut self, caller: caller, spender: address, value: uint256) -> Result<bool>;78 fn approve(&mut self, caller: caller, spender: address, value: uint256) -> Result<bool> {79 todo!()80 }55 fn allowance(&self, owner: address, spender: address) -> Result<uint256>;81 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {82 todo!()83 }56}84}5785