use serde::{Deserialize, Serialize};
use crate::enums::{Currency, ServiceType, Unit};
use crate::traits::{Bo4eMeta, Bo4eObject};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "json-schema", schemars(rename = "Dienstleistungspreis"))]
#[serde(rename_all = "camelCase")]
pub struct ServicePrice {
#[serde(flatten)]
pub meta: Bo4eMeta,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "dienstleistungstyp"))]
pub service_type: Option<ServiceType>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "wert"))]
pub value: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "waehrung"))]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "bezugseinheit"))]
pub reference_unit: Option<Unit>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "beschreibung"))]
pub description: Option<String>,
}
impl Bo4eObject for ServicePrice {
fn type_name_german() -> &'static str {
"Dienstleistungspreis"
}
fn type_name_english() -> &'static str {
"ServicePrice"
}
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_installation_service() {
let price = ServicePrice {
service_type: Some(ServiceType::Disconnection),
value: Some(75.0),
currency: Some(Currency::Eur),
description: Some("Zählerinstallation".to_string()),
..Default::default()
};
assert_eq!(price.service_type, Some(ServiceType::Disconnection));
assert_eq!(price.value, Some(75.0));
}
#[test]
fn test_default() {
let price = ServicePrice::default();
assert!(price.service_type.is_none());
assert!(price.value.is_none());
}
#[test]
fn test_roundtrip() {
let price = ServicePrice {
service_type: Some(ServiceType::ManualReadingMonthly),
value: Some(50.0),
currency: Some(Currency::Eur),
reference_unit: Some(Unit::Hour),
description: Some("Energieberatung".to_string()),
..Default::default()
};
let json = serde_json::to_string(&price).unwrap();
let parsed: ServicePrice = serde_json::from_str(&json).unwrap();
assert_eq!(price, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(ServicePrice::type_name_german(), "Dienstleistungspreis");
assert_eq!(ServicePrice::type_name_english(), "ServicePrice");
}
}