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

Support alias for overloaded methods #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 25 additions & 1 deletion derive/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,34 @@ pub struct Contract {

impl<'a> From<&'a ethabi::Contract> for Contract {
fn from(c: &'a ethabi::Contract) -> Self {

let functions: Vec<Function> = c.functions
.iter()
.fold(
Vec::new(),
|mut functions, (method_name, fns)| {
if fns.len() == 1 {
functions.push(fns.get(0).unwrap().into());
} else {
let mut i = 0;
for func in fns {
let mut func: Function = func.into();
if i > 0 {
func.alias = format!("{}_{}", method_name, i);
} else {
func.alias = method_name.to_string();
}
functions.push(func);
i += 1;
}
}
functions
});

Contract {
constructor: c.constructor.as_ref().map(Into::into),
functions: c.functions().map(Into::into).collect(),
events: c.events().map(Into::into).collect(),
functions,
}
}
}
Expand Down
35 changes: 19 additions & 16 deletions derive/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,55 @@ use super::{
to_ethabi_param_vec, to_token,
};

struct TemplateParam {
pub struct TemplateParam {
/// Template param declaration.
///
/// ```text
/// [T0: Into<Uint>, T1: Into<Bytes>, T2: IntoIterator<Item = U2>, U2 = Into<Uint>]
/// ```
declaration: TokenStream,
pub declaration: TokenStream,
/// Template param definition.
///
/// ```text
/// [param0: T0, hello_world: T1, param2: T2]
/// ```
definition: TokenStream,
pub definition: TokenStream,
}

struct Inputs {
pub struct Inputs {
/// Collects template params into vector.
///
/// ```text
/// [Token::Uint(param0.into()), Token::Bytes(hello_world.into()), Token::Array(param2.into_iter().map(Into::into).collect())]
/// ```
tokenize: Vec<TokenStream>,
pub tokenize: Vec<TokenStream>,
/// Template params.
template_params: Vec<TemplateParam>,
pub template_params: Vec<TemplateParam>,
/// Quote used to recreate `Vec<ethabi::Param>`
recreate_quote: TokenStream,
pub recreate_quote: TokenStream,
}

struct Outputs {
pub struct Outputs {
/// Decoding implementation.
implementation: TokenStream,
pub implementation: TokenStream,
/// Decode result.
result: TokenStream,
pub result: TokenStream,
/// Quote used to recreate `Vec<ethabi::Param>`.
recreate_quote: TokenStream,
pub recreate_quote: TokenStream,
}

/// Structure used to generate contract's function interface.
pub struct Function {
/// Function name alias.
pub alias: String,
/// Function name.
name: String,
pub name: String,
/// Function input params.
inputs: Inputs,
pub inputs: Inputs,
/// Function output params.
outputs: Outputs,
pub outputs: Outputs,
/// Constant function.
constant: bool,
pub constant: bool,
}

impl<'a> From<&'a ethabi::Function> for Function {
Expand Down Expand Up @@ -125,6 +127,7 @@ impl<'a> From<&'a ethabi::Function> for Function {
};

Function {
alias: f.name.clone(),
name: f.name.clone(),
inputs: Inputs { tokenize, template_params, recreate_quote: to_ethabi_param_vec(&f.inputs) },
outputs: Outputs {
Expand All @@ -141,7 +144,7 @@ impl Function {
/// Generates the interface for contract's function.
pub fn generate(&self) -> TokenStream {
let name = &self.name;
let module_name = syn::Ident::new(&self.name.to_snake_case(), Span::call_site());
let module_name = syn::Ident::new(&self.alias.to_snake_case(), Span::call_site());
let tokenize = &self.inputs.tokenize;
let declarations: &Vec<_> = &self.inputs.template_params.iter().map(|i| &i.declaration).collect();
let definitions: &Vec<_> = &self.inputs.template_params.iter().map(|i| &i.definition).collect();
Expand Down