Skip to main content

bo4e_core/enums/
calculation_formula.rs

1//! Calculation formula (Berechnungsformel) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Calculation formula type.
6///
7/// Defines standard calculation formulas used in energy billing.
8///
9/// German: Berechnungsformel
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 = "Berechnungsformel"))]
13#[non_exhaustive]
14pub enum CalculationFormula {
15    /// Highest of maximum values (Höchstwert der Maximalwerte)
16    #[serde(rename = "HOECHSTWERT")]
17    HighestValue,
18
19    /// Minimum value (Minimalwert)
20    #[serde(rename = "MINIMALWERT")]
21    MinimumValue,
22
23    /// Average value (Mittelwert)
24    #[serde(rename = "MITTELWERT")]
25    AverageValue,
26
27    /// Sum (Summenwert)
28    #[serde(rename = "SUMMENWERT")]
29    SumValue,
30}
31
32impl CalculationFormula {
33    /// Returns the German name.
34    pub fn german_name(&self) -> &'static str {
35        match self {
36            Self::HighestValue => "Höchstwert",
37            Self::MinimumValue => "Minimalwert",
38            Self::AverageValue => "Mittelwert",
39            Self::SumValue => "Summenwert",
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_serialize() {
50        assert_eq!(
51            serde_json::to_string(&CalculationFormula::HighestValue).unwrap(),
52            r#""HOECHSTWERT""#
53        );
54    }
55
56    #[test]
57    fn test_roundtrip() {
58        for formula in [
59            CalculationFormula::HighestValue,
60            CalculationFormula::MinimumValue,
61            CalculationFormula::AverageValue,
62            CalculationFormula::SumValue,
63        ] {
64            let json = serde_json::to_string(&formula).unwrap();
65            let parsed: CalculationFormula = serde_json::from_str(&json).unwrap();
66            assert_eq!(formula, parsed);
67        }
68    }
69}