bo4e_core/com/
price_position.rs

1//! Price position (Preisposition) component.
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::{CalculationMethod, PriceType, Unit};
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8use super::PriceTier;
9
10/// A position in a price sheet with its associated price tiers.
11///
12/// German: Preisposition
13///
14/// # Example
15///
16/// ```rust
17/// use bo4e_core::com::PricePosition;
18/// use bo4e_core::enums::PriceType;
19///
20/// let position = PricePosition {
21///     description: Some("Arbeitspreis HT".to_string()),
22///     price_type: Some(PriceType::WorkingPriceSingleTariff),
23///     ..Default::default()
24/// };
25/// ```
26#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
27#[serde(rename_all = "camelCase")]
28pub struct PricePosition {
29    /// BO4E metadata
30    #[serde(flatten)]
31    pub meta: Bo4eMeta,
32
33    /// Description/name of the price position (Bezeichnung)
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub description: Option<String>,
36
37    /// Type of price (Preistyp)
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub price_type: Option<PriceType>,
40
41    /// Reference unit (Bezugseinheit)
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub reference_unit: Option<Unit>,
44
45    /// Calculation method (Berechnungsmethode)
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub calculation_method: Option<CalculationMethod>,
48
49    /// Price tiers (Preisstaffeln)
50    #[serde(default, skip_serializing_if = "Vec::is_empty")]
51    pub tiers: Vec<PriceTier>,
52
53    /// Article ID (Artikel-ID)
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub article_id: Option<String>,
56
57    /// BDEW article number (BDEW Artikelnummer)
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub bdew_article_number: Option<String>,
60}
61
62impl Bo4eObject for PricePosition {
63    fn type_name_german() -> &'static str {
64        "Preisposition"
65    }
66
67    fn type_name_english() -> &'static str {
68        "PricePosition"
69    }
70
71    fn meta(&self) -> &Bo4eMeta {
72        &self.meta
73    }
74
75    fn meta_mut(&mut self) -> &mut Bo4eMeta {
76        &mut self.meta
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_work_price_position() {
86        let position = PricePosition {
87            description: Some("Arbeitspreis HT".to_string()),
88            price_type: Some(PriceType::WorkingPriceSingleTariff),
89            reference_unit: Some(Unit::KilowattHour),
90            tiers: vec![PriceTier {
91                unit_price: Some(0.30),
92                ..Default::default()
93            }],
94            ..Default::default()
95        };
96
97        assert_eq!(
98            position.price_type,
99            Some(PriceType::WorkingPriceSingleTariff)
100        );
101        assert_eq!(position.tiers.len(), 1);
102    }
103
104    #[test]
105    fn test_default() {
106        let position = PricePosition::default();
107        assert!(position.description.is_none());
108        assert!(position.tiers.is_empty());
109    }
110
111    #[test]
112    fn test_roundtrip() {
113        let position = PricePosition {
114            description: Some("Grundpreis".to_string()),
115            price_type: Some(PriceType::BasePrice),
116            reference_unit: Some(Unit::Month),
117            bdew_article_number: Some("9990001".to_string()),
118            ..Default::default()
119        };
120
121        let json = serde_json::to_string(&position).unwrap();
122        let parsed: PricePosition = serde_json::from_str(&json).unwrap();
123        assert_eq!(position, parsed);
124    }
125
126    #[test]
127    fn test_bo4e_object_impl() {
128        assert_eq!(PricePosition::type_name_german(), "Preisposition");
129        assert_eq!(PricePosition::type_name_english(), "PricePosition");
130    }
131}