Skip to main content

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#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
27#[cfg_attr(feature = "json-schema", schemars(rename = "Fremdkostenblock"))]
28#[serde(rename_all = "camelCase")]
29pub struct ExternalCostBlock {
30    /// BO4E metadata
31    #[serde(flatten)]
32    pub meta: Bo4eMeta,
33
34    /// Name/designation of the cost block (Kostenblockbezeichnung)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    #[cfg_attr(feature = "json-schema", schemars(rename = "kostenblockbezeichnung"))]
37    pub designation: Option<String>,
38
39    /// Cost class (Kostenklasse)
40    #[serde(skip_serializing_if = "Option::is_none")]
41    #[cfg_attr(feature = "json-schema", schemars(rename = "kostenklasse"))]
42    pub cost_class: Option<CostClass>,
43
44    /// Sum of all costs in this block (Summe Kostenblock)
45    #[serde(skip_serializing_if = "Option::is_none")]
46    #[cfg_attr(feature = "json-schema", schemars(rename = "summeKostenblock"))]
47    pub total_amount: Option<Amount>,
48}
49
50impl Bo4eObject for ExternalCostBlock {
51    fn type_name_german() -> &'static str {
52        "Fremdkostenblock"
53    }
54
55    fn type_name_english() -> &'static str {
56        "ExternalCostBlock"
57    }
58
59    fn meta(&self) -> &Bo4eMeta {
60        &self.meta
61    }
62
63    fn meta_mut(&mut self) -> &mut Bo4eMeta {
64        &mut self.meta
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_external_cost_block() {
74        let cost_block = ExternalCostBlock {
75            designation: Some("Netzkosten Fremd".to_string()),
76            cost_class: Some(CostClass::ExternalCosts),
77            total_amount: Some(Amount::eur(350.0)),
78            ..Default::default()
79        };
80
81        assert_eq!(cost_block.designation, Some("Netzkosten Fremd".to_string()));
82    }
83
84    #[test]
85    fn test_default() {
86        let cost_block = ExternalCostBlock::default();
87        assert!(cost_block.designation.is_none());
88        assert!(cost_block.total_amount.is_none());
89    }
90
91    #[test]
92    fn test_roundtrip() {
93        let cost_block = ExternalCostBlock {
94            designation: Some("Messkosten extern".to_string()),
95            cost_class: Some(CostClass::InternalCosts),
96            total_amount: Some(Amount::eur(75.50)),
97            ..Default::default()
98        };
99
100        let json = serde_json::to_string(&cost_block).unwrap();
101        let parsed: ExternalCostBlock = serde_json::from_str(&json).unwrap();
102        assert_eq!(cost_block, parsed);
103    }
104
105    #[test]
106    fn test_bo4e_object_impl() {
107        assert_eq!(ExternalCostBlock::type_name_german(), "Fremdkostenblock");
108        assert_eq!(ExternalCostBlock::type_name_english(), "ExternalCostBlock");
109    }
110}