bnr_xfs/currency/
cash_order.rs1use std::fmt;
2
3use crate::Result;
4use crate::{device_handle::CallbackArg, impl_xfs_struct};
5
6use super::{Currency, Denomination};
7
8#[repr(C)]
10#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
11pub struct CashOrder {
12 currency: Currency,
13 denomination: Denomination,
14}
15
16impl CashOrder {
17 pub const fn new() -> Self {
19 Self {
20 currency: Currency::new(),
21 denomination: Denomination::new(),
22 }
23 }
24
25 pub const fn create(currency: Currency, denomination: Denomination) -> Self {
27 Self {
28 currency,
29 denomination,
30 }
31 }
32
33 pub const fn currency(&self) -> &Currency {
35 &self.currency
36 }
37
38 pub fn currency_mut(&mut self) -> &mut Currency {
40 &mut self.currency
41 }
42
43 pub fn set_currency(&mut self, currency: Currency) {
45 self.currency = currency;
46 }
47
48 pub fn with_currency(mut self, currency: Currency) -> Self {
50 self.set_currency(currency);
51 self
52 }
53
54 pub const fn denomination(&self) -> &Denomination {
56 &self.denomination
57 }
58
59 pub fn denomination_mut(&mut self) -> &mut Denomination {
61 &mut self.denomination
62 }
63
64 pub fn set_denomination(&mut self, denomination: Denomination) {
66 self.denomination = denomination;
67 }
68
69 pub fn with_denomination(mut self, denomination: Denomination) -> Self {
71 self.set_denomination(denomination);
72 self
73 }
74}
75
76impl fmt::Display for CashOrder {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 write!(f, "{{")?;
79 write!(f, r#""currency": {}, "#, self.currency)?;
80 write!(f, r#""denomination": {}"#, self.denomination)?;
81 write!(f, "}}")
82 }
83}
84
85impl CallbackArg for CashOrder {
86 fn value(&self) -> i32 {
87 let cash_order = self as &CashOrder;
88 cash_order
89 .currency()
90 .from_mdu_value(cash_order.denomination().amount()) as i32
91 }
92
93 fn is_null(&self) -> bool {
94 false
95 }
96
97 fn is_cash_order(&self) -> bool {
98 true
99 }
100
101 fn as_cash_order(&self) -> Result<&CashOrder> {
102 Ok(self as &CashOrder)
103 }
104
105 fn as_cash_order_mut(&mut self) -> Result<&mut CashOrder> {
106 Ok(self as &mut CashOrder)
107 }
108}
109
110impl_xfs_struct!(CashOrder, "cashOrder", [currency: Currency, denomination: Denomination]);