-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
151 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! Module for generating contracts schema. | ||
use super::utils; | ||
use crate::{command, log, project::Project}; | ||
|
||
/// SchemaAction configuration. | ||
pub struct SchemaAction<'a> { | ||
project: &'a Project, | ||
contracts_names: Option<String>, | ||
} | ||
|
||
impl<'a> SchemaAction<'a> { | ||
/// Crate a new SchemaAction for a given configuration. | ||
pub fn new(project: &'a Project, contracts_names: Option<String>) -> Self { | ||
SchemaAction { | ||
project, | ||
contracts_names, | ||
} | ||
} | ||
} | ||
|
||
impl SchemaAction<'_> { | ||
/// Main function that runs the whole workflow. | ||
pub fn build(&self) { | ||
utils::check_target_requirements(); | ||
utils::validate_contract_name_argument(self.project, self.contracts_names()); | ||
self.generate_schema_files(); | ||
} | ||
|
||
/// Generates *_schema.json files. | ||
fn generate_schema_files(&self) { | ||
log::info("Generating schema files..."); | ||
let contracts = | ||
utils::contracts(self.project, self.contracts_names()).unwrap_or_else(|_| { | ||
Error::FailedToParseArgument("contracts_names".to_string()).print_and_die() | ||
}); | ||
for contract in contracts { | ||
command::cargo_generate_schema_files( | ||
self.project.project_root(), | ||
&contract.struct_name(), | ||
&contract.module_name(), | ||
); | ||
} | ||
} | ||
|
||
fn contracts_names(&self) -> String { | ||
self.contracts_names.clone().unwrap_or_default() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::{command, errors::Error, odra_toml::Contract, project::Project}; | ||
|
||
/// Check if wasm32-unknown-unknown target is installed. | ||
pub fn check_target_requirements() { | ||
if !command::command_output("rustup target list --installed").contains("wasm32-unknown-unknown") | ||
{ | ||
Error::WasmTargetNotInstalled.print_and_die(); | ||
} | ||
} | ||
|
||
/// Returns list of contract to process. | ||
pub fn contracts(project: &Project, names_string: String) -> Result<Vec<Contract>, &'static str> { | ||
let names = parse_contracts_names(names_string)?; | ||
let odra_toml = project.odra_toml(); | ||
Ok(match names.is_empty() { | ||
true => odra_toml.contracts, | ||
false => odra_toml | ||
.contracts | ||
.into_iter() | ||
.filter(|c| names.contains(&c.struct_name())) | ||
.collect(), | ||
}) | ||
} | ||
|
||
/// Check if contract name argument is valid if set. | ||
pub fn validate_contract_name_argument(project: &Project, names_string: String) { | ||
let names = parse_contracts_names(names_string); | ||
names.iter().for_each(|contract_name| { | ||
if !project | ||
.odra_toml() | ||
.contracts | ||
.iter() | ||
.any(|c| c.struct_name() == *contract_name) | ||
{ | ||
Error::ContractNotFound(contract_name.clone()).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 != ' ') { | ||
return Err("Input contains non-space whitespace characters"); | ||
} | ||
|
||
let trimmed = input.split_whitespace().collect::<Vec<&str>>().join(" "); | ||
Ok(trimmed) | ||
} | ||
|
||
fn parse_contracts_names(names_string: String) -> Result<Vec<String>, &'static str> { | ||
match names_string.is_empty() { | ||
true => Ok(vec![]), | ||
false => remove_extra_spaces(&names_string).map(|string| { | ||
string | ||
.split(' ') | ||
.map(ToString::to_string) | ||
.collect::<Vec<_>>() | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters