Skip to main content

bo4e_core/enums/
tariff_time.rs

1//! Tariff time (Tarifzeit) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Tariff time period.
6///
7/// Used to identify different tariff times, for example for pricing or consumption measurement.
8///
9/// German: Tarifzeit
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
12#[cfg_attr(feature = "json-schema", schemars(rename = "Tarifzeit"))]
13#[non_exhaustive]
14pub enum TariffTime {
15    /// Standard tariff time for single-tariff configurations
16    #[serde(rename = "TZ_STANDARD")]
17    Standard,
18
19    /// High tariff time for multi-tariff configurations (HT - Hochtarif)
20    #[serde(rename = "TZ_HT")]
21    HighTariff,
22
23    /// Low tariff time for multi-tariff configurations (NT - Niedrigtarif)
24    #[serde(rename = "TZ_NT")]
25    LowTariff,
26}
27
28impl TariffTime {
29    /// Returns the German name.
30    pub fn german_name(&self) -> &'static str {
31        match self {
32            Self::Standard => "Tarifzeit Standard",
33            Self::HighTariff => "Tarifzeit HT (Hochtarif)",
34            Self::LowTariff => "Tarifzeit NT (Niedrigtarif)",
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_serialize() {
45        assert_eq!(
46            serde_json::to_string(&TariffTime::Standard).unwrap(),
47            r#""TZ_STANDARD""#
48        );
49        assert_eq!(
50            serde_json::to_string(&TariffTime::HighTariff).unwrap(),
51            r#""TZ_HT""#
52        );
53        assert_eq!(
54            serde_json::to_string(&TariffTime::LowTariff).unwrap(),
55            r#""TZ_NT""#
56        );
57    }
58
59    #[test]
60    fn test_roundtrip() {
61        for time in [
62            TariffTime::Standard,
63            TariffTime::HighTariff,
64            TariffTime::LowTariff,
65        ] {
66            let json = serde_json::to_string(&time).unwrap();
67            let parsed: TariffTime = serde_json::from_str(&json).unwrap();
68            assert_eq!(time, parsed);
69        }
70    }
71}