bo4e_core/com/
external_cost_block.rs

1//! External cost block (Fremdkostenblock) component.
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::CostClass;
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8use super::Amount;
9
10/// A block of external (third-party) costs.
11///
12/// German: Fremdkostenblock
13///
14/// # Example
15///
16/// ```rust
17/// use bo4e_core::com::{ExternalCostBlock, Amount};
18///
19/// let cost_block = ExternalCostBlock {
20///     designation: Some("Netzkosten Fremd".to_string()),
21///     total_amount: Some(Amount::eur(350.0)),
22///     ..Default::default()
23/// };
24/// ```
25#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub struct ExternalCostBlock {
28    /// BO4E metadata
29    #[serde(flatten)]
30    pub meta: Bo4eMeta,
31
32    /// Name/designation of the cost block (Kostenblockbezeichnung)
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub designation: Option<String>,
35
36    /// Cost class (Kostenklasse)
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub cost_class: Option<CostClass>,
39
40    /// Sum of all costs in this block (Summe Kostenblock)
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub total_amount: Option<Amount>,
43}
44
45impl Bo4eObject for ExternalCostBlock {
46    fn type_name_german() -> &'static str {
47        "Fremdkostenblock"
48    }
49
50    fn type_name_english() -> &'static str {
51        "ExternalCostBlock"
52    }
53
54    fn meta(&self) -> &Bo4eMeta {
55        &self.meta
56    }
57
58    fn meta_mut(&mut self) -> &mut Bo4eMeta {
59        &mut self.meta
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_external_cost_block() {
69        let cost_block = ExternalCostBlock {
70            designation: Some("Netzkosten Fremd".to_string()),
71            cost_class: Some(CostClass::ExternalCosts),
72            total_amount: Some(Amount::eur(350.0)),
73            ..Default::default()
74        };
75
76        assert_eq!(cost_block.designation, Some("Netzkosten Fremd".to_string()));
77    }
78
79    #[test]
80    fn test_default() {
81        let cost_block = ExternalCostBlock::default();
82        assert!(cost_block.designation.is_none());
83        assert!(cost_block.total_amount.is_none());
84    }
85
86    #[test]
87    fn test_roundtrip() {
88        let cost_block = ExternalCostBlock {
89            designation: Some("Messkosten extern".to_string()),
90            cost_class: Some(CostClass::InternalCosts),
91            total_amount: Some(Amount::eur(75.50)),
92            ..Default::default()
93        };
94
95        let json = serde_json::to_string(&cost_block).unwrap();
96        let parsed: ExternalCostBlock = serde_json::from_str(&json).unwrap();
97        assert_eq!(cost_block, parsed);
98    }
99
100    #[test]
101    fn test_bo4e_object_impl() {
102        assert_eq!(ExternalCostBlock::type_name_german(), "Fremdkostenblock");
103        assert_eq!(ExternalCostBlock::type_name_english(), "ExternalCostBlock");
104    }
105}