use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum EnergyDirection {
#[serde(rename = "AUSSP")]
FeedOut,
#[serde(rename = "EINSP")]
FeedIn,
}
impl EnergyDirection {
pub fn german_name(&self) -> &'static str {
match self {
Self::FeedOut => "Ausspeisung",
Self::FeedIn => "Einspeisung",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize() {
assert_eq!(
serde_json::to_string(&EnergyDirection::FeedOut).unwrap(),
r#""AUSSP""#
);
assert_eq!(
serde_json::to_string(&EnergyDirection::FeedIn).unwrap(),
r#""EINSP""#
);
}
#[test]
fn test_deserialize() {
assert_eq!(
serde_json::from_str::<EnergyDirection>(r#""AUSSP""#).unwrap(),
EnergyDirection::FeedOut
);
assert_eq!(
serde_json::from_str::<EnergyDirection>(r#""EINSP""#).unwrap(),
EnergyDirection::FeedIn
);
}
#[test]
fn test_roundtrip() {
for dir in [EnergyDirection::FeedOut, EnergyDirection::FeedIn] {
let json = serde_json::to_string(&dir).unwrap();
let parsed: EnergyDirection = serde_json::from_str(&json).unwrap();
assert_eq!(dir, parsed);
}
}
}