Skip to content

Commit

Permalink
More validation to generate command (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
zie1ony authored Jul 19, 2023
1 parent e06118b commit 32d9cb1
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
.idea
testproject
Cargo.lock
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Changelog for `cargo-odra`.

## [0.0.9] - 2023-07-19

### Added
- `generate` command validates if the module is already added to `lib.rs`.

## [0.0.8] - 2023-06-26

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "A cargo utility that helps to create, manage and test your smart
keywords = ["wasm", "webassembly", "blockchain"]
categories = ["wasm", "smart contracts"]
name = "cargo-odra"
version = "0.0.8"
version = "0.0.9"
edition = "2021"

[dependencies]
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,11 @@ workspace, put all contracts in the same Odra.toml folder.

* [Odra](https://github.com/odradev/odra)
* [Cargo Odra](https://github.com/odradev/cargo-odra)
* [Odra Template](https://github.com/odradev/odra-template)
* [Odra Casper](https://github.com/odradev/odra-casper)
* [Original Proposal for Odra Framework](https://github.com/odradev/odra-proposal)

## Contact
Write **[email protected]**

---
<div align="center">
by <a href="https://odra.dev">odra.dev<a>
</dev>
26 changes: 26 additions & 0 deletions src/actions/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ impl GenerateAction<'_> {
&self.contract_module_ident
}

/// Returns the module Ref identifier.
fn module_ref_ident(&self) -> String {
format!("{}Ref", self.contract_module_ident)
}

/// Returns a path to file with contract definition.
fn module_file_path(&self) -> PathBuf {
self.module_root
Expand Down Expand Up @@ -96,6 +101,27 @@ impl GenerateAction<'_> {
.register_module_snippet(self.contract_name(), self.module_ident())
.unwrap_or_else(|err| err.print_and_die());

// Read the file.
let lib_rs_path = self.module_root.join("src/lib.rs");
let lib_rs = command::read_file_content(lib_rs_path)
.unwrap_or_else(|_| Error::LibRsNotFound.print_and_die());

// If the file already has module registered, throw an error.
if lib_rs.contains(&register_module_code) {
Error::ModuleAlreadyInLibRs(String::from(self.contract_name())).print_and_die();
}

// Check if he file might have the module registered in another form.
if lib_rs.contains(self.contract_name())
|| (lib_rs.contains(self.module_ident()) && lib_rs.contains(&self.module_ref_ident()))
{
log::warn(format!(
"src/lib.rs probably already has {} enabled. Skipping.",
self.contract_name()
));
return;
}

// Write to file.
command::append_file(self.module_root.join("src/lib.rs"), &register_module_code);

Expand Down
8 changes: 8 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ pub enum Error {

#[error("Malformed fqn of contract")]
MalformedFqn,

#[error("src/lib.rs not found.")]
LibRsNotFound,

#[error("Module {0} already in src/lib.rs")]
ModuleAlreadyInLibRs(String),
}

impl Error {
Expand Down Expand Up @@ -96,6 +102,8 @@ impl Error {
Error::FailedToGenerateProjectFromTemplate(_) => 19,
Error::FailedToParseArgument(_) => 20,
Error::MalformedFqn => 21,
Error::LibRsNotFound => 22,
Error::ModuleAlreadyInLibRs(_) => 23,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn info<T: AsRef<str>>(message: T) {
}

/// Warning message, not used yet - remove underscore when in use.
pub fn _warn<T: AsRef<str>>(message: T) {
pub fn warn<T: AsRef<str>>(message: T) {
prettycli::warn(message.as_ref());
}

Expand Down

0 comments on commit 32d9cb1

Please sign in to comment.