Skip to main content

bo4e_core/enums/
energy_direction.rs

1//! Energy direction (Energierichtung) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Direction of energy flow.
6///
7/// Specifies the energy direction of a market and/or metering location.
8///
9/// German: Energierichtung
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 = "Energierichtung"))]
13#[non_exhaustive]
14pub enum EnergyDirection {
15    /// Energy feed-out/withdrawal (Ausspeisung)
16    #[serde(rename = "AUSSP")]
17    FeedOut,
18
19    /// Energy feed-in/injection (Einspeisung)
20    #[serde(rename = "EINSP")]
21    FeedIn,
22}
23
24impl EnergyDirection {
25    /// Returns the German name.
26    pub fn german_name(&self) -> &'static str {
27        match self {
28            Self::FeedOut => "Ausspeisung",
29            Self::FeedIn => "Einspeisung",
30        }
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_serialize() {
40        assert_eq!(
41            serde_json::to_string(&EnergyDirection::FeedOut).unwrap(),
42            r#""AUSSP""#
43        );
44        assert_eq!(
45            serde_json::to_string(&EnergyDirection::FeedIn).unwrap(),
46            r#""EINSP""#
47        );
48    }
49
50    #[test]
51    fn test_deserialize() {
52        assert_eq!(
53            serde_json::from_str::<EnergyDirection>(r#""AUSSP""#).unwrap(),
54            EnergyDirection::FeedOut
55        );
56        assert_eq!(
57            serde_json::from_str::<EnergyDirection>(r#""EINSP""#).unwrap(),
58            EnergyDirection::FeedIn
59        );
60    }
61
62    #[test]
63    fn test_roundtrip() {
64        for dir in [EnergyDirection::FeedOut, EnergyDirection::FeedIn] {
65            let json = serde_json::to_string(&dir).unwrap();
66            let parsed: EnergyDirection = serde_json::from_str(&json).unwrap();
67            assert_eq!(dir, parsed);
68        }
69    }
70}