From 5cb68598a76e2f56b17f85e50efed2b5740d010d Mon Sep 17 00:00:00 2001 From: AlexD10S Date: Fri, 29 Nov 2024 10:57:25 +0100 Subject: [PATCH] refactor: get_messages and get_constructors --- crates/pop-contracts/src/utils/metadata.rs | 100 ++++++++++++--------- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/crates/pop-contracts/src/utils/metadata.rs b/crates/pop-contracts/src/utils/metadata.rs index ea363964..38cdc12d 100644 --- a/crates/pop-contracts/src/utils/metadata.rs +++ b/crates/pop-contracts/src/utils/metadata.rs @@ -43,7 +43,34 @@ pub enum FunctionType { /// /// # Arguments /// * `path` - Location path of the project or contract artifact. -pub fn get_messages(path: &Path) -> Result, Error> { +pub fn get_messages

(path: P) -> Result, Error> +where + P: AsRef, +{ + get_contract_functions(path.as_ref(), FunctionType::Message) +} + +/// Extracts a list of smart contract contructors parsing the contract artifact. +/// +/// # Arguments +/// * `path` - Location path of the project or contract artifact. +pub fn get_constructors

(path: P) -> Result, Error> +where + P: AsRef, +{ + get_contract_functions(path.as_ref(), FunctionType::Constructor) +} + +/// Extracts a list of smart contract functions (messages or constructors) parsing the contract +/// artifact. +/// +/// # Arguments +/// * `path` - Location path of the project or contract artifact. +/// * `function_type` - Specifies whether to extract messages or constructors. +fn get_contract_functions( + path: &Path, + function_type: FunctionType, +) -> Result, Error> { let contract_artifacts = if path.is_dir() || path.ends_with("Cargo.toml") { let cargo_toml_path = if path.ends_with("Cargo.toml") { path.to_path_buf() } else { path.join("Cargo.toml") }; @@ -53,19 +80,35 @@ pub fn get_messages(path: &Path) -> Result, Error> { }; let transcoder = contract_artifacts.contract_transcoder()?; let metadata = transcoder.metadata(); - Ok(metadata - .spec() - .messages() - .iter() - .map(|message| ContractFunction { - label: message.label().to_string(), - mutates: message.mutates(), - payable: message.payable(), - args: process_args(message.args(), metadata.registry()), - docs: message.docs().join(" "), - default: *message.default(), - }) - .collect()) + + Ok(match function_type { + FunctionType::Message => metadata + .spec() + .messages() + .iter() + .map(|message| ContractFunction { + label: message.label().to_string(), + mutates: message.mutates(), + payable: message.payable(), + args: process_args(message.args(), metadata.registry()), + docs: message.docs().join(" "), + default: *message.default(), + }) + .collect(), + FunctionType::Constructor => metadata + .spec() + .constructors() + .iter() + .map(|constructor| ContractFunction { + label: constructor.label().to_string(), + payable: *constructor.payable(), + args: process_args(constructor.args(), metadata.registry()), + docs: constructor.docs().join(" "), + default: *constructor.default(), + mutates: true, + }) + .collect(), + }) } /// Extracts the information of a smart contract message parsing the contract artifact. @@ -83,34 +126,6 @@ where .ok_or_else(|| Error::InvalidMessageName(message.to_string())) } -/// Extracts a list of smart contract contructors parsing the contract artifact. -/// -/// # Arguments -/// * `path` - Location path of the project or contract artifact. -pub fn get_constructors(path: &Path) -> Result, Error> { - let cargo_toml_path = match path.ends_with("Cargo.toml") { - true => path.to_path_buf(), - false => path.join("Cargo.toml"), - }; - let contract_artifacts = - ContractArtifacts::from_manifest_or_file(Some(&cargo_toml_path), None)?; - let transcoder = contract_artifacts.contract_transcoder()?; - let metadata = transcoder.metadata(); - Ok(metadata - .spec() - .constructors() - .iter() - .map(|constructor| ContractFunction { - label: constructor.label().to_string(), - payable: *constructor.payable(), - args: process_args(constructor.args(), metadata.registry()), - docs: constructor.docs().join(" "), - default: *constructor.default(), - mutates: true, - }) - .collect()) -} - /// Extracts the information of a smart contract constructor parsing the contract artifact. /// /// # Arguments @@ -292,6 +307,7 @@ fn format_type(ty: &Type, registry: &PortableRegistry) -> String { /// * `path` - Location path of the project or contract artifact. /// * `label` - Label of the contract message to retrieve. /// * `args` - Argument values provided by the user. +/// * `function_type` - Specifies whether to process arguments of messages or constructors. pub fn process_function_args

( path: P, label: &str,