bullet_exchange_interface/decimals/
surrogate_decimal.rs1use std::str::FromStr;
2
3use borsh::{BorshDeserialize, BorshSerialize};
4use rust_decimal::Decimal;
5use schemars;
6
7use super::TryDecimalOps;
8use crate::error::ConfigError;
9
10#[derive(
11 BorshDeserialize, BorshSerialize, Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd,
12)]
13#[cfg_attr(feature = "schema", derive(sov_universal_wallet::UniversalWallet))]
14#[allow(dead_code)]
15pub struct SurrogateDecimal {
16 flags: u32,
17 hi: u32,
18 lo: u32,
19 mid: u32,
20}
21
22impl std::fmt::Debug for SurrogateDecimal {
23 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24 write!(f, "SurrogateDecimal({})", Decimal::from(*self))
25 }
26}
27
28impl std::fmt::Display for SurrogateDecimal {
29 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30 write!(f, "{}", Decimal::from(*self))
31 }
32}
33
34impl serde::Serialize for SurrogateDecimal {
36 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
37 where
38 S: serde::Serializer,
39 {
40 serde::Serialize::serialize(&Decimal::from(*self), serializer)
41 }
42}
43
44impl<'de> serde::Deserialize<'de> for SurrogateDecimal {
45 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
46 where
47 D: serde::Deserializer<'de>,
48 {
49 let decimal: Decimal = serde::Deserialize::deserialize(deserializer)?;
50 Ok(Self::from(decimal))
51 }
52}
53
54impl From<SurrogateDecimal> for Decimal {
55 fn from(surrogate: SurrogateDecimal) -> Decimal {
56 unsafe { std::mem::transmute::<SurrogateDecimal, Decimal>(surrogate) }
58 }
59}
60
61impl From<Decimal> for SurrogateDecimal {
62 fn from(decimal: Decimal) -> Self {
63 unsafe { std::mem::transmute::<Decimal, Self>(decimal) }
64 }
65}
66
67impl FromStr for SurrogateDecimal {
68 type Err = ConfigError;
69
70 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
71 Decimal::try_from_str(s).map(|x| x.into())
72 }
73}
74
75impl schemars::JsonSchema for SurrogateDecimal {
76 fn schema_name() -> String {
77 "Decimal".to_string()
78 }
79
80 fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
81 Decimal::json_schema(generator)
82 }
83}