Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] #1917: Added easy_from_str_impl macro for use with AssetValueType #2054

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ The following is a short set of guidelines for contributing to Iroha.
* Fork [Iroha](https://github.com/hyperledger/iroha/tree/iroha2-dev).
* Fix your issue of choice.
* Write [tests](https://doc.rust-lang.org/cargo/commands/cargo-test.html). Ensure they all pass (`cargo test`).
* Fix [`clippy`](https://lib.rs/crates/cargo-lints) warnings: `cargo lints clippy --workspace --tests --benches`
* Format code `cargo fmt --all` and generate docs `cargo run --bin iroha_docs >"docs/source/references/config.md" && git add "docs/source/references/config.md"`
* Fix [`clippy`](https://lib.rs/crates/cargo-lints) warnings: `cargo lints clippy --workspace --benches --tests --examples --all-features`
* Format code `cargo +nightly fmt --all` and generate docs `cargo run --bin iroha_docs >"docs/source/references/config.md" && git add "docs/source/references/config.md"`
* `git pull -r hyperledger iroha2-dev`, `git commit -s`, `git push <your-fork>`, and [create a pull request](https://github.com/hyperledger/iroha/compare) to the `iroha2-dev` branch on GitHub.

### Reporting Bugs
Expand Down
34 changes: 22 additions & 12 deletions data_model/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,31 @@ pub enum AssetValueType {
Store,
}

impl FromStr for AssetValueType {
type Err = &'static str;

fn from_str(value_type: &str) -> Result<Self, Self::Err> {
// TODO: Could be implemented with some macro
match value_type {
"Quantity" => Ok(AssetValueType::Quantity),
"BigQuantity" => Ok(AssetValueType::BigQuantity),
"Fixed" => Ok(AssetValueType::Fixed),
"Store" => Ok(AssetValueType::Store),
_ => Err("Unknown variant"),
/// A declarative macro that implements `FromStr` for a given
/// C like enumeration. The macro is invoked like follows:
/// `easy_from_str_impl! { NameOfEnum, EnumVariation1, EnumVariation2, ... }`
macro_rules! easy_from_str_impl {
(eval_to $cmp:expr, $enum_type:ty, $enum_value:tt) => {
if $cmp == stringify!($enum_value) {
return Ok(<$enum_type>::$enum_value);
}
}
};
($enum_type:ty, $( $enum_value:tt ),+ ) => {
impl FromStr for $enum_type {
type Err = &'static str;

fn from_str(value_type: &str) -> Result<Self, Self::Err> {
$(
easy_from_str_impl!{eval_to value_type, $enum_type, $enum_value}
)+
return Err(concat!("Unknown variant for type ", stringify!($enum_type)));
}
}
};
}

easy_from_str_impl! {AssetValueType, Quantity, BigQuantity, Fixed, Store}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to mention for future macros. I think that's not a good tradition in Iroha to write such simple to implement but hard to read syntax.

Take a look at this rule:

Rust macros let you dream up practically whatever input syntax you want. Aim to keep input syntax familiar and cohesive with the rest of your users' code by mirroring existing Rust syntax where possible. Pay attention to the choice and placement of keywords and punctuation.


/// Asset's inner value.
#[derive(
Debug, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, FromVariant, IntoSchema,
Expand Down