bnr_xfs/denominations/billset/
list.rs

1use std::{cmp, fmt};
2
3use crate::impl_xfs_array;
4use crate::xfs::method_response::XfsMethodResponse;
5use crate::{Error, Result};
6
7use super::BillsetInfo;
8
9pub const BILLSET_ID_LIST_LEN: usize = 61;
10
11const BILLSET_INFO_DEFAULT: BillsetInfo = BillsetInfo::new();
12
13/// Represents a list of [BillsetInfo].
14#[derive(Clone, Debug, Eq, PartialEq)]
15pub struct BillsetIdList {
16    size: usize,
17    items: [BillsetInfo; BILLSET_ID_LIST_LEN],
18}
19
20impl BillsetIdList {
21    /// Creates a new [BillsetIdList].
22    pub const fn new() -> Self {
23        Self {
24            size: 0,
25            items: [BILLSET_INFO_DEFAULT; BILLSET_ID_LIST_LEN],
26        }
27    }
28
29    /// Gets the maximum number of [BillsetInfo] items.
30    pub const fn max_size() -> usize {
31        BILLSET_ID_LIST_LEN
32    }
33
34    /// Gets the size of the [BillsetIdList].
35    pub const fn size(&self) -> usize {
36        self.size
37    }
38
39    /// Sets the size of the [BillsetIdList].
40    ///
41    /// No-op if `val` is larger than [BILLSET_ID_LIST_LEN].
42    pub fn set_size(&mut self, val: u32) {
43        let size = val as usize;
44        if size <= BILLSET_ID_LIST_LEN {
45            self.size = size;
46        }
47    }
48
49    /// Builder function that sets the size of the [BillsetIdList].
50    ///
51    /// No-op if `val` is larger than [BILLSET_ID_LIST_LEN].
52    pub fn with_size(mut self, val: u32) -> Self {
53        self.set_size(val);
54        self
55    }
56
57    /// Gets a reference to the list of set [BillsetInfo] items.
58    pub fn items(&self) -> &[BillsetInfo] {
59        let len = cmp::min(self.size, BILLSET_ID_LIST_LEN);
60        self.items[..len].as_ref()
61    }
62
63    /// Sets the list of [BillsetInfo] items.
64    pub fn set_items(&mut self, val: &[BillsetInfo]) {
65        let len = cmp::min(val.len(), BILLSET_ID_LIST_LEN);
66        self.items[..len]
67            .iter_mut()
68            .zip(val[..len].iter())
69            .for_each(|(dst, src)| *dst = src.clone());
70        self.items[len..]
71            .iter_mut()
72            .for_each(|item| *item = BillsetInfo::new());
73        self.size = len;
74    }
75
76    /// Builder function that sets the list of [BillsetInfo] items.
77    pub fn with_items(mut self, val: &[BillsetInfo]) -> Self {
78        self.set_items(val);
79        self
80    }
81
82    /// Pushes a [BillsetInfo] onto the end of the list.
83    ///
84    /// No-op if the list is at capacity.
85    pub fn push_item(&mut self, val: BillsetInfo) {
86        if self.size < BILLSET_ID_LIST_LEN {
87            self.items[self.size] = val;
88            self.size += 1;
89        }
90    }
91}
92
93impl Default for BillsetIdList {
94    fn default() -> Self {
95        Self::new()
96    }
97}
98
99impl fmt::Display for BillsetIdList {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        write!(f, r#"{{"items":["#)?;
102        for (i, item) in self.items().iter().enumerate() {
103            if i != 0 {
104                write!(f, ",")?;
105            }
106            write!(f, "{item}")?;
107        }
108        write!(f, "]}}")
109    }
110}
111
112impl_xfs_array!(BillsetIdList, "billsetIdList");
113
114impl TryFrom<&XfsMethodResponse> for BillsetIdList {
115    type Error = Error;
116
117    fn try_from(val: &XfsMethodResponse) -> Result<Self> {
118        val.as_params()?
119            .params()
120            .iter()
121            .map(|m| m.inner())
122            .find(|m| m.value().array().is_some())
123            .ok_or(Error::Xfs(format!(
124                "Expected BillsetIdList XfsMethodResponse, have: {val}"
125            )))?
126            .value()
127            .try_into()
128    }
129}
130
131impl TryFrom<XfsMethodResponse> for BillsetIdList {
132    type Error = Error;
133
134    fn try_from(val: XfsMethodResponse) -> Result<Self> {
135        (&val).try_into()
136    }
137}