bnr_xfs/currency/
cash_order.rs

1use std::fmt;
2
3use crate::Result;
4use crate::{device_handle::CallbackArg, impl_xfs_struct};
5
6use super::{Currency, Denomination};
7
8/// Represents a cash order event initiated by the device.
9#[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    /// Creates a new [CashOrder].
18    pub const fn new() -> Self {
19        Self {
20            currency: Currency::new(),
21            denomination: Denomination::new(),
22        }
23    }
24
25    /// Creates a new [CashOrder] from the provided parameters.
26    pub const fn create(currency: Currency, denomination: Denomination) -> Self {
27        Self {
28            currency,
29            denomination,
30        }
31    }
32
33    /// Gets a reference to the [Currency].
34    pub const fn currency(&self) -> &Currency {
35        &self.currency
36    }
37
38    /// Gets a mutable reference to the [Currency].
39    pub fn currency_mut(&mut self) -> &mut Currency {
40        &mut self.currency
41    }
42
43    /// Sets the [Currency].
44    pub fn set_currency(&mut self, currency: Currency) {
45        self.currency = currency;
46    }
47
48    /// Builder function that sets the [Currency].
49    pub fn with_currency(mut self, currency: Currency) -> Self {
50        self.set_currency(currency);
51        self
52    }
53
54    /// Gets a reference to the [Denomination].
55    pub const fn denomination(&self) -> &Denomination {
56        &self.denomination
57    }
58
59    /// Gets a mutable reference to the [Denomination].
60    pub fn denomination_mut(&mut self) -> &mut Denomination {
61        &mut self.denomination
62    }
63
64    /// Sets the [Denomination].
65    pub fn set_denomination(&mut self, denomination: Denomination) {
66        self.denomination = denomination;
67    }
68
69    /// Builder function that sets the [Denomination].
70    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]);