Skip to content

Commit

Permalink
PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Jul 31, 2023
1 parent 81b9d84 commit 121a4ba
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 504 deletions.
23 changes: 1 addition & 22 deletions sdk/src/types/block/address/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for AccountAddress {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = AccountAddressDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid account address type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for AccountAddress {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
AccountAddressDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(AccountAddress, AccountAddressDto, "account address");
}
23 changes: 1 addition & 22 deletions sdk/src/types/block/address/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for Ed25519Address {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = Ed25519AddressDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid ed25519 address type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for Ed25519Address {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
Ed25519AddressDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(Ed25519Address, Ed25519AddressDto, "ed25519 address");
}
23 changes: 1 addition & 22 deletions sdk/src/types/block/address/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for NftAddress {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = NftAddressDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid nft address type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for NftAddress {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
NftAddressDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(NftAddress, NftAddressDto, "nft address");
}
11 changes: 7 additions & 4 deletions sdk/src/types/block/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,12 @@ pub(crate) mod dto {
use serde::{Deserialize, Serialize};

use super::*;
use crate::types::{
block::{payload::dto::PayloadDto, Error},
TryFromDto, ValidationParams,
use crate::{
types::{
block::{payload::dto::PayloadDto, Error},
TryFromDto, ValidationParams,
},
utils::serde::string,
};

/// The block object that nodes gossip around in the network.
Expand All @@ -302,7 +305,7 @@ pub(crate) mod dto {
///
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payload: Option<PayloadDto>,
#[serde(with = "crate::utils::serde::string")]
#[serde(with = "string")]
pub burned_mana: u64,
}

Expand Down
23 changes: 1 addition & 22 deletions sdk/src/types/block/input/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for UtxoInput {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = UtxoInputDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid UTXO input type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
dto.try_into().map_err(serde::de::Error::custom)
}
}

impl Serialize for UtxoInput {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
UtxoInputDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(UtxoInput, UtxoInputDto, "UTXO input");
}
29 changes: 29 additions & 0 deletions sdk/src/types/block/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,32 @@ macro_rules! create_bitflags {
};
}
pub(crate) use create_bitflags;

#[macro_export]
macro_rules! impl_serde_typed_dto {
($base:ty, $dto:ty, $type_str:literal) => {
impl<'de> Deserialize<'de> for $base {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = <$dto>::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid {} type: expected {}, found {}",
$type_str,
Self::KIND,
dto.kind
)));
}
dto.try_into().map_err(serde::de::Error::custom)
}
}

impl Serialize for $base {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
<$dto>::from(self).serialize(s)
}
}
};
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ pub(crate) mod dto {
// Amount of IOTA tokens held by the output.
#[serde(with = "string")]
pub amount: u64,
#[serde(with = "crate::utils::serde::string")]
#[serde(with = "string")]
pub mana: u64,
// Native tokens held by the output.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ pub(crate) mod dto {
// Amount of IOTA tokens held by the output.
#[serde(with = "string")]
pub amount: u64,
#[serde(with = "crate::utils::serde::string")]
#[serde(with = "string")]
pub mana: u64,
// Native tokens held by the output.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
Expand Down
23 changes: 1 addition & 22 deletions sdk/src/types/block/output/feature/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for IssuerFeature {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = IssuerFeatureDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid issuer feature type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for IssuerFeature {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
IssuerFeatureDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(IssuerFeature, IssuerFeatureDto, "issuer feature");
}
23 changes: 1 addition & 22 deletions sdk/src/types/block/output/feature/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for MetadataFeature {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = MetadataFeatureDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid metadata feature type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for MetadataFeature {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
MetadataFeatureDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(MetadataFeature, MetadataFeatureDto<'_>, "metadata feature");
}
23 changes: 1 addition & 22 deletions sdk/src/types/block/output/feature/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for SenderFeature {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = SenderFeatureDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid sender feature type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for SenderFeature {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
SenderFeatureDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(SenderFeature, SenderFeatureDto, "sender feature");
}
23 changes: 1 addition & 22 deletions sdk/src/types/block/output/feature/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for StakingFeature {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = StakingFeatureDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid staking feature type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for StakingFeature {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
StakingFeatureDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(StakingFeature, StakingFeatureDto, "staking feature");
}
23 changes: 1 addition & 22 deletions sdk/src/types/block/output/feature/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for TagFeature {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = TagFeatureDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid tag feature type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for TagFeature {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
TagFeatureDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(TagFeature, TagFeatureDto<'_>, "tag feature");
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ pub(crate) mod dto {
// Amount of IOTA tokens held by the output.
#[serde(with = "string")]
pub amount: u64,
#[serde(with = "crate::utils::serde::string")]
#[serde(with = "string")]
pub mana: u64,
// Native tokens held by the output.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
Expand Down
23 changes: 1 addition & 22 deletions sdk/src/types/block/output/token_scheme/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,5 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for SimpleTokenScheme {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = SimpleTokenSchemeDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid simple token scheme type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
dto.try_into().map_err(serde::de::Error::custom)
}
}

impl Serialize for SimpleTokenScheme {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
SimpleTokenSchemeDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(SimpleTokenScheme, SimpleTokenSchemeDto, "simple token scheme");
}
27 changes: 5 additions & 22 deletions sdk/src/types/block/output/unlock_condition/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,9 @@ mod dto {
}
}

impl<'de> Deserialize<'de> for AddressUnlockCondition {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let dto = AddressUnlockConditionDto::deserialize(d)?;
if dto.kind != Self::KIND {
return Err(serde::de::Error::custom(format!(
"invalid address unlock condition type: expected {}, found {}",
Self::KIND,
dto.kind
)));
}
Ok(dto.into())
}
}

impl Serialize for AddressUnlockCondition {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
AddressUnlockConditionDto::from(self).serialize(s)
}
}
impl_serde_typed_dto!(
AddressUnlockCondition,
AddressUnlockConditionDto,
"address unlock condition"
);
}
Loading

0 comments on commit 121a4ba

Please sign in to comment.