bo4e_core/com/
price_position.rs1use serde::{Deserialize, Serialize};
4
5use crate::enums::{CalculationMethod, PriceType, Unit};
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8use super::PriceTier;
9
10#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
27#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
28#[cfg_attr(feature = "json-schema", schemars(rename = "Preisposition"))]
29#[serde(rename_all = "camelCase")]
30pub struct PricePosition {
31 #[serde(flatten)]
33 pub meta: Bo4eMeta,
34
35 #[serde(skip_serializing_if = "Option::is_none")]
37 #[cfg_attr(feature = "json-schema", schemars(rename = "bezeichnung"))]
38 pub description: Option<String>,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
42 #[cfg_attr(feature = "json-schema", schemars(rename = "preistyp"))]
43 pub price_type: Option<PriceType>,
44
45 #[serde(skip_serializing_if = "Option::is_none")]
47 #[cfg_attr(feature = "json-schema", schemars(rename = "bezugseinheit"))]
48 pub reference_unit: Option<Unit>,
49
50 #[serde(skip_serializing_if = "Option::is_none")]
52 #[cfg_attr(feature = "json-schema", schemars(rename = "berechnungsmethode"))]
53 pub calculation_method: Option<CalculationMethod>,
54
55 #[serde(default, skip_serializing_if = "Vec::is_empty")]
57 #[cfg_attr(feature = "json-schema", schemars(rename = "preisstaffeln"))]
58 pub tiers: Vec<PriceTier>,
59
60 #[serde(skip_serializing_if = "Option::is_none")]
62 #[cfg_attr(feature = "json-schema", schemars(rename = "artikelId"))]
63 pub article_id: Option<String>,
64
65 #[serde(skip_serializing_if = "Option::is_none")]
67 #[cfg_attr(feature = "json-schema", schemars(rename = "bdewArtikelnummer"))]
68 pub bdew_article_number: Option<String>,
69}
70
71impl Bo4eObject for PricePosition {
72 fn type_name_german() -> &'static str {
73 "Preisposition"
74 }
75
76 fn type_name_english() -> &'static str {
77 "PricePosition"
78 }
79
80 fn meta(&self) -> &Bo4eMeta {
81 &self.meta
82 }
83
84 fn meta_mut(&mut self) -> &mut Bo4eMeta {
85 &mut self.meta
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn test_work_price_position() {
95 let position = PricePosition {
96 description: Some("Arbeitspreis HT".to_string()),
97 price_type: Some(PriceType::WorkingPriceSingleTariff),
98 reference_unit: Some(Unit::KilowattHour),
99 tiers: vec![PriceTier {
100 unit_price: Some(0.30),
101 ..Default::default()
102 }],
103 ..Default::default()
104 };
105
106 assert_eq!(
107 position.price_type,
108 Some(PriceType::WorkingPriceSingleTariff)
109 );
110 assert_eq!(position.tiers.len(), 1);
111 }
112
113 #[test]
114 fn test_default() {
115 let position = PricePosition::default();
116 assert!(position.description.is_none());
117 assert!(position.tiers.is_empty());
118 }
119
120 #[test]
121 fn test_roundtrip() {
122 let position = PricePosition {
123 description: Some("Grundpreis".to_string()),
124 price_type: Some(PriceType::BasePrice),
125 reference_unit: Some(Unit::Month),
126 bdew_article_number: Some("9990001".to_string()),
127 ..Default::default()
128 };
129
130 let json = serde_json::to_string(&position).unwrap();
131 let parsed: PricePosition = serde_json::from_str(&json).unwrap();
132 assert_eq!(position, parsed);
133 }
134
135 #[test]
136 fn test_bo4e_object_impl() {
137 assert_eq!(PricePosition::type_name_german(), "Preisposition");
138 assert_eq!(PricePosition::type_name_english(), "PricePosition");
139 }
140}