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 TariffPrice {
#[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>,
}
impl Bo4eObject for TariffPrice {
fn type_name_german() -> &'static str {
"Tarifpreis"
}
fn type_name_english() -> &'static str {
"TariffPrice"
}
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() {
let price = TariffPrice {
price_type: Some(PriceType::WorkingPriceSingleTariff),
value: Some(30.5),
currency: Some(Currency::Eur),
reference_unit: Some(Unit::KilowattHour),
..Default::default()
};
assert_eq!(price.price_type, Some(PriceType::WorkingPriceSingleTariff));
assert_eq!(price.value, Some(30.5));
}
#[test]
fn test_base_price() {
let price = TariffPrice {
price_type: Some(PriceType::BasePrice),
value: Some(12.50),
currency: Some(Currency::Eur),
reference_unit: Some(Unit::Month),
description: Some("Monthly base fee".to_string()),
..Default::default()
};
assert_eq!(price.price_type, Some(PriceType::BasePrice));
}
#[test]
fn test_default() {
let price = TariffPrice::default();
assert!(price.price_type.is_none());
assert!(price.value.is_none());
}
#[test]
fn test_roundtrip() {
let price = TariffPrice {
price_type: Some(PriceType::WorkingPriceSingleTariff),
value: Some(25.75),
currency: Some(Currency::Eur),
reference_unit: Some(Unit::KilowattHour),
description: Some("Peak rate".to_string()),
..Default::default()
};
let json = serde_json::to_string(&price).unwrap();
let parsed: TariffPrice = serde_json::from_str(&json).unwrap();
assert_eq!(price, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(TariffPrice::type_name_german(), "Tarifpreis");
assert_eq!(TariffPrice::type_name_english(), "TariffPrice");
}
}