bnr_xfs/
cash_unit.rs

1use std::fmt;
2
3use crate::xfs::method_response::XfsMethodResponse;
4use crate::{impl_xfs_struct, Error, Result, TransportCount};
5
6mod counters;
7mod lock;
8mod logical_cash_unit;
9mod number;
10mod pcu_name;
11mod physical_cash_unit;
12mod status;
13mod threshold;
14mod unit_id;
15
16pub use counters::*;
17pub use lock::*;
18pub use logical_cash_unit::*;
19pub use number::*;
20pub use pcu_name::*;
21pub use physical_cash_unit::*;
22pub use status::*;
23pub use threshold::*;
24pub use unit_id::*;
25
26/// Represents a cash unit in a BNR device.
27///
28/// Describes the entire set of [LogicalCashUnit]s and [PhysicalCashUnit]s present on a device.
29#[repr(C)]
30#[derive(Clone, Debug, Default, PartialEq)]
31pub struct CashUnit {
32    transport_count: TransportCount,
33    logical_cash_unit_list: LogicalCashUnitList,
34    physical_cash_unit_list: PhysicalCashUnitList,
35}
36
37impl CashUnit {
38    /// Creates a new [CashUnit].
39    pub fn new() -> Self {
40        Self {
41            transport_count: TransportCount::new(),
42            logical_cash_unit_list: LogicalCashUnitList::new(),
43            physical_cash_unit_list: PhysicalCashUnitList::new(),
44        }
45    }
46
47    /// Gets the [TransportCount].
48    pub const fn transport_count(&self) -> u32 {
49        self.transport_count.inner()
50    }
51
52    /// Sets the [TransportCount].
53    pub fn set_transport_count(&mut self, count: u32) {
54        self.transport_count.set_inner(count);
55    }
56
57    /// Builder function that sets the [TransportCount].
58    pub fn with_transport_count(mut self, count: u32) -> Self {
59        self.set_transport_count(count);
60        self
61    }
62
63    /// Gets a reference to the [LogicalCashUnitList].
64    pub const fn logical_cash_unit_list(&self) -> &LogicalCashUnitList {
65        &self.logical_cash_unit_list
66    }
67
68    /// Gets a mutable reference to the [LogicalCashUnitList].
69    pub fn logical_cash_unit_list_mut(&mut self) -> &mut LogicalCashUnitList {
70        &mut self.logical_cash_unit_list
71    }
72
73    /// Sets the [LogicalCashUnitList].
74    pub fn set_logical_cash_unit_list(&mut self, lcu: LogicalCashUnitList) {
75        self.logical_cash_unit_list = lcu;
76    }
77
78    /// Builder function that sets the [LogicalCashUnitList].
79    pub fn with_logical_cash_unit_list(mut self, lcu: LogicalCashUnitList) -> Self {
80        self.set_logical_cash_unit_list(lcu);
81        self
82    }
83
84    /// Gets a reference to the [PhysicalCashUnitList].
85    pub const fn physical_cash_unit_list(&self) -> &PhysicalCashUnitList {
86        &self.physical_cash_unit_list
87    }
88
89    /// Sets the [PhysicalCashUnitList].
90    pub fn set_physical_cash_unit_list(&mut self, pcu: PhysicalCashUnitList) {
91        self.physical_cash_unit_list = pcu;
92    }
93
94    /// Builder function that sets the [PhysicalCashUnitList].
95    pub fn with_physical_cash_unit_list(mut self, pcu: PhysicalCashUnitList) -> Self {
96        self.set_physical_cash_unit_list(pcu);
97        self
98    }
99
100    /// Deconstructs the [CashUnit] into a [LogicalCashUnitList] and [PhysicalCashUnitList].
101    pub fn into_lists(self) -> (LogicalCashUnitList, PhysicalCashUnitList) {
102        (self.logical_cash_unit_list, self.physical_cash_unit_list)
103    }
104}
105
106impl fmt::Display for CashUnit {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        write!(f, "{{")?;
109        write!(f, r#""transport_count":{},"#, self.transport_count)?;
110        write!(
111            f,
112            r#""logical_cash_unit_list":{},"#,
113            self.logical_cash_unit_list
114        )?;
115        write!(
116            f,
117            r#""physical_cash_unit_list":{}"#,
118            self.physical_cash_unit_list
119        )?;
120        write!(f, "}}")
121    }
122}
123
124impl_xfs_struct!(
125    CashUnit,
126    "cashUnit",
127    [
128        transport_count: TransportCount,
129        logical_cash_unit_list: LogicalCashUnitList,
130        physical_cash_unit_list: PhysicalCashUnitList
131    ]
132);
133
134impl TryFrom<&XfsMethodResponse> for CashUnit {
135    type Error = Error;
136
137    fn try_from(val: &XfsMethodResponse) -> Result<Self> {
138        val.as_params()?
139            .params()
140            .iter()
141            .map(|m| m.inner())
142            .find(|m| m.value().xfs_struct().is_some())
143            .ok_or(Error::Xfs(format!(
144                "Expected CashUnit XfsMethodResponse, have: {val}"
145            )))?
146            .value()
147            .try_into()
148    }
149}
150
151impl TryFrom<XfsMethodResponse> for CashUnit {
152    type Error = Error;
153
154    fn try_from(val: XfsMethodResponse) -> Result<Self> {
155        (&val).try_into()
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162    use crate::xfs;
163
164    #[test]
165    fn test_parse_cash_unit() -> Result<()> {
166        let unit_xml = include_bytes!("../tests/xml/query-cash-unit.xml");
167        let unit_xml_str = std::str::from_utf8(unit_xml.as_ref())?;
168
169        let res = xfs::from_str::<xfs::method_response::XfsMethodResponseStruct>(unit_xml_str)?;
170        let _cash_unit = CashUnit::try_from(res.inner())?;
171
172        Ok(())
173    }
174}