bo4e_core/com/
price_tier.rs1use serde::{Deserialize, Serialize};
4
5use crate::traits::{Bo4eMeta, Bo4eObject};
6
7#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
25#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
26#[cfg_attr(feature = "json-schema", schemars(rename = "Preisstaffel"))]
27#[serde(rename_all = "camelCase")]
28pub struct PriceTier {
29 #[serde(flatten)]
31 pub meta: Bo4eMeta,
32
33 #[serde(skip_serializing_if = "Option::is_none")]
35 #[cfg_attr(feature = "json-schema", schemars(rename = "staffelgrenzeVon"))]
36 pub lower_limit: Option<f64>,
37
38 #[serde(skip_serializing_if = "Option::is_none")]
40 #[cfg_attr(feature = "json-schema", schemars(rename = "staffelgrenzeBis"))]
41 pub upper_limit: Option<f64>,
42
43 #[serde(skip_serializing_if = "Option::is_none")]
45 #[cfg_attr(feature = "json-schema", schemars(rename = "einheitspreis"))]
46 pub unit_price: Option<f64>,
47
48 #[serde(skip_serializing_if = "Option::is_none")]
50 #[cfg_attr(feature = "json-schema", schemars(rename = "staffelnummer"))]
51 pub tier_number: Option<i32>,
52
53 #[serde(skip_serializing_if = "Option::is_none")]
55 #[cfg_attr(feature = "json-schema", schemars(rename = "artikelId"))]
56 pub article_id: Option<String>,
57}
58
59impl Bo4eObject for PriceTier {
60 fn type_name_german() -> &'static str {
61 "Preisstaffel"
62 }
63
64 fn type_name_english() -> &'static str {
65 "PriceTier"
66 }
67
68 fn meta(&self) -> &Bo4eMeta {
69 &self.meta
70 }
71
72 fn meta_mut(&mut self) -> &mut Bo4eMeta {
73 &mut self.meta
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn test_consumption_tiers() {
83 let tier1 = PriceTier {
84 lower_limit: Some(0.0),
85 upper_limit: Some(1000.0),
86 unit_price: Some(0.30),
87 tier_number: Some(1),
88 ..Default::default()
89 };
90
91 let tier2 = PriceTier {
92 lower_limit: Some(1000.0),
93 upper_limit: Some(5000.0),
94 unit_price: Some(0.25),
95 tier_number: Some(2),
96 ..Default::default()
97 };
98
99 assert!(tier1.unit_price.unwrap() > tier2.unit_price.unwrap());
100 }
101
102 #[test]
103 fn test_default() {
104 let tier = PriceTier::default();
105 assert!(tier.lower_limit.is_none());
106 assert!(tier.upper_limit.is_none());
107 assert!(tier.unit_price.is_none());
108 }
109
110 #[test]
111 fn test_serialize() {
112 let tier = PriceTier {
113 lower_limit: Some(0.0),
114 upper_limit: Some(1000.0),
115 unit_price: Some(0.30),
116 tier_number: Some(1),
117 ..Default::default()
118 };
119
120 let json = serde_json::to_string(&tier).unwrap();
121 assert!(json.contains(r#""lowerLimit":0"#));
122 assert!(json.contains(r#""upperLimit":1000"#));
123 assert!(json.contains(r#""unitPrice":0.3"#));
124 assert!(json.contains(r#""tierNumber":1"#));
125 }
126
127 #[test]
128 fn test_roundtrip() {
129 let tier = PriceTier {
130 lower_limit: Some(100.0),
131 upper_limit: Some(500.0),
132 unit_price: Some(0.28),
133 tier_number: Some(2),
134 article_id: Some("BDEW-123".to_string()),
135 ..Default::default()
136 };
137
138 let json = serde_json::to_string(&tier).unwrap();
139 let parsed: PriceTier = serde_json::from_str(&json).unwrap();
140 assert_eq!(tier, parsed);
141 }
142
143 #[test]
144 fn test_bo4e_object_impl() {
145 assert_eq!(PriceTier::type_name_german(), "Preisstaffel");
146 assert_eq!(PriceTier::type_name_english(), "PriceTier");
147 }
148}