Skip to main content

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#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
25#[cfg_attr(feature = "json-schema", schemars(rename = "Margenpreis"))]
26#[serde(rename_all = "camelCase")]
27pub struct MarginPrice {
28    /// BO4E metadata
29    #[serde(flatten)]
30    pub meta: Bo4eMeta,
31
32    /// Margin value (Wert)
33    #[serde(skip_serializing_if = "Option::is_none")]
34    #[cfg_attr(feature = "json-schema", schemars(rename = "wert"))]
35    pub value: Option<f64>,
36
37    /// Currency (Waehrung)
38    #[serde(skip_serializing_if = "Option::is_none")]
39    #[cfg_attr(feature = "json-schema", schemars(rename = "waehrung"))]
40    pub currency: Option<Currency>,
41
42    /// Reference unit (Bezugseinheit)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[cfg_attr(feature = "json-schema", schemars(rename = "bezugseinheit"))]
45    pub reference_unit: Option<Unit>,
46
47    /// Description (Beschreibung)
48    #[serde(skip_serializing_if = "Option::is_none")]
49    #[cfg_attr(feature = "json-schema", schemars(rename = "beschreibung"))]
50    pub description: Option<String>,
51}
52
53impl Bo4eObject for MarginPrice {
54    fn type_name_german() -> &'static str {
55        "Margenpreis"
56    }
57
58    fn type_name_english() -> &'static str {
59        "MarginPrice"
60    }
61
62    fn meta(&self) -> &Bo4eMeta {
63        &self.meta
64    }
65
66    fn meta_mut(&mut self) -> &mut Bo4eMeta {
67        &mut self.meta
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_margin_price() {
77        let margin = MarginPrice {
78            value: Some(0.5),
79            currency: Some(Currency::Eur),
80            reference_unit: Some(Unit::KilowattHour),
81            description: Some("Vertriebsmarge".to_string()),
82            ..Default::default()
83        };
84
85        assert_eq!(margin.value, Some(0.5));
86    }
87
88    #[test]
89    fn test_default() {
90        let margin = MarginPrice::default();
91        assert!(margin.value.is_none());
92        assert!(margin.description.is_none());
93    }
94
95    #[test]
96    fn test_roundtrip() {
97        let margin = MarginPrice {
98            value: Some(1.25),
99            currency: Some(Currency::Eur),
100            reference_unit: Some(Unit::KilowattHour),
101            description: Some("Deckungsbeitrag".to_string()),
102            ..Default::default()
103        };
104
105        let json = serde_json::to_string(&margin).unwrap();
106        let parsed: MarginPrice = serde_json::from_str(&json).unwrap();
107        assert_eq!(margin, parsed);
108    }
109
110    #[test]
111    fn test_bo4e_object_impl() {
112        assert_eq!(MarginPrice::type_name_german(), "Margenpreis");
113        assert_eq!(MarginPrice::type_name_english(), "MarginPrice");
114    }
115}