bnr_xfs/currency/
cash_type.rs

1use std::fmt;
2
3use crate::{create_xfs_i4, impl_xfs_array, impl_xfs_struct, Size};
4
5use super::CurrencyCode;
6
7pub const CASH_TYPE_LIST_LEN: usize = 14;
8
9create_xfs_i4!(Value, "value", "Represents the value of a [CashType].");
10
11create_xfs_i4!(
12    Variant,
13    "variant",
14    "Represents the variant of a [CashType]."
15);
16
17/// Represents a cash type ISO currency code, value, and variant.
18#[repr(C)]
19#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
20pub struct CashType {
21    currency_code: CurrencyCode,
22    value: Value,
23    variant: Variant,
24}
25
26impl CashType {
27    /// Creates a new [CashType].
28    pub const fn new() -> Self {
29        Self {
30            currency_code: CurrencyCode::new(),
31            value: Value::new(),
32            variant: Variant::new(),
33        }
34    }
35
36    /// Gets the [CurrencyCode].
37    pub const fn currency_code(&self) -> CurrencyCode {
38        self.currency_code
39    }
40
41    /// Gets the value.
42    pub const fn value(&self) -> u32 {
43        self.value.inner()
44    }
45
46    /// Gets the variant.
47    pub const fn variant(&self) -> u32 {
48        self.variant.inner()
49    }
50}
51
52impl fmt::Display for CashType {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "{{")?;
55        write!(f, r#""currency_code":{},"#, self.currency_code)?;
56        write!(f, r#""value":{},"#, self.value)?;
57        write!(f, r#""variant":{}"#, self.variant)?;
58        write!(f, "}}")
59    }
60}
61
62impl_xfs_struct!(CashType, "cashType", [currency_code: CurrencyCode, value: Value, variant: Variant]);
63
64/// [CashType] list used for LCU's [secondary cash_type](LogicalCashUnit::secondary_cash_types).
65#[repr(C)]
66#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
67pub struct CashTypeList {
68    size: Size,
69    items: [CashType; CASH_TYPE_LIST_LEN],
70}
71
72impl CashTypeList {
73    /// Creates a new [CashTypeList].
74    pub const fn new() -> Self {
75        Self {
76            size: Size::new(),
77            items: [CashType::new(); CASH_TYPE_LIST_LEN],
78        }
79    }
80
81    /// Gets the max size.
82    pub const fn max_size() -> usize {
83        CASH_TYPE_LIST_LEN
84    }
85
86    /// Gets the size.
87    pub const fn size(&self) -> u32 {
88        self.size.inner()
89    }
90
91    /// Sets the size.
92    pub fn set_size(&mut self, val: u32) {
93        self.size.set_inner(val);
94    }
95
96    /// Gets whether the size is within bounds.
97    pub const fn size_is_valid(&self) -> bool {
98        self.size.inner() as usize <= CASH_TYPE_LIST_LEN
99    }
100
101    /// Gets a reference to the list of [CashType]s.
102    pub fn items(&self) -> &[CashType] {
103        let size = self.size.inner() as usize;
104        if size <= CASH_TYPE_LIST_LEN {
105            self.items[..size].as_ref()
106        } else {
107            self.items.as_ref()
108        }
109    }
110
111    /// Gets a mutable reference to the list of [CashType]s.
112    pub fn items_mut(&mut self) -> &mut [CashType] {
113        let size = self.size.inner() as usize;
114        if size <= CASH_TYPE_LIST_LEN {
115            self.items[..size].as_mut()
116        } else {
117            self.items.as_mut()
118        }
119    }
120}
121
122impl fmt::Display for CashTypeList {
123    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124        write!(f, "{{")?;
125        write!(f, r#""size":{},"#, self.size)?;
126
127        write!(f, r#""items":["#)?;
128        for (i, item) in self.items().iter().enumerate() {
129            if i != 0 {
130                write!(f, ", ")?;
131            }
132            write!(f, "{item}")?;
133        }
134        write!(f, "]}}")
135    }
136}
137
138impl_xfs_array!(CashTypeList, "secondaryCashTypes");