Skip to content

Commit

Permalink
Show function name correctly for contract.{encode,call,send}
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Jul 24, 2024
1 parent 4495f74 commit 23e1db7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/interpreter/builtins/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ fn concat(args: &[Value]) -> Result<Value> {
pub struct Concat;

impl FunctionDef for Concat {
fn name(&self) -> &str {
"concat"
fn name(&self) -> String {
"concat".to_string()
}

fn get_valid_args(&self, receiver: &Option<Value>) -> Vec<Vec<FunctionParam>> {
Expand Down
20 changes: 17 additions & 3 deletions src/interpreter/functions/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ pub enum ContractCallMode {
Send,
}

impl std::fmt::Display for ContractCallMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ContractCallMode::Default => write!(f, "default"),
ContractCallMode::Encode => write!(f, "encode"),
ContractCallMode::Call => write!(f, "call"),
ContractCallMode::Send => write!(f, "send"),
}
}
}

impl TryFrom<&str> for ContractCallMode {
type Error = anyhow::Error;

Expand Down Expand Up @@ -107,13 +118,16 @@ impl ContractFunction {
}

impl FunctionDef for ContractFunction {
fn name(&self) -> &str {
&self.func_name
fn name(&self) -> String {
match self.mode {
ContractCallMode::Default => self.func_name.clone(),
_ => format!("{}.{}", self.func_name, self.mode),
}
}

fn get_valid_args(&self, receiver: &Option<Value>) -> Vec<Vec<FunctionParam>> {
let (ContractInfo(_, abi), _) = receiver.clone().unwrap().as_contract().unwrap();
let functions = abi.function(self.name()).cloned().unwrap_or(vec![]);
let functions = abi.function(&self.func_name).cloned().unwrap_or(vec![]);

functions
.into_iter()
Expand Down
22 changes: 11 additions & 11 deletions src/interpreter/functions/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{anyhow, Result};
use futures::{future::BoxFuture, FutureExt};

pub trait FunctionDef: std::fmt::Debug + Send + Sync {
fn name(&self) -> &str;
fn name(&self) -> String;

fn get_valid_args(&self, receiver: &Option<Value>) -> Vec<Vec<FunctionParam>>;

Expand Down Expand Up @@ -39,8 +39,8 @@ impl SyncProperty {
}

impl FunctionDef for SyncProperty {
fn name(&self) -> &str {
&self.name
fn name(&self) -> String {
self.name.clone()
}

fn get_valid_args(&self, _: &Option<Value>) -> Vec<Vec<FunctionParam>> {
Expand Down Expand Up @@ -84,8 +84,8 @@ impl AsyncProperty {
}

impl FunctionDef for AsyncProperty {
fn name(&self) -> &str {
&self.name
fn name(&self) -> String {
self.name.clone()
}

fn get_valid_args(&self, _: &Option<Value>) -> Vec<Vec<FunctionParam>> {
Expand Down Expand Up @@ -132,8 +132,8 @@ impl SyncMethod {
}

impl FunctionDef for SyncMethod {
fn name(&self) -> &str {
&self.name
fn name(&self) -> String {
self.name.clone()
}

fn get_valid_args(&self, _: &Option<Value>) -> Vec<Vec<FunctionParam>> {
Expand Down Expand Up @@ -180,8 +180,8 @@ impl SyncFunction {
}

impl FunctionDef for SyncFunction {
fn name(&self) -> &str {
&self.name
fn name(&self) -> String {
self.name.clone()
}

fn get_valid_args(&self, _: &Option<Value>) -> Vec<Vec<FunctionParam>> {
Expand Down Expand Up @@ -224,8 +224,8 @@ impl AsyncMethod {
}

impl FunctionDef for AsyncMethod {
fn name(&self) -> &str {
&self.name
fn name(&self) -> String {
self.name.clone()
}

fn get_valid_args(&self, _: &Option<Value>) -> Vec<Vec<FunctionParam>> {
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter/functions/user_defined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl From<UserDefinedFunction> for Value {
}

impl FunctionDef for UserDefinedFunction {
fn name(&self) -> &str {
self.func_name.as_str()
fn name(&self) -> String {
self.func_name.clone()
}

fn get_valid_args(&self, _receiver: &Option<Value>) -> Vec<Vec<FunctionParam>> {
Expand Down

0 comments on commit 23e1db7

Please sign in to comment.