bnr_xfs/cash_unit/threshold/
mode.rs

1use std::fmt;
2
3use crate::impl_xfs_enum;
4
5const THRESHOLD_SENSOR: u32 = 0;
6const THRESHOLD_COUNT: u32 = 1;
7
8/// Threshold mode used to determine the [ThresholdStatus] of a PCU.
9#[repr(u32)]
10#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
11pub enum ThresholdMode {
12    /// [ThresholdStatus] changes are determined by the sensors of the BNR (physical filling status).
13    #[default]
14    Sensor = THRESHOLD_SENSOR,
15    /// [ThresholdStatus] changes are determined by comparing the PCU counts to the Threshold levels.
16    Count = THRESHOLD_COUNT,
17}
18
19impl ThresholdMode {
20    /// Creates a new [ThresholdMode].
21    pub const fn new() -> Self {
22        Self::Sensor
23    }
24
25    /// Creates a new [ThresholdMode] from the provided parameter.
26    pub const fn create(val: u32) -> Self {
27        match val {
28            THRESHOLD_SENSOR => Self::Sensor,
29            THRESHOLD_COUNT => Self::Count,
30            _ => Self::Sensor,
31        }
32    }
33}
34
35impl From<&ThresholdMode> for &'static str {
36    fn from(val: &ThresholdMode) -> Self {
37        match val {
38            ThresholdMode::Sensor => "sensor",
39            ThresholdMode::Count => "count",
40        }
41    }
42}
43
44impl From<ThresholdMode> for &'static str {
45    fn from(val: ThresholdMode) -> Self {
46        (&val).into()
47    }
48}
49
50impl fmt::Display for ThresholdMode {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, r#""{}""#, <&str>::from(self))
53    }
54}
55
56impl_xfs_enum!(ThresholdMode, "thresholdMode");