From 46d2ca73f1badfa7081b1f8300b3e43170f13914 Mon Sep 17 00:00:00 2001 From: paulobressan Date: Thu, 5 Oct 2023 18:41:04 -0300 Subject: [PATCH] fix: added default for i128 to string --- src/model.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/model.rs b/src/model.rs index ead944d7..23da94f1 100644 --- a/src/model.rs +++ b/src/model.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use merge::Merge; -use serde::{Deserialize, Serialize}; +use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer}; use serde_json::Value as JsonValue; use strum_macros::Display; @@ -29,6 +29,8 @@ pub enum Era { pub enum MetadatumRendition { MapJson(JsonValue), ArrayJson(JsonValue), + #[serde(serialize_with = "serialize_int_scalar")] + #[serde(deserialize_with = "deserialize_int_scalar")] IntScalar(i128), TextScalar(String), BytesHex(String), @@ -374,3 +376,18 @@ pub struct Event { pub fingerprint: Option, } + +fn serialize_int_scalar(value: &i128, serializer: S) -> Result +where + S: Serializer, +{ + value.to_string().serialize(serializer) +} + +fn deserialize_int_scalar<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let value: String = Deserialize::deserialize(deserializer)?; + Ok(value.parse().map_err(DeError::custom)?) +}