Skip to content

Commit

Permalink
[refactor] hyperledger-iroha#2253: Add Registrable trait to `data_m…
Browse files Browse the repository at this point in the history
…odel`

Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
  • Loading branch information
Erigara committed Jul 22, 2022
1 parent 162d8e8 commit 425e7fc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 64 deletions.
2 changes: 1 addition & 1 deletion client/tests/integration/queries/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn find_role_by_id() -> Result<()> {

let found_role = test_client.request(client::role::by_id(role_id))?;

assert_eq!(found_role.id(), new_role.id());
assert_eq!(found_role.id(), new_role.build().id());
assert!(found_role.permissions().next().is_none());

Ok(())
Expand Down
39 changes: 21 additions & 18 deletions data_model/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use iroha_schema::IntoSchema;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

#[cfg(feature = "mutable_api")]
use crate::Registrable;
use crate::{
asset::{prelude::AssetId, AssetsMap},
domain::prelude::*,
Expand Down Expand Up @@ -129,18 +131,29 @@ impl Default for SignatureCheckCondition {
#[display(fmt = "[{id}]")]
pub struct NewAccount {
/// Identification
id: <NewAccount as Identifiable>::Id,
id: <Account as Identifiable>::Id,
/// Signatories, i.e. signatures attached to this message.
signatories: Signatories,
/// Metadata that should be submitted with the builder
metadata: Metadata,
}

impl Identifiable for NewAccount {
type Id = <Account as Identifiable>::Id;
#[cfg(feature = "mutable_api")]
impl Registrable for NewAccount {
type Target = Account;

fn id(&self) -> &Self::Id {
&self.id
#[must_use]
#[inline]
fn build(self) -> Self::Target {
Self::Target {
id: self.id,
signatories: self.signatories,
assets: AssetsMap::default(),
permission_tokens: Permissions::default(),
signature_check_condition: SignatureCheckCondition::default(),
metadata: self.metadata,
roles: RoleIds::default(),
}
}
}

Expand Down Expand Up @@ -176,19 +189,9 @@ impl NewAccount {
}
}

/// Construct [`Account`]
#[must_use]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> Account {
Account {
id: self.id,
signatories: self.signatories,
assets: AssetsMap::default(),
permission_tokens: Permissions::default(),
signature_check_condition: SignatureCheckCondition::default(),
metadata: self.metadata,
roles: RoleIds::default(),
}
/// Identification
pub fn id(&self) -> &<Account as Identifiable>::Id {
&self.id
}
}

Expand Down
29 changes: 15 additions & 14 deletions data_model/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,19 @@ pub struct NewAssetDefinition {
metadata: Metadata,
}

impl Identifiable for NewAssetDefinition {
type Id = <AssetDefinition as Identifiable>::Id;
#[cfg(feature = "mutable_api")]
impl crate::Registrable for NewAssetDefinition {
type Target = AssetDefinition;

fn id(&self) -> &Self::Id {
&self.id
#[must_use]
#[inline]
fn build(self) -> Self::Target {
Self::Target {
id: self.id,
value_type: self.value_type,
mintable: self.mintable,
metadata: self.metadata,
}
}
}

Expand Down Expand Up @@ -455,17 +463,10 @@ impl NewAssetDefinition {
}
}

/// Construct [`AssetDefinition`]
/// Identification
#[inline]
#[must_use]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> AssetDefinition {
AssetDefinition {
id: self.id,
value_type: self.value_type,
mintable: self.mintable,
metadata: self.metadata,
}
pub fn id(&self) -> &<AssetDefinition as Identifiable>::Id {
&self.id
}
}

Expand Down
39 changes: 20 additions & 19 deletions data_model/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ pub struct NewDomain {
metadata: Metadata,
}

#[cfg(feature = "mutable_api")]
impl crate::Registrable for NewDomain {
type Target = Domain;

#[must_use]
#[inline]
fn build(self) -> Self::Target {
Self::Target {
id: self.id,
accounts: AccountsMap::default(),
asset_definitions: AssetDefinitionsMap::default(),
metadata: self.metadata,
logo: self.logo,
}
}
}

impl HasMetadata for NewDomain {
#[inline]
fn metadata(&self) -> &crate::metadata::Metadata {
Expand Down Expand Up @@ -116,17 +133,9 @@ impl NewDomain {
}
}

/// Construct [`Domain`]
#[must_use]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> Domain {
Domain {
id: self.id,
accounts: AccountsMap::default(),
asset_definitions: AssetDefinitionsMap::default(),
metadata: self.metadata,
logo: self.logo,
}
/// Identification
pub fn id(&self) -> &<Domain as Identifiable>::Id {
&self.id
}
}

Expand All @@ -147,14 +156,6 @@ impl NewDomain {
}
}

impl Identifiable for NewDomain {
type Id = <Domain as Identifiable>::Id;

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

/// Named group of [`Account`] and [`Asset`](`crate::asset::Asset`) entities.
#[derive(
Debug,
Expand Down
12 changes: 12 additions & 0 deletions data_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,16 @@ pub trait Registered: Identifiable {
type With: Into<RegistrableBox>;
}

/// Trait for proxy objects used for registration.
#[cfg(feature = "mutable_api")]
pub trait Registrable {
/// Constructed type
type Target;

/// Construct [`Self::Target`]
fn build(self) -> Self::Target;
}

/// Limits of length of the identifiers (e.g. in [`domain::Domain`], [`account::Account`], [`asset::AssetDefinition`]) in number of chars
#[derive(Debug, Clone, Copy, PartialEq, Eq, Decode, Encode, Deserialize, Serialize)]
pub struct LengthLimits {
Expand Down Expand Up @@ -856,6 +866,8 @@ pub mod prelude {
//! Prelude: re-export of most commonly used traits, structs and macros in this crate.
#[cfg(feature = "std")]
pub use super::current_time;
#[cfg(feature = "mutable_api")]
pub use super::Registrable;
pub use super::{
account::prelude::*,
asset::prelude::*,
Expand Down
24 changes: 12 additions & 12 deletions data_model/src/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ pub struct NewRole {
inner: Role,
}

impl Identifiable for NewRole {
type Id = <Role as Identifiable>::Id;
#[cfg(feature = "mutable_api")]
impl crate::Registrable for NewRole {
type Target = Role;

#[must_use]
#[inline]
fn id(&self) -> &Self::Id {
&self.inner.id
fn build(self) -> Self::Target {
self.inner
}
}

Expand Down Expand Up @@ -166,21 +168,19 @@ impl NewRole {
}
}

/// Identification
#[inline]
pub fn id(&self) -> &<Role as Identifiable>::Id {
&self.inner.id
}

/// Add permission to the [`Role`]
#[must_use]
#[inline]
pub fn add_permission(mut self, perm: impl Into<PermissionToken>) -> Self {
self.inner.permissions.insert(perm.into());
self
}

/// Construct [`Role`]
#[must_use]
#[inline]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> Role {
self.inner
}
}

/// The prelude re-exports most commonly used traits, structs and macros from this module.
Expand Down

0 comments on commit 425e7fc

Please sign in to comment.