Skip to content

Commit

Permalink
Merge pull request #30 from gmoratorio/add-custom-deserializer
Browse files Browse the repository at this point in the history
add custom deserializer for Decimal in DexPairOHLC
  • Loading branch information
Vardominator authored Nov 18, 2024
2 parents 5431d31 + 61e0d24 commit 55b8d16
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "maestro-rust-sdk"
version = "1.2.4"
version = "1.2.5"
description = "Rust SDK for the Maestro Dapp Platform"
repository = "https://github.com/maestro-org/rust-sdk"
documentation = "https://docs.gomaestro.org/"
Expand Down
22 changes: 22 additions & 0 deletions src/models/markets.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde::de::{self, Deserializer};
use rust_decimal::prelude::FromPrimitive;

fn deserialize_decimal_from_f64<'de, D>(deserializer: D) -> Result<Decimal, D::Error>
where
D: Deserializer<'de>,
{
let float = f64::deserialize(deserializer)?;
Decimal::from_f64(float).ok_or_else(|| de::Error::custom("Failed to convert float to Decimal"))
}

#[derive(Clone, Debug, Deserialize)]
pub struct DexPairOHLC {
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_a_change_pct: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_a_close: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_a_high: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_a_low: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_a_open: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_a_volume: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_b_change_pct: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_b_close: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_b_high: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_b_low: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_b_open: Decimal,
#[serde(deserialize_with = "deserialize_decimal_from_f64")]
pub coin_b_volume: Decimal,
pub count: i64,
pub timestamp: DateTime<Utc>,
Expand Down

0 comments on commit 55b8d16

Please sign in to comment.