use serde::{Deserialize, Serialize};
use crate::enums::CostClass;
use crate::traits::{Bo4eMeta, Bo4eObject};
use super::{Amount, CostPosition};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "json-schema", schemars(rename = "Kostenblock"))]
#[serde(rename_all = "camelCase")]
pub struct CostBlock {
#[serde(flatten)]
pub meta: Bo4eMeta,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "kostenblockbezeichnung"))]
pub designation: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "kostenklasse"))]
pub cost_class: Option<CostClass>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "json-schema", schemars(rename = "summeKostenblock"))]
pub total_amount: Option<Amount>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[cfg_attr(feature = "json-schema", schemars(rename = "kostenpositionen"))]
pub positions: Vec<CostPosition>,
}
impl Bo4eObject for CostBlock {
fn type_name_german() -> &'static str {
"Kostenblock"
}
fn type_name_english() -> &'static str {
"CostBlock"
}
fn meta(&self) -> &Bo4eMeta {
&self.meta
}
fn meta_mut(&mut self) -> &mut Bo4eMeta {
&mut self.meta
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::com::Price;
#[test]
fn test_cost_block() {
let cost_block = CostBlock {
designation: Some("Netzkosten".to_string()),
cost_class: Some(CostClass::ExternalCosts),
total_amount: Some(Amount::eur(500.0)),
positions: vec![
CostPosition {
title: Some("Arbeitspreis Netz".to_string()),
amount: Some(Amount::eur(300.0)),
..Default::default()
},
CostPosition {
title: Some("Leistungspreis Netz".to_string()),
amount: Some(Amount::eur(200.0)),
..Default::default()
},
],
..Default::default()
};
assert_eq!(cost_block.designation, Some("Netzkosten".to_string()));
assert_eq!(cost_block.positions.len(), 2);
}
#[test]
fn test_default() {
let cost_block = CostBlock::default();
assert!(cost_block.designation.is_none());
assert!(cost_block.total_amount.is_none());
assert!(cost_block.positions.is_empty());
}
#[test]
fn test_serialize() {
let cost_block = CostBlock {
designation: Some("Messkosten".to_string()),
total_amount: Some(Amount::eur(100.0)),
..Default::default()
};
let json = serde_json::to_string(&cost_block).unwrap();
assert!(json.contains(r#""designation":"Messkosten""#));
}
#[test]
fn test_roundtrip() {
let cost_block = CostBlock {
designation: Some("Energiekosten".to_string()),
cost_class: Some(CostClass::Procurement),
total_amount: Some(Amount::eur(1234.56)),
positions: vec![CostPosition {
title: Some("Arbeitspreis Energie".to_string()),
unit_price: Some(Price::eur_per_kwh(0.25)),
..Default::default()
}],
..Default::default()
};
let json = serde_json::to_string(&cost_block).unwrap();
let parsed: CostBlock = serde_json::from_str(&json).unwrap();
assert_eq!(cost_block, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(CostBlock::type_name_german(), "Kostenblock");
assert_eq!(CostBlock::type_name_english(), "CostBlock");
}
}