bo4e_core/com/
margin_price.rs

1//! Margin price (Margenpreis) component.
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::{Currency, Unit};
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8/// A margin price component.
9///
10/// German: Margenpreis
11///
12/// # Example
13///
14/// ```rust
15/// use bo4e_core::com::MarginPrice;
16///
17/// let margin = MarginPrice {
18///     value: Some(0.5),
19///     description: Some("Vertriebsmarge".to_string()),
20///     ..Default::default()
21/// };
22/// ```
23#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase")]
25pub struct MarginPrice {
26    /// BO4E metadata
27    #[serde(flatten)]
28    pub meta: Bo4eMeta,
29
30    /// Margin value (Wert)
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub value: Option<f64>,
33
34    /// Currency (Waehrung)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub currency: Option<Currency>,
37
38    /// Reference unit (Bezugseinheit)
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub reference_unit: Option<Unit>,
41
42    /// Description (Beschreibung)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub description: Option<String>,
45}
46
47impl Bo4eObject for MarginPrice {
48    fn type_name_german() -> &'static str {
49        "Margenpreis"
50    }
51
52    fn type_name_english() -> &'static str {
53        "MarginPrice"
54    }
55
56    fn meta(&self) -> &Bo4eMeta {
57        &self.meta
58    }
59
60    fn meta_mut(&mut self) -> &mut Bo4eMeta {
61        &mut self.meta
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_margin_price() {
71        let margin = MarginPrice {
72            value: Some(0.5),
73            currency: Some(Currency::Eur),
74            reference_unit: Some(Unit::KilowattHour),
75            description: Some("Vertriebsmarge".to_string()),
76            ..Default::default()
77        };
78
79        assert_eq!(margin.value, Some(0.5));
80    }
81
82    #[test]
83    fn test_default() {
84        let margin = MarginPrice::default();
85        assert!(margin.value.is_none());
86        assert!(margin.description.is_none());
87    }
88
89    #[test]
90    fn test_roundtrip() {
91        let margin = MarginPrice {
92            value: Some(1.25),
93            currency: Some(Currency::Eur),
94            reference_unit: Some(Unit::KilowattHour),
95            description: Some("Deckungsbeitrag".to_string()),
96            ..Default::default()
97        };
98
99        let json = serde_json::to_string(&margin).unwrap();
100        let parsed: MarginPrice = serde_json::from_str(&json).unwrap();
101        assert_eq!(margin, parsed);
102    }
103
104    #[test]
105    fn test_bo4e_object_impl() {
106        assert_eq!(MarginPrice::type_name_german(), "Margenpreis");
107        assert_eq!(MarginPrice::type_name_english(), "MarginPrice");
108    }
109}