Skip to content

Commit

Permalink
ease serde derive on ICS20 & ICS721 types
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Jan 25, 2024
1 parent c8d8c43 commit 6c23da7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 33 deletions.
1 change: 0 additions & 1 deletion ibc-apps/ics20-transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub mod types {
pub use ibc_app_transfer_types::*;
}

#[cfg(feature = "serde")]
pub mod context;
#[cfg(feature = "serde")]
pub mod handler;
Expand Down
4 changes: 2 additions & 2 deletions ibc-apps/ics20-transfer/types/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use super::error::TokenTransferError;
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Display, From, Into)]
pub struct Amount(
#[cfg_attr(feature = "schema", schemars(with = "String"))]
#[serde(serialize_with = "serializers::serialize")]
#[serde(deserialize_with = "deserialize")]
#[cfg_attr(feature = "serde", serde(serialize_with = "serializers::serialize"))]
#[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize"))]
U256,
);

Expand Down
19 changes: 5 additions & 14 deletions ibc-apps/ics20-transfer/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,18 @@
#[cfg(any(test, feature = "std"))]
extern crate std;

#[cfg(feature = "serde")]
mod amount;
#[cfg(feature = "serde")]
pub use amount::*;
#[cfg(feature = "serde")]
mod coin;
#[cfg(feature = "serde")]
pub use coin::*;
#[cfg(feature = "serde")]
mod denom;
#[cfg(feature = "serde")]
mod memo;

pub use amount::*;
pub use coin::*;
pub use denom::*;
#[cfg(feature = "serde")]
pub mod error;
pub mod events;
#[cfg(feature = "serde")]
pub mod msgs;
#[cfg(feature = "serde")]
pub mod packet;

pub mod error;
mod memo;
pub use memo::*;
/// Re-exports `U256` from `primitive-types` crate for convenience.
pub use primitive_types::U256;
Expand Down
6 changes: 6 additions & 0 deletions ibc-apps/ics721-nft-transfer/types/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;

#[cfg(feature = "serde")]
use base64::prelude::BASE64_STANDARD;
#[cfg(feature = "serde")]
use base64::Engine;
use ibc_core::primitives::prelude::*;
use mime::Mime;
Expand All @@ -25,6 +27,7 @@ use crate::error::NftTransferError;
#[derive(Clone, Debug, Default, PartialEq, Eq, derive_more::From)]
pub struct Data(String);

#[cfg(feature = "serde")]
impl Data {
/// Parses the data in the format specified by ICS-721.
pub fn parse_as_ics721_data(&self) -> Result<Ics721Data, NftTransferError> {
Expand All @@ -46,6 +49,7 @@ impl FromStr for Data {
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for Data {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -55,6 +59,7 @@ impl serde::Serialize for Data {
}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Data {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -86,6 +91,7 @@ impl<'de> serde::Deserialize<'de> for Data {
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Ics721Data(BTreeMap<String, DataValue>);

#[cfg(feature = "serde")]
impl FromStr for Ics721Data {
type Err = NftTransferError;

Expand Down
21 changes: 6 additions & 15 deletions ibc-apps/ics721-nft-transfer/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,19 @@
#[cfg(any(test, feature = "std"))]
extern crate std;

#[cfg(feature = "serde")]
mod class;
#[cfg(feature = "serde")]
pub use class::*;
#[cfg(feature = "serde")]
mod data;
#[cfg(feature = "serde")]
pub use data::*;
#[cfg(feature = "serde")]
mod memo;
mod token;

pub mod events;
#[cfg(feature = "serde")]
pub mod msgs;
#[cfg(feature = "serde")]
pub use class::*;
pub use data::*;
pub mod packet;
#[cfg(feature = "serde")]
mod token;
#[cfg(feature = "serde")]
pub use memo::*;
pub use token::*;

pub mod error;
mod memo;
pub use memo::*;

/// Re-exports ICS-721 NFT transfer proto types from the `ibc-proto` crate.
pub mod proto {
Expand Down
2 changes: 1 addition & 1 deletion ibc-apps/ics721-nft-transfer/types/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::token::{TokenData, TokenIds, TokenUri};

/// Defines the structure of token transfers' packet bytes
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(
feature = "parity-scale-codec",
Expand Down

0 comments on commit 6c23da7

Please sign in to comment.