1use std::fmt;
4
5use crate::impl_xfs_struct;
6
7mod cash_order;
8mod cash_type;
9mod code;
10mod cu_enum;
11mod denomination;
12mod exponent;
13mod mix;
14
15pub use cash_order::*;
16pub use cash_type::*;
17pub use code::*;
18pub use cu_enum::*;
19pub use denomination::*;
20pub use exponent::*;
21pub use mix::*;
22
23#[repr(C)]
25#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
26pub struct Currency {
27 currency_code: CurrencyCode,
28 exponent: Exponent,
29}
30
31impl Currency {
32 pub const fn new() -> Self {
34 Self {
35 currency_code: CurrencyCode::new(),
36 exponent: Exponent::new(),
37 }
38 }
39
40 pub fn currency_code(&self) -> CurrencyCode {
42 self.currency_code
43 }
44
45 pub fn exponent(&self) -> i32 {
50 self.exponent.inner()
51 }
52
53 pub fn to_mdu_value(&self, value: u32) -> u32 {
64 (value as f32 * 10f32.powf(self.exponent.inner().abs() as f32)) as u32
65 }
66
67 pub fn from_mdu_value(&self, value: u32) -> u32 {
78 (value as f32 * 10f32.powf(self.exponent.inner() as f32)) as u32
79 }
80}
81
82impl From<CurrencyCode> for Currency {
83 fn from(val: CurrencyCode) -> Self {
84 let exponent: Exponent = val.into();
85
86 Self {
87 currency_code: val,
88 exponent,
89 }
90 }
91}
92
93impl fmt::Display for Currency {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 write!(
96 f,
97 r#""currency": {{"currency_code": "{}", "exponent": {}}}"#,
98 self.currency_code, self.exponent
99 )
100 }
101}
102
103impl_xfs_struct!(Currency, "currency", [currency_code: CurrencyCode, exponent: Exponent]);