Skip to content

Commit

Permalink
[fix] hyperledger-iroha#1917: Add easy_from_str_impl macro (hyperledg…
Browse files Browse the repository at this point in the history
…er-iroha#2054)

Signed-off-by: Sam H. Smith <[email protected]>
  • Loading branch information
SamHSmith authored and appetrosyan committed May 12, 2022
1 parent e9e9cfd commit ceec6b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
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}

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

0 comments on commit ceec6b1

Please sign in to comment.