Skip to main content

bo4e_core/enums/
division.rs

1//! Energy division (Sparte) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Energy division/sector.
6///
7/// Indicates which energy sector a business object belongs to.
8///
9/// German: Sparte
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 = "Sparte"))]
13#[non_exhaustive]
14pub enum Division {
15    /// Electricity (Strom)
16    #[serde(rename = "STROM")]
17    Electricity,
18
19    /// Natural gas (Gas)
20    #[serde(rename = "GAS")]
21    Gas,
22
23    /// District heating (Fernwaerme)
24    #[serde(rename = "FERNWAERME")]
25    DistrictHeating,
26
27    /// Local/near heating (Nahwaerme)
28    #[serde(rename = "NAHWAERME")]
29    LocalHeating,
30
31    /// Water (Wasser)
32    #[serde(rename = "WASSER")]
33    Water,
34
35    /// Wastewater (Abwasser)
36    #[serde(rename = "ABWASSER")]
37    Wastewater,
38
39    /// Cross-divisional electricity and gas (Strom und Gas)
40    #[serde(rename = "STROM_UND_GAS")]
41    ElectricityAndGas,
42}
43
44impl Division {
45    /// Returns the German name.
46    pub fn german_name(&self) -> &'static str {
47        match self {
48            Self::Electricity => "Strom",
49            Self::Gas => "Gas",
50            Self::DistrictHeating => "Fernwaerme",
51            Self::LocalHeating => "Nahwaerme",
52            Self::Water => "Wasser",
53            Self::Wastewater => "Abwasser",
54            Self::ElectricityAndGas => "Strom und Gas",
55        }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_serialize() {
65        assert_eq!(
66            serde_json::to_string(&Division::Electricity).unwrap(),
67            r#""STROM""#
68        );
69        assert_eq!(serde_json::to_string(&Division::Gas).unwrap(), r#""GAS""#);
70    }
71
72    #[test]
73    fn test_deserialize() {
74        assert_eq!(
75            serde_json::from_str::<Division>(r#""STROM""#).unwrap(),
76            Division::Electricity
77        );
78        assert_eq!(
79            serde_json::from_str::<Division>(r#""FERNWAERME""#).unwrap(),
80            Division::DistrictHeating
81        );
82    }
83
84    #[test]
85    fn test_roundtrip() {
86        for division in [
87            Division::Electricity,
88            Division::Gas,
89            Division::DistrictHeating,
90            Division::LocalHeating,
91            Division::Water,
92            Division::Wastewater,
93            Division::ElectricityAndGas,
94        ] {
95            let json = serde_json::to_string(&division).unwrap();
96            let parsed: Division = serde_json::from_str(&json).unwrap();
97            assert_eq!(division, parsed);
98        }
99    }
100}