bnr_xfs/
dispense.rs

1//! Types for handling dispense requests.
2
3use std::fmt;
4
5use crate::currency::{Currency, Denomination, MixNumber};
6use crate::impl_xfs_struct;
7use crate::xfs::method_call::XfsMethodCall;
8use crate::xfs::params::{XfsParam, XfsParams};
9use crate::xfs::value::XfsValue;
10
11/// Structure that defines the parameters of `bnr_Dispense()` or `bnr_Denominate()`, to specify
12/// either an amount or a list of banknotes to dispense.
13#[repr(C)]
14#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
15pub struct DispenseRequest {
16    mix_number: MixNumber,
17    denomination: Denomination,
18    currency: Currency,
19}
20
21impl DispenseRequest {
22    /// Creates a new [DispenseRequest].
23    pub const fn new() -> Self {
24        Self {
25            mix_number: MixNumber::new(),
26            denomination: Denomination::new(),
27            currency: Currency::new(),
28        }
29    }
30
31    /// Gets the [MixNumber] of the [DispenseRequest].
32    pub const fn mix_number(&self) -> MixNumber {
33        self.mix_number
34    }
35
36    /// Sets the [MixNumber] of the [DispenseRequest].
37    pub fn set_mix_number(&mut self, mix: MixNumber) {
38        self.mix_number = mix;
39    }
40
41    /// Builder function that sets the [MixNumber] of the [DispenseRequest].
42    pub fn with_mix_number(mut self, mix: MixNumber) -> Self {
43        self.set_mix_number(mix);
44        self
45    }
46
47    /// Gets the [Denomination] of the [DispenseRequest].
48    pub const fn denomination(&self) -> &Denomination {
49        &self.denomination
50    }
51
52    /// Sets the [Denomination] of the [DispenseRequest].
53    pub fn set_denomination(&mut self, denomination: Denomination) {
54        self.denomination = denomination;
55    }
56
57    /// Builder function that sets the [Denomination] of the [DispenseRequest].
58    pub fn with_denomination(mut self, denomination: Denomination) -> Self {
59        self.set_denomination(denomination);
60        self
61    }
62
63    /// Gets the [Currency] of the [DispenseRequest].
64    pub const fn currency(&self) -> &Currency {
65        &self.currency
66    }
67
68    /// Sets the [Currency] of the [DispenseRequest].
69    pub fn set_currency(&mut self, currency: Currency) {
70        self.currency = currency;
71    }
72
73    /// Builder function that sets the [Currency] of the [DispenseRequest].
74    pub fn with_currency(mut self, currency: Currency) -> Self {
75        self.set_currency(currency);
76        self
77    }
78}
79
80impl fmt::Display for DispenseRequest {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        write!(f, "{{")?;
83        write!(f, r#""mix_number": {}, "#, self.mix_number)?;
84        write!(f, r#""denomination": {}, "#, self.denomination)?;
85        write!(f, r#""currency": {}"#, self.currency)?;
86        write!(f, "}}")
87    }
88}
89
90impl_xfs_struct!(
91    DispenseRequest,
92    "dispenseRequest",
93    [
94        mix_number: MixNumber,
95        denomination: Denomination,
96        currency: Currency
97    ]
98);
99
100impl From<&DispenseRequest> for XfsMethodCall {
101    fn from(val: &DispenseRequest) -> Self {
102        Self::new().with_params(XfsParams::create([
103            XfsParam::create(XfsValue::new().with_i4(val.mix_number().inner() as i32)),
104            XfsParam::create(XfsValue::new().with_i4(val.denomination().amount() as i32)),
105            XfsParam::create(
106                XfsValue::new().with_string(<&str>::from(val.currency().currency_code())),
107            ),
108            XfsParam::create(XfsValue::new().with_array(val.denomination().items_raw().into())),
109            XfsParam::create(XfsValue::new().with_base64("ODAw")),
110        ]))
111    }
112}