Skip to main content

bo4e_core/com/
quantity.rs

1//! Quantity (Menge) component.
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::Unit;
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8/// A quantity with value and unit.
9///
10/// German: Menge
11///
12/// # Example
13///
14/// ```rust
15/// use bo4e_core::com::Quantity;
16/// use bo4e_core::enums::Unit;
17///
18/// let qty = Quantity {
19///     value: Some(3500.0),
20///     unit: Some(Unit::KilowattHour),
21///     ..Default::default()
22/// };
23/// ```
24#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
25#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
26#[cfg_attr(feature = "json-schema", schemars(rename = "Menge"))]
27#[serde(rename_all = "camelCase")]
28pub struct Quantity {
29    /// BO4E metadata
30    #[serde(flatten)]
31    pub meta: Bo4eMeta,
32
33    /// Numeric value (Wert)
34    #[serde(skip_serializing_if = "Option::is_none")]
35    #[cfg_attr(feature = "json-schema", schemars(rename = "wert"))]
36    pub value: Option<f64>,
37
38    /// Unit of measurement (Einheit)
39    #[serde(skip_serializing_if = "Option::is_none")]
40    #[cfg_attr(feature = "json-schema", schemars(rename = "einheit"))]
41    pub unit: Option<Unit>,
42}
43
44impl Bo4eObject for Quantity {
45    fn type_name_german() -> &'static str {
46        "Menge"
47    }
48
49    fn type_name_english() -> &'static str {
50        "Quantity"
51    }
52
53    fn meta(&self) -> &Bo4eMeta {
54        &self.meta
55    }
56
57    fn meta_mut(&mut self) -> &mut Bo4eMeta {
58        &mut self.meta
59    }
60}
61
62impl Quantity {
63    /// Create a quantity in kWh.
64    pub fn kwh(value: f64) -> Self {
65        Self {
66            value: Some(value),
67            unit: Some(Unit::KilowattHour),
68            ..Default::default()
69        }
70    }
71
72    /// Create a quantity in cubic meters.
73    pub fn cubic_meters(value: f64) -> Self {
74        Self {
75            value: Some(value),
76            unit: Some(Unit::CubicMeter),
77            ..Default::default()
78        }
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_kwh_quantity() {
88        let qty = Quantity::kwh(3500.0);
89        assert_eq!(qty.value, Some(3500.0));
90        assert_eq!(qty.unit, Some(Unit::KilowattHour));
91    }
92
93    #[test]
94    fn test_gas_quantity() {
95        let qty = Quantity::cubic_meters(1500.0);
96        assert_eq!(qty.unit, Some(Unit::CubicMeter));
97    }
98
99    #[test]
100    fn test_roundtrip() {
101        let qty = Quantity::kwh(12345.67);
102        let json = serde_json::to_string(&qty).unwrap();
103        let parsed: Quantity = serde_json::from_str(&json).unwrap();
104        assert_eq!(qty, parsed);
105    }
106
107    #[test]
108    fn test_bo4e_object_impl() {
109        assert_eq!(Quantity::type_name_german(), "Menge");
110        assert_eq!(Quantity::type_name_english(), "Quantity");
111    }
112}