Skip to content

Commit

Permalink
Validate names in Odra.toml #37
Browse files Browse the repository at this point in the history
  • Loading branch information
kpob committed Jun 25, 2024
1 parent 3b23694 commit 13ecc48
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/actions/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl BuildAction<'_> {
pub fn build(&self) {
utils::check_target_requirements();
utils::validate_contract_name_argument(self.project, self.contracts_names());
utils::validate_contract_names(self.project);
self.build_wasm_files();
self.optimize_wasm_files();
}
Expand Down
1 change: 1 addition & 0 deletions src/actions/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl SchemaAction<'_> {
pub fn build(&self) {
utils::check_target_requirements();
utils::validate_contract_name_argument(self.project, self.contracts_names());
utils::validate_contract_names(self.project);
self.generate_schema_files();
}

Expand Down
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub enum Error {
#[error("Contract {0} not found in Odra.toml")]
ContractNotFound(String),

#[error("Contract {0} defined multiple times in Odra.toml, please make sure every contract has a unique name.")]
ContractDuplicate(String),

#[error("Odra is not a dependency of this project.")]
OdraNotADependency,

Expand Down Expand Up @@ -128,6 +131,7 @@ impl Error {
Error::FailedToParseTemplatesFile(_) => 28,
Error::TemplateNotFound(_) => 29,
Error::IncorrectTemplateType => 30,
Error::ContractDuplicate(_) => 31,
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ pub fn validate_contract_name_argument(project: &Project, names_string: String)
});
}

/// Validate if contract names are unique.
pub fn validate_contract_names(project: &Project) {
project.odra_toml().contracts.iter().for_each(|contract| {
if project
.odra_toml()
.contracts
.iter()
.filter(|c| c.struct_name() == contract.struct_name())
.count()
> 1
{
Error::ContractDuplicate(contract.struct_name()).print_and_die();
}
});
}

fn remove_extra_spaces(input: &str) -> Result<String, &'static str> {
// Ensure there are no other separators
if input.chars().any(|c| c.is_whitespace() && c != ' ') {
Expand Down

0 comments on commit 13ecc48

Please sign in to comment.