Skip to content

Commit

Permalink
Add serde support for AssetId.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Nov 5, 2024
1 parent ad286cd commit 674ac16
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ members = [
# Our crates
polymesh-api-codegen-macro = { version = "3.6.1", path = "crates/polymesh-api-codegen-macro", default-features = false }
polymesh-api-codegen = { version = "3.6.1", path = "crates/polymesh-api-codegen", default-features = false }
polymesh-api-client = { version = "3.8.0", path = "crates/polymesh-api-client", default-features = false }
polymesh-api-client = { version = "3.8.1", path = "crates/polymesh-api-client", default-features = false }
polymesh-api-client-extras = { version = "3.4.1", path = "crates/polymesh-api-client-extras", default-features = false }
polymesh-api-tester = { version = "0.8.0", path = "crates/polymesh-api-tester", default-features = false }
polymesh-api-ink = { version = "1.5.0", path = "crates/polymesh-api-ink", default-features = false }
Expand Down
5 changes: 4 additions & 1 deletion crates/polymesh-api-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polymesh-api-client"
version = "3.8.0"
version = "3.8.1"
edition = "2021"
authors = ["Robert G. Jakabosky <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -24,6 +24,7 @@ serde_json = { workspace = true, features = ["preserve_order", "arbitrary_precis
serde = { workspace = true }
impl-serde = { workspace = true, default-features = false, optional = true }
serde-big-array = { workspace = true, optional = true }
uuid = { version = "1.11", default-features = false }

# substrate
codec = { version = "3", package = "parity-scale-codec", default-features = false }
Expand Down Expand Up @@ -81,6 +82,7 @@ serde = [
"impl-serde",
"serde-big-array",
"hex/serde",
"uuid/serde",
"sp-core/serde",
"sp-core/impl-serde",
"sp-weights/serde",
Expand All @@ -96,6 +98,7 @@ v14 = ["frame-metadata/v14"]

std = [
"codec/std",
"uuid/std",
"frame-metadata/std",
"sp-core/std",
"sp-std/std",
Expand Down
90 changes: 89 additions & 1 deletion crates/polymesh-api-client/src/basic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,77 @@ impl<'de> Deserialize<'de> for IdentityId {

#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
#[cfg_attr(all(feature = "std", feature = "type_info"), derive(TypeInfo))]
pub struct AssetId([u8; 16]);
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct AssetId(
#[cfg_attr(
feature = "utoipa",
schema(example = "67e55044-10b1-426f-9247-bb680e5fe0c8")
)]
pub [u8; 16],
);

impl AssetId {
pub fn as_uuid(&self) -> uuid::Uuid {
uuid::Uuid::from_bytes(self.0)
}
}

impl fmt::Display for AssetId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_uuid().fmt(f)
}
}

impl fmt::Debug for AssetId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_uuid().fmt(f)
}
}

impl From<uuid::Uuid> for AssetId {
fn from(uuid: uuid::Uuid) -> Self {
Self(uuid.into_bytes())
}
}

impl std::str::FromStr for AssetId {
type Err = uuid::Error;

fn from_str(uuid_str: &str) -> Result<Self, Self::Err> {
let uuid = uuid::Uuid::parse_str(uuid_str)?;
Ok(uuid.into())
}
}

impl TryFrom<&'_ str> for AssetId {
type Error = uuid::Error;

fn try_from(uuid_str: &'_ str) -> Result<Self, Self::Error> {
let uuid = uuid::Uuid::parse_str(uuid_str)?;
Ok(uuid.into())
}
}

#[cfg(feature = "serde")]
impl Serialize for AssetId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
self.as_uuid().serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for AssetId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let uuid: uuid::Uuid = Deserialize::deserialize(deserializer)?;
Ok(Self(uuid.into_bytes()))
}
}

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -736,6 +806,24 @@ mod tests {
assert_eq!(decoded, account);
}

#[test]
fn asset_id_roundtrip() {
let asset: AssetId = "67e55044-10b1-426f-9247-bb680e5fe0c8"
.parse()
.expect("AssetId");
let data = serde_json::to_string(&asset).expect("encode json");
let decoded: AssetId = serde_json::from_str(&data).expect("decode as json");
assert_eq!(decoded, asset);
}

#[test]
fn asset_id_display() {
let str_id = "67e55044-10b1-426f-9247-bb680e5fe0c8";
let asset: AssetId = str_id.parse().expect("AssetId");
let display_id = format!("{asset}");
assert_eq!(display_id, str_id);
}

#[test]
fn multi_address_roundtrip() {
let account = AccountId::from_ss58check("5Enm3VfZioxHVBpZgJcACv7pZPZZeYWymvrZ9cxkXhNHJWe5")
Expand Down

0 comments on commit 674ac16

Please sign in to comment.