use serde::{Deserialize, Serialize};
use crate::com::{Amount, CostBlock, TimePeriod};
use crate::enums::Division;
use crate::traits::{Bo4eMeta, Bo4eObject};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Costs {
#[serde(flatten)]
pub meta: Bo4eMeta,
#[serde(skip_serializing_if = "Option::is_none")]
pub designation: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub division: Option<Division>,
#[serde(skip_serializing_if = "Option::is_none")]
pub period: Option<TimePeriod>,
#[serde(skip_serializing_if = "Option::is_none")]
pub total_amount: Option<Amount>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub cost_blocks: Vec<CostBlock>,
#[serde(skip_serializing_if = "Option::is_none")]
pub market_location: Option<Box<super::MarketLocation>>,
}
impl Bo4eObject for Costs {
fn type_name_german() -> &'static str {
"Kosten"
}
fn type_name_english() -> &'static str {
"Costs"
}
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::CostBlock;
#[test]
fn test_costs_creation() {
let costs = Costs {
designation: Some("Jahreskosten 2024".to_string()),
division: Some(Division::Electricity),
total_amount: Some(Amount::eur(2500.00)),
..Default::default()
};
assert_eq!(costs.total_amount, Some(Amount::eur(2500.00)));
}
#[test]
fn test_costs_with_blocks() {
let costs = Costs {
designation: Some("Annual Costs".to_string()),
cost_blocks: vec![
CostBlock {
designation: Some("Energy".to_string()),
total_amount: Some(Amount::eur(1500.0)),
..Default::default()
},
CostBlock {
designation: Some("Network".to_string()),
total_amount: Some(Amount::eur(800.0)),
..Default::default()
},
],
total_amount: Some(Amount::eur(2300.0)),
..Default::default()
};
assert_eq!(costs.cost_blocks.len(), 2);
}
#[test]
fn test_serialize() {
let costs = Costs {
meta: Bo4eMeta::with_type("Kosten"),
designation: Some("Test Costs".to_string()),
total_amount: Some(Amount::eur(1000.0)),
..Default::default()
};
let json = serde_json::to_string(&costs).unwrap();
assert!(json.contains(r#""designation":"Test Costs""#));
assert!(json.contains(r#""_typ":"Kosten""#));
}
#[test]
fn test_roundtrip() {
let costs = Costs {
meta: Bo4eMeta::with_type("Kosten"),
designation: Some("Gas Costs".to_string()),
description: Some("Annual gas costs".to_string()),
division: Some(Division::Gas),
total_amount: Some(Amount::eur(1234.56)),
..Default::default()
};
let json = serde_json::to_string(&costs).unwrap();
let parsed: Costs = serde_json::from_str(&json).unwrap();
assert_eq!(costs, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(Costs::type_name_german(), "Kosten");
assert_eq!(Costs::type_name_english(), "Costs");
}
}