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