Skip to content

Commit

Permalink
[feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macr…
Browse files Browse the repository at this point in the history
…os for Identifiable, Eq, Hash, Ord

Signed-off-by: Ilia Churin <[email protected]>
  • Loading branch information
ilchu committed Jul 22, 2022
1 parent 9c5c531 commit 179ca40
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 375 deletions.
719 changes: 537 additions & 182 deletions data_model/derive/src/lib.rs

Large diffs are not rendered by default.

40 changes: 15 additions & 25 deletions data_model/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::collections::{btree_map, btree_set};

use derive_more::Display;
use getset::{Getters, MutGetters, Setters};
use iroha_data_model_derive::OrdEqHash;
#[cfg(feature = "ffi_api")]
use iroha_ffi::ffi_bindgen;
use iroha_schema::IntoSchema;
Expand Down Expand Up @@ -126,9 +127,19 @@ impl Default for SignatureCheckCondition {
/// Builder which should be submitted in a transaction to create a new [`Account`]
#[allow(clippy::multiple_inherent_impl)]
#[derive(
Debug, Display, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema,
Debug,
Display,
Clone,
OrdEqHash,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
Identifiable,
)]
#[display(fmt = "[{id}]")]
#[id(type = "<Account as Identifiable>::Id")]
pub struct NewAccount {
/// Identification
id: <Account as Identifiable>::Id,
Expand Down Expand Up @@ -210,8 +221,7 @@ impl NewAccount {
Debug,
Display,
Clone,
PartialEq,
Eq,
OrdEqHash,
Getters,
MutGetters,
Setters,
Expand All @@ -220,10 +230,12 @@ impl NewAccount {
Deserialize,
Serialize,
IntoSchema,
Identifiable,
)]
#[allow(clippy::multiple_inherent_impl)]
#[cfg_attr(feature = "ffi_api", ffi_bindgen)]
#[display(fmt = "({id})")] // TODO: Add more?
#[id(type = "Id")]
pub struct Account {
/// An Identification of the [`Account`].
id: <Self as Identifiable>::Id,
Expand All @@ -243,14 +255,6 @@ pub struct Account {
roles: RoleIds,
}

impl Identifiable for Account {
type Id = Id;

fn id(&self) -> &Self::Id {
&self.id
}
}

impl HasMetadata for Account {
fn metadata(&self) -> &Metadata {
&self.metadata
Expand All @@ -261,20 +265,6 @@ impl Registered for Account {
type With = NewAccount;
}

impl PartialOrd for Account {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Account {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.id().cmp(other.id())
}
}

#[cfg_attr(feature = "ffi_api", ffi_bindgen)]
impl Account {
/// Construct builder for [`Account`] identifiable by [`Id`] containing the given signatories.
Expand Down
81 changes: 18 additions & 63 deletions data_model/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::collections::btree_map;

use derive_more::Display;
use getset::{Getters, MutGetters, Setters};
use iroha_data_model_derive::OrdEqHash;
use iroha_macro::FromVariant;
use iroha_primitives::{fixed, fixed::Fixed};
use iroha_schema::IntoSchema;
Expand Down Expand Up @@ -113,8 +114,7 @@ impl AssetDefinitionEntry {
Debug,
Display,
Clone,
PartialEq,
Eq,
OrdEqHash,
Getters,
MutGetters,
Setters,
Expand All @@ -123,10 +123,12 @@ impl AssetDefinitionEntry {
Deserialize,
Serialize,
IntoSchema,
Identifiable,
)]
#[allow(clippy::multiple_inherent_impl)]
#[cfg_attr(feature = "ffi_api", iroha_ffi::ffi_bindgen)]
#[display(fmt = "{id} {value_type}{mintable}")]
#[id(type = "DefinitionId")]
pub struct AssetDefinition {
/// An Identification of the [`AssetDefinition`].
id: <Self as Identifiable>::Id,
Expand All @@ -147,20 +149,6 @@ impl HasMetadata for AssetDefinition {
}
}

impl PartialOrd for AssetDefinition {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for AssetDefinition {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.id().cmp(other.id())
}
}

/// An assets mintability scheme. `Infinitely` means elastic
/// supply. `Once` is what you want to use. Don't use `Not` explicitly
/// outside of smartcontracts.
Expand Down Expand Up @@ -198,18 +186,19 @@ pub enum Mintable {
Debug,
Display,
Clone,
PartialEq,
Eq,
OrdEqHash,
Getters,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
Identifiable,
)]
#[getset(get = "pub")]
#[cfg_attr(feature = "ffi_api", iroha_ffi::ffi_bindgen)]
#[display(fmt = "{id}: {value}")]
#[id(type = "Id")]
pub struct Asset {
/// Component Identification.
#[getset(skip)]
Expand Down Expand Up @@ -299,20 +288,6 @@ impl AssetValue {
}
}

impl PartialOrd for Asset {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Asset {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.id().cmp(other.id())
}
}

macro_rules! impl_try_as_for_asset_value {
( $($variant:ident( $ty:ty ),)* ) => {$(
impl TryAsMut<$ty> for AssetValue {
Expand Down Expand Up @@ -407,9 +382,19 @@ pub struct Id {
/// Builder which can be submitted in a transaction to create a new [`AssetDefinition`]
#[allow(clippy::multiple_inherent_impl)]
#[derive(
Debug, Display, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema,
Debug,
Display,
Clone,
OrdEqHash,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
Identifiable,
)]
#[display(fmt = "{id} {mintable}{value_type}")]
#[id(type = "<AssetDefinition as Identifiable>::Id")]
pub struct NewAssetDefinition {
id: <AssetDefinition as Identifiable>::Id,
value_type: AssetValueType,
Expand Down Expand Up @@ -439,20 +424,6 @@ impl HasMetadata for NewAssetDefinition {
}
}

impl PartialOrd for NewAssetDefinition {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for NewAssetDefinition {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}

impl NewAssetDefinition {
/// Create a [`NewAssetDefinition`], reserved for internal use.
fn new(id: <AssetDefinition as Identifiable>::Id, value_type: AssetValueType) -> Self {
Expand Down Expand Up @@ -601,26 +572,10 @@ impl Id {
}
}

impl Identifiable for Asset {
type Id = Id;

fn id(&self) -> &Self::Id {
&self.id
}
}

impl Registered for Asset {
type With = Self;
}

impl Identifiable for AssetDefinition {
type Id = DefinitionId;

fn id(&self) -> &Self::Id {
&self.id
}
}

impl Registered for AssetDefinition {
type With = NewAssetDefinition;
}
Expand Down
60 changes: 18 additions & 42 deletions data_model/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};
use core::{cmp::Ordering, str::FromStr};
use core::str::FromStr;

use derive_more::{Display, FromStr};
use getset::{Getters, MutGetters};
use iroha_crypto::PublicKey;
use iroha_data_model_derive::OrdEqHash;
#[cfg(feature = "ffi_api")]
use iroha_ffi::ffi_bindgen;
use iroha_primitives::conststr::ConstString;
Expand Down Expand Up @@ -72,16 +73,26 @@ impl From<GenesisDomain> for Domain {

/// Builder which can be submitted in a transaction to create a new [`Domain`]
#[derive(
Debug, Display, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema,
Debug,
Display,
Clone,
OrdEqHash,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
Identifiable,
)]
#[allow(clippy::multiple_inherent_impl)]
#[display(fmt = "[{id}]")]
#[id(type = "<Domain as Identifiable>::Id")]
pub struct NewDomain {
/// The identification associated to the domain builder.
/// The identification associated with the domain builder.
id: <Domain as Identifiable>::Id,
/// The (IPFS) link to the logo of this domain.
logo: Option<IpfsPath>,
/// metadata associated to the domain builder.
/// Metadata associated with the domain builder.
metadata: Metadata,
}

Expand Down Expand Up @@ -109,20 +120,6 @@ impl HasMetadata for NewDomain {
}
}

impl PartialOrd for NewDomain {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for NewDomain {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}

impl NewDomain {
/// Create a [`NewDomain`], reserved for internal use.
#[must_use]
Expand Down Expand Up @@ -162,19 +159,20 @@ impl NewDomain {
Debug,
Display,
Clone,
PartialEq,
Eq,
OrdEqHash,
Getters,
MutGetters,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
Identifiable,
)]
#[allow(clippy::multiple_inherent_impl)]
#[cfg_attr(feature = "ffi_api", ffi_bindgen)]
#[display(fmt = "[{id}]")]
#[id(type = "Id")]
pub struct Domain {
/// Identification of this [`Domain`].
id: <Self as Identifiable>::Id,
Expand All @@ -197,32 +195,10 @@ impl HasMetadata for Domain {
}
}

impl Identifiable for Domain {
type Id = Id;

fn id(&self) -> &Self::Id {
&self.id
}
}

impl Registered for Domain {
type With = NewDomain;
}

impl PartialOrd for Domain {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Domain {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.id().cmp(other.id())
}
}

#[cfg_attr(feature = "ffi_api", ffi_bindgen)]
impl Domain {
/// Construct builder for [`Domain`] identifiable by [`Id`].
Expand Down
Loading

0 comments on commit 179ca40

Please sign in to comment.