bnr_xfs/cash_unit/
threshold.rs

1use std::fmt;
2
3use crate::xfs::{
4    value::XfsValue,
5    xfs_struct::{XfsMember, XfsStruct},
6};
7use crate::{impl_xfs_i4, Error, Result};
8
9mod mode;
10mod status;
11
12pub use mode::*;
13pub use status::*;
14
15/// The PCU [ThresholdStatus] becomes Full when the bill count is greater or equal to this value.
16#[repr(C)]
17#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
18pub struct ThresholdFull(u32);
19
20impl ThresholdFull {
21    /// Creates a new [ThresholdFull].
22    pub const fn new() -> Self {
23        Self(0)
24    }
25
26    /// Creates a new [ThresholdFull] from the provided parameter.
27    pub const fn create(val: u32) -> Self {
28        Self(val)
29    }
30
31    /// Gets the inner representation of [ThresholdFull].
32    pub const fn inner(&self) -> u32 {
33        self.0
34    }
35
36    /// Gets the inner representation of [ThresholdFull].
37    pub fn set_inner(&mut self, val: u32) {
38        self.0 = val;
39    }
40}
41
42impl fmt::Display for ThresholdFull {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "{}", self.inner())
45    }
46}
47
48impl_xfs_i4!(ThresholdFull, "full");
49
50/// The PCU [ThresholdStatus] becomes High when the bill count is greater than this value.
51#[repr(C)]
52#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
53pub struct ThresholdHigh(u32);
54
55impl ThresholdHigh {
56    /// Creates a new [ThresholdHigh].
57    pub const fn new() -> Self {
58        Self(0)
59    }
60
61    /// Creates a new [ThresholdHigh] from the provided parameter.
62    pub const fn create(val: u32) -> Self {
63        Self(val)
64    }
65
66    /// Gets the inner representation of [ThresholdHigh].
67    pub const fn inner(&self) -> u32 {
68        self.0
69    }
70
71    /// Gets the inner representation of [ThresholdHigh].
72    pub fn set_inner(&mut self, val: u32) {
73        self.0 = val;
74    }
75}
76
77impl fmt::Display for ThresholdHigh {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        write!(f, "{}", self.inner())
80    }
81}
82
83impl_xfs_i4!(ThresholdHigh, "high");
84
85/// The PCU [ThresholdStatus] becomes Low when the bill count is lower to this value.
86#[repr(C)]
87#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
88pub struct ThresholdLow(u32);
89
90impl ThresholdLow {
91    /// Creates a new [ThresholdLow].
92    pub const fn new() -> Self {
93        Self(0)
94    }
95
96    /// Creates a new [ThresholdLow] from the provided parameter.
97    pub const fn create(val: u32) -> Self {
98        Self(val)
99    }
100
101    /// Gets the inner representation of [ThresholdLow].
102    pub const fn inner(&self) -> u32 {
103        self.0
104    }
105
106    /// Gets the inner representation of [ThresholdLow].
107    pub fn set_inner(&mut self, val: u32) {
108        self.0 = val;
109    }
110}
111
112impl fmt::Display for ThresholdLow {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        write!(f, "{}", self.inner())
115    }
116}
117
118impl_xfs_i4!(ThresholdLow, "low");
119
120/// The PCU [ThresholdStatus] becomes Empty when the bill count is lower or equal to this value.
121#[repr(C)]
122#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
123pub struct ThresholdEmpty(u32);
124
125impl ThresholdEmpty {
126    /// Creates a new [ThresholdEmpty].
127    pub const fn new() -> Self {
128        Self(0)
129    }
130
131    /// Creates a new [ThresholdEmpty] from the provided parameter.
132    pub const fn create(val: u32) -> Self {
133        Self(val)
134    }
135
136    /// Gets the inner representation of [ThresholdEmpty].
137    pub const fn inner(&self) -> u32 {
138        self.0
139    }
140
141    /// Gets the inner representation of [ThresholdEmpty].
142    pub fn set_inner(&mut self, val: u32) {
143        self.0 = val;
144    }
145}
146
147impl fmt::Display for ThresholdEmpty {
148    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149        write!(f, "{}", self.inner())
150    }
151}
152
153impl_xfs_i4!(ThresholdEmpty, "empty");
154
155/// Structure that defines the levels determining a physical cash unit [ThresholdStatus].
156#[repr(C)]
157#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
158pub struct Threshold {
159    full: ThresholdFull,
160    high: ThresholdHigh,
161    low: ThresholdLow,
162    empty: ThresholdEmpty,
163}
164
165impl Threshold {
166    /// Creates a new [Threshold].
167    pub const fn new() -> Self {
168        Self {
169            full: ThresholdFull::new(),
170            high: ThresholdHigh::new(),
171            low: ThresholdLow::new(),
172            empty: ThresholdEmpty::new(),
173        }
174    }
175
176    /// Gets the [XfsMember] name.
177    pub const fn xfs_name() -> &'static str {
178        "threshold"
179    }
180
181    /// Gets the full threshold limit.
182    pub const fn full(&self) -> u32 {
183        self.full.inner()
184    }
185
186    /// Sets the full threshold limit.
187    pub fn set_full(&mut self, val: u32) {
188        self.full.set_inner(val);
189    }
190
191    /// Builder function that sets the full threshold limit.
192    pub fn with_full(mut self, val: u32) -> Self {
193        self.set_full(val);
194        self
195    }
196
197    /// Gets the high threshold limit.
198    pub const fn high(&self) -> u32 {
199        self.high.inner()
200    }
201
202    /// Sets the high threshold limit.
203    pub fn set_high(&mut self, val: u32) {
204        self.high.set_inner(val);
205    }
206
207    /// Builder function that sets the high threshold limit.
208    pub fn with_high(mut self, val: u32) -> Self {
209        self.set_high(val);
210        self
211    }
212
213    /// Gets the low threshold limit.
214    pub const fn low(&self) -> u32 {
215        self.low.inner()
216    }
217
218    /// Sets the low threshold limit.
219    pub fn set_low(&mut self, val: u32) {
220        self.low.set_inner(val);
221    }
222
223    /// Builder function that sets the low threshold limit.
224    pub fn with_low(mut self, val: u32) -> Self {
225        self.set_low(val);
226        self
227    }
228
229    /// Gets the empty threshold limit.
230    pub const fn empty(&self) -> u32 {
231        self.empty.inner()
232    }
233
234    /// Sets the empty threshold limit.
235    pub fn set_empty(&mut self, val: u32) {
236        self.empty.set_inner(val);
237    }
238
239    /// Builder function that sets the empty threshold limit.
240    pub fn with_empty(mut self, val: u32) -> Self {
241        self.set_empty(val);
242        self
243    }
244}
245
246impl From<&Threshold> for XfsStruct {
247    fn from(val: &Threshold) -> Self {
248        Self::create([
249            val.full.into(),
250            val.high.into(),
251            val.low.into(),
252            val.empty.into(),
253        ])
254    }
255}
256
257impl From<Threshold> for XfsStruct {
258    fn from(val: Threshold) -> Self {
259        (&val).into()
260    }
261}
262
263impl From<&Threshold> for XfsValue {
264    fn from(val: &Threshold) -> Self {
265        Self::new().with_xfs_struct(val.into())
266    }
267}
268
269impl From<Threshold> for XfsValue {
270    fn from(val: Threshold) -> Self {
271        (&val).into()
272    }
273}
274
275impl From<&Threshold> for XfsMember {
276    fn from(val: &Threshold) -> Self {
277        Self::create(Threshold::xfs_name(), val.into())
278    }
279}
280
281impl From<Threshold> for XfsMember {
282    fn from(val: Threshold) -> Self {
283        (&val).into()
284    }
285}
286
287impl TryFrom<&XfsValue> for Threshold {
288    type Error = Error;
289
290    fn try_from(val: &XfsValue) -> Result<Self> {
291        val.xfs_struct()
292            .ok_or(Error::Xfs(format!(
293                "Expected Threshold XfsValue, have: {val}"
294            )))?
295            .try_into()
296    }
297}
298
299impl TryFrom<XfsValue> for Threshold {
300    type Error = Error;
301
302    fn try_from(val: XfsValue) -> Result<Self> {
303        (&val).try_into()
304    }
305}
306
307impl TryFrom<&XfsMember> for Threshold {
308    type Error = Error;
309
310    fn try_from(val: &XfsMember) -> Result<Self> {
311        match (val.name(), val.value().xfs_struct()) {
312            (n, Some(s)) if n == Self::xfs_name() => s.try_into(),
313            _ => Err(Error::Xfs(format!(
314                "Expected Threshold XfsMember, have: {val}"
315            ))),
316        }
317    }
318}
319
320impl TryFrom<XfsMember> for Threshold {
321    type Error = Error;
322
323    fn try_from(val: XfsMember) -> Result<Self> {
324        (&val).try_into()
325    }
326}
327
328impl TryFrom<&XfsStruct> for Threshold {
329    type Error = Error;
330
331    fn try_from(val: &XfsStruct) -> Result<Self> {
332        let full: ThresholdFull = val.find_member(ThresholdFull::xfs_name())?.try_into()?;
333        let high: ThresholdHigh = val.find_member(ThresholdHigh::xfs_name())?.try_into()?;
334        let low: ThresholdLow = val.find_member(ThresholdLow::xfs_name())?.try_into()?;
335        let empty: ThresholdEmpty = val.find_member(ThresholdEmpty::xfs_name())?.try_into()?;
336
337        Ok(Self {
338            full,
339            high,
340            low,
341            empty,
342        })
343    }
344}
345
346impl TryFrom<XfsStruct> for Threshold {
347    type Error = Error;
348
349    fn try_from(val: XfsStruct) -> Result<Self> {
350        (&val).try_into()
351    }
352}
353
354impl fmt::Display for Threshold {
355    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356        write!(f, "{{")?;
357        write!(f, r#""full":{},"#, self.full)?;
358        write!(f, r#""high":{},"#, self.high)?;
359        write!(f, r#""low":{},"#, self.low)?;
360        write!(f, r#""empty":{}"#, self.empty)?;
361        write!(f, "}}")
362    }
363}