Skip to content

Commit

Permalink
Fix generate casing
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed Dec 6, 2023
1 parent a88368d commit 97d971d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 33 deletions.
15 changes: 3 additions & 12 deletions src/actions/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,9 @@ impl BuildAction<'_> {
log::info("Generating wasm files...");
command::mkdir(paths::wasm_dir(self.project.project_root()));
for contract in self.contracts() {
command::cargo_build_wasm_files(
self.project.project_root(),
&paths::to_snake_titlecase(&contract.name),
);
command::cargo_build_wasm_files(self.project.project_root(), &contract.name);
let source = paths::wasm_path_in_target("build_contract", self.project.project_root());
let target = paths::wasm_path_in_wasm_dir(
&paths::to_snake_titlecase(&contract.name),
self.project.project_root(),
);
let target = paths::wasm_path_in_wasm_dir(&contract.name, self.project.project_root());
log::info(format!("Saving {}", target.display()));
command::cp(source, target);
}
Expand All @@ -90,10 +84,7 @@ impl BuildAction<'_> {
fn optimize_wasm_files(&self) {
log::info("Optimizing wasm files...");
for contract in self.contracts() {
command::wasm_strip(
&paths::to_snake_titlecase(contract.name),
self.project.project_root(),
);
command::wasm_strip(&contract.name, self.project.project_root());
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/actions/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::{
pub struct GenerateAction<'a> {
project: &'a Project,
contract_name: String,
contract_module_ident: String,
module_root: PathBuf,
module_name: String,
template_generator: TemplateGenerator,
Expand All @@ -31,8 +30,7 @@ impl<'a> GenerateAction<'a> {
pub fn new(project: &'a Project, contract_name: String, module_name: Option<String>) -> Self {
GenerateAction {
project,
contract_name: paths::to_snake_case(&contract_name),
contract_module_ident: contract_name.to_case(Case::UpperCamel),
contract_name,
module_root: project.module_root(module_name.clone()),
module_name: project.module_name(module_name),
template_generator: TemplateGenerator::new(
Expand All @@ -58,15 +56,20 @@ impl GenerateAction<'_> {
}

/// Returns the module identifier. It is the struct name.
fn module_ident(&self) -> &str {
&self.contract_module_ident
fn module_ident(&self) -> String {
let contract_name = self.contract_name();
contract_name.to_case(Case::UpperCamel)
}

fn contract_snake_case(&self) -> String {
paths::to_snake_case(self.contract_name())
}

/// Returns a path to file with contract definition.
fn module_file_path(&self) -> PathBuf {
self.module_root
.join("src")
.join(self.contract_name())
.join(self.contract_snake_case())
.with_extension("rs")
}

Expand All @@ -75,7 +78,7 @@ impl GenerateAction<'_> {
// Rename module name.
let contract_body = self
.template_generator
.module_template(self.module_ident())
.module_template(&self.module_ident())
.unwrap_or_else(|err| err.print_and_die());

// Make sure the file do not exists.
Expand All @@ -93,7 +96,7 @@ impl GenerateAction<'_> {
// Prepare code to add.
let register_module_code = self
.template_generator
.register_module_snippet(self.contract_name(), self.module_ident())
.register_module_snippet(&self.contract_snake_case(), &self.module_ident())
.unwrap_or_else(|err| err.print_and_die());

// Write to file.
Expand Down
9 changes: 0 additions & 9 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,3 @@ pub fn to_snake_case<T: AsRef<str>>(text: T) -> String {
.without_boundaries(&[Boundary::UpperDigit, Boundary::LowerDigit])
.to_case(Case::Snake)
}

pub fn to_snake_titlecase<T: AsRef<str>>(text: T) -> String {
let mut text = to_snake_case(text);
if let Some(r) = text.get_mut(0..1) {
r.make_ascii_uppercase();
}

text
}
5 changes: 1 addition & 4 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use ureq::get;

use crate::{
command::read_file_content,
consts::{
MODULE_REGISTER,
MODULE_TEMPLATE,
},
consts::{MODULE_REGISTER, MODULE_TEMPLATE},
errors::Error,
project::OdraLocation,
};
Expand Down

0 comments on commit 97d971d

Please sign in to comment.