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