From 23e1db7819650220d181711f727b399f243cbd8f Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Thu, 25 Jul 2024 00:09:57 +0100 Subject: [PATCH] Show function name correctly for contract.{encode,call,send} --- src/interpreter/builtins/concat.rs | 4 ++-- src/interpreter/functions/contract.rs | 20 +++++++++++++++++--- src/interpreter/functions/definition.rs | 22 +++++++++++----------- src/interpreter/functions/user_defined.rs | 4 ++-- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/interpreter/builtins/concat.rs b/src/interpreter/builtins/concat.rs index 51aafce..136ae2f 100644 --- a/src/interpreter/builtins/concat.rs +++ b/src/interpreter/builtins/concat.rs @@ -53,8 +53,8 @@ fn concat(args: &[Value]) -> Result { 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) -> Vec> { diff --git a/src/interpreter/functions/contract.rs b/src/interpreter/functions/contract.rs index 801016c..97325e6 100644 --- a/src/interpreter/functions/contract.rs +++ b/src/interpreter/functions/contract.rs @@ -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; @@ -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) -> Vec> { 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() diff --git a/src/interpreter/functions/definition.rs b/src/interpreter/functions/definition.rs index ecaede5..61a6bd0 100644 --- a/src/interpreter/functions/definition.rs +++ b/src/interpreter/functions/definition.rs @@ -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) -> Vec>; @@ -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) -> Vec> { @@ -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) -> Vec> { @@ -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) -> Vec> { @@ -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) -> Vec> { @@ -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) -> Vec> { diff --git a/src/interpreter/functions/user_defined.rs b/src/interpreter/functions/user_defined.rs index c24c394..1fa7dad 100644 --- a/src/interpreter/functions/user_defined.rs +++ b/src/interpreter/functions/user_defined.rs @@ -21,8 +21,8 @@ impl From 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) -> Vec> {