From 58e7f54b216c559c90f7b4d327d5106f6899f74b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20P=C5=82askonka?= Date: Tue, 9 Jan 2024 16:55:23 +0100 Subject: [PATCH] Removed name from Odra.toml --- src/actions/build.rs | 16 +++++++++------- src/actions/generate.rs | 9 ++++----- src/odra_toml.rs | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/actions/build.rs b/src/actions/build.rs index a5d4b40..6baed5f 100644 --- a/src/actions/build.rs +++ b/src/actions/build.rs @@ -37,7 +37,7 @@ impl BuildAction<'_> { false => odra_toml .contracts .into_iter() - .filter(|c| names.contains(&c.name)) + .filter(|c| names.contains(&c.struct_name())) .collect(), } } @@ -60,7 +60,7 @@ impl BuildAction<'_> { .odra_toml() .contracts .iter() - .any(|c| c.name == *contract_name) + .any(|c| c.struct_name() == *contract_name) { Error::ContractNotFound(contract_name.clone()).print_and_die(); } @@ -71,15 +71,17 @@ impl BuildAction<'_> { fn build_wasm_files(&self) { log::info("Generating wasm files..."); command::mkdir(paths::wasm_dir(self.project.project_root())); + for contract in self.contracts() { let build_contract = format!("{}_build_contract", &contract.module_name()); command::cargo_build_wasm_files( self.project.project_root(), - &contract.name, + &contract.struct_name(), &contract.module_name(), ); let source = paths::wasm_path_in_target(&build_contract, self.project.project_root()); - let target = paths::wasm_path_in_wasm_dir(&contract.name, self.project.project_root()); + let target = + paths::wasm_path_in_wasm_dir(&contract.struct_name(), self.project.project_root()); log::info(format!("Saving {}", target.display())); command::cp(source.clone(), target); // if a contract is in a module, copy the file also to the module wasm folder @@ -92,7 +94,7 @@ impl BuildAction<'_> { .join(contract.module_name()) .join("wasm"); command::mkdir(module_wasm_dir.clone()); - let mut module_wasm_path = module_wasm_dir.clone().join(&contract.name); + let mut module_wasm_path = module_wasm_dir.clone().join(&contract.struct_name()); module_wasm_path.set_extension("wasm"); log::info(format!("Copying to {}", module_wasm_path.display())); command::cp(source, module_wasm_path); @@ -104,10 +106,10 @@ impl BuildAction<'_> { fn optimize_wasm_files(&self) { log::info("Optimizing wasm files..."); for contract in self.contracts() { - command::wasm_strip(&contract.name, self.project.project_root()); + command::wasm_strip(&contract.struct_name(), self.project.project_root()); if contract.module_name() != self.project.name { command::wasm_strip( - &contract.name, + &contract.struct_name(), self.project.project_root().join(contract.module_name()), ); } diff --git a/src/actions/generate.rs b/src/actions/generate.rs index 471ccf0..5e4c02a 100644 --- a/src/actions/generate.rs +++ b/src/actions/generate.rs @@ -137,21 +137,20 @@ impl GenerateAction<'_> { /// Add contract definition to Odra.toml. fn update_odra_toml(&self) { let mut odra_toml = self.project.odra_toml(); - let contract_name = self.contract_name(); + let contract_name = self.module_ident(); // Check if Odra.toml has already a contract. - let exists = odra_toml.has_contract(contract_name); + let exists = odra_toml.has_contract(contract_name.as_str()); if exists { - Error::ContractAlreadyInOdraToml(String::from(contract_name)).print_and_die(); + Error::ContractAlreadyInOdraToml(contract_name).print_and_die(); } // Add contract to Odra.toml. odra_toml.contracts.push(Contract { - name: self.contract_name().to_string(), fqn: format!( "{}::{}::{}", self.module_name, - self.contract_name(), + self.module_ident(), self.module_ident() ), }); diff --git a/src/odra_toml.rs b/src/odra_toml.rs index 5746aee..3d97041 100644 --- a/src/odra_toml.rs +++ b/src/odra_toml.rs @@ -2,6 +2,7 @@ use std::path::{Path, PathBuf}; +use convert_case::{Boundary, Case, Casing}; use serde_derive::{Deserialize, Serialize}; use crate::{ @@ -12,8 +13,6 @@ use crate::{ /// Struct describing contract. #[derive(Deserialize, Serialize, Debug, Clone)] pub struct Contract { - /// Name of the contract - pub name: String, pub fqn: String, } @@ -26,6 +25,14 @@ impl Contract { .unwrap_or_else(|| MalformedFqn.print_and_die()) .to_string() } + + pub fn struct_name(&self) -> String { + self.fqn + .split_terminator("::") + .last() + .unwrap_or_else(|| MalformedFqn.print_and_die()) + .to_string() + } } /// Odra configuration. @@ -58,7 +65,9 @@ impl OdraToml { /// Check if the contract is defined in Odra.toml file. pub fn has_contract(&self, contract_name: &str) -> bool { - self.contracts.iter().any(|c| c.name == contract_name) + self.contracts + .iter() + .any(|c| c.struct_name() == contract_name) } /// Check if any contract in Odra.toml is a part of a module with given name