Skip to main content

bo4e_core/enums/
cost_class.rs

1//! Cost class (Kostenklasse) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Cost class categorization.
6///
7/// Cost classes form the top level of different costs.
8/// Typically, total costs of a cost class are calculated in an application.
9///
10/// German: Kostenklasse
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
13#[cfg_attr(feature = "json-schema", schemars(rename = "Kostenklasse"))]
14#[non_exhaustive]
15pub enum CostClass {
16    /// External costs (Fremdkosten)
17    #[serde(rename = "FREMDKOSTEN")]
18    ExternalCosts,
19
20    /// Procurement costs (Beschaffung)
21    #[serde(rename = "BESCHAFFUNG")]
22    Procurement,
23
24    /// Internal costs (Selbstkosten)
25    #[serde(rename = "SELBSTKOSTEN")]
26    InternalCosts,
27
28    /// Margins (Margen)
29    #[serde(rename = "MARGEN")]
30    Margins,
31
32    /// Energy supply costs (Energieversorgungskosten)
33    #[serde(rename = "ENERGIEVERSORGUNGSKOSTEN")]
34    EnergySupplyCosts,
35}
36
37impl CostClass {
38    /// Returns the German name.
39    pub fn german_name(&self) -> &'static str {
40        match self {
41            Self::ExternalCosts => "Fremdkosten",
42            Self::Procurement => "Beschaffung",
43            Self::InternalCosts => "Selbstkosten",
44            Self::Margins => "Margen",
45            Self::EnergySupplyCosts => "Energieversorgungskosten",
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_serialize() {
56        assert_eq!(
57            serde_json::to_string(&CostClass::ExternalCosts).unwrap(),
58            r#""FREMDKOSTEN""#
59        );
60        assert_eq!(
61            serde_json::to_string(&CostClass::Procurement).unwrap(),
62            r#""BESCHAFFUNG""#
63        );
64    }
65
66    #[test]
67    fn test_roundtrip() {
68        for cost_class in [
69            CostClass::ExternalCosts,
70            CostClass::Procurement,
71            CostClass::InternalCosts,
72            CostClass::Margins,
73            CostClass::EnergySupplyCosts,
74        ] {
75            let json = serde_json::to_string(&cost_class).unwrap();
76            let parsed: CostClass = serde_json::from_str(&json).unwrap();
77            assert_eq!(cost_class, parsed);
78        }
79    }
80}