|
@@ -1,5 +1,5 @@
|
|
|
use crate::Asset;
|
|
|
-use serde::{de, Deserialize, Serialize, Serializer};
|
|
|
+use serde::{de, ser::SerializeStruct, Deserialize, Serialize, Serializer};
|
|
|
|
|
|
/// The raw storage for cents, the more the better
|
|
|
pub type AmountCents = i128;
|
|
@@ -66,24 +66,27 @@ impl TryInto<Amount> for AnyAmount {
|
|
|
/// The `cents` and `Asset` must be used to store amounts in the storage
|
|
|
/// layer. Float or string representations should be used to display
|
|
|
#[derive(
|
|
|
- Clone,
|
|
|
- Debug,
|
|
|
- Eq,
|
|
|
- PartialEq,
|
|
|
- Serialize,
|
|
|
- Deserialize,
|
|
|
- borsh::BorshSerialize,
|
|
|
- borsh::BorshDeserialize,
|
|
|
+ Clone, Debug, Eq, PartialEq, Deserialize, borsh::BorshSerialize, borsh::BorshDeserialize,
|
|
|
)]
|
|
|
pub struct Amount {
|
|
|
asset: Asset,
|
|
|
- #[serde(
|
|
|
- deserialize_with = "deserialize_string_to_amount",
|
|
|
- serialize_with = "serialize_amount_as_string"
|
|
|
- )]
|
|
|
+ #[serde(deserialize_with = "deserialize_string_to_amount")]
|
|
|
cents: AmountCents,
|
|
|
}
|
|
|
|
|
|
+impl Serialize for Amount {
|
|
|
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
+ where
|
|
|
+ S: Serializer,
|
|
|
+ {
|
|
|
+ let mut state = serializer.serialize_struct("amount", 3)?;
|
|
|
+ state.serialize_field("asset", &self.asset)?;
|
|
|
+ state.serialize_field("cents", &self.cents.to_string())?;
|
|
|
+ state.serialize_field("human", &self.to_string())?;
|
|
|
+ state.end()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
fn deserialize_string_to_amount<'de, D>(deserializer: D) -> Result<AmountCents, D::Error>
|
|
|
where
|
|
|
D: de::Deserializer<'de>,
|
|
@@ -92,13 +95,6 @@ where
|
|
|
s.parse::<AmountCents>().map_err(serde::de::Error::custom)
|
|
|
}
|
|
|
|
|
|
-fn serialize_amount_as_string<S>(value: &AmountCents, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
-where
|
|
|
- S: Serializer,
|
|
|
-{
|
|
|
- serializer.serialize_str(&value.to_string())
|
|
|
-}
|
|
|
-
|
|
|
impl Amount {
|
|
|
/// Creates a new amount from an asset and cents
|
|
|
pub(crate) fn new(asset: Asset, cents: AmountCents) -> Self {
|