use serde::{Deserialize, Serialize};
use crate::enums::{Currency, PriceType, Unit};
use crate::traits::{Bo4eMeta, Bo4eObject};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NetworkCharge {
#[serde(flatten)]
pub meta: Bo4eMeta,
#[serde(skip_serializing_if = "Option::is_none")]
pub price_type: Option<PriceType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reference_unit: Option<Unit>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub network_operator_code: Option<String>,
}
impl Bo4eObject for NetworkCharge {
fn type_name_german() -> &'static str {
"Netzentgelt"
}
fn type_name_english() -> &'static str {
"NetworkCharge"
}
fn meta(&self) -> &Bo4eMeta {
&self.meta
}
fn meta_mut(&mut self) -> &mut Bo4eMeta {
&mut self.meta
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_work_price_charge() {
let charge = NetworkCharge {
price_type: Some(PriceType::WorkingPriceSingleTariff),
value: Some(5.82),
currency: Some(Currency::Eur),
reference_unit: Some(Unit::KilowattHour),
..Default::default()
};
assert_eq!(charge.price_type, Some(PriceType::WorkingPriceSingleTariff));
assert_eq!(charge.value, Some(5.82));
}
#[test]
fn test_base_price_charge() {
let charge = NetworkCharge {
price_type: Some(PriceType::BasePrice),
value: Some(55.0),
currency: Some(Currency::Eur),
reference_unit: Some(Unit::Year),
network_operator_code: Some("9900001".to_string()),
..Default::default()
};
assert_eq!(charge.price_type, Some(PriceType::BasePrice));
}
#[test]
fn test_default() {
let charge = NetworkCharge::default();
assert!(charge.price_type.is_none());
assert!(charge.value.is_none());
}
#[test]
fn test_roundtrip() {
let charge = NetworkCharge {
price_type: Some(PriceType::WorkingPriceSingleTariff),
value: Some(6.25),
currency: Some(Currency::Eur),
reference_unit: Some(Unit::KilowattHour),
description: Some("Arbeitspreis Netznutzung".to_string()),
..Default::default()
};
let json = serde_json::to_string(&charge).unwrap();
let parsed: NetworkCharge = serde_json::from_str(&json).unwrap();
assert_eq!(charge, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(NetworkCharge::type_name_german(), "Netzentgelt");
assert_eq!(NetworkCharge::type_name_english(), "NetworkCharge");
}
}