Skip to main content

bo4e_core/com/
substitution_value.rs

1//! Substitution value (Ersatzwert) component.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::enums::Unit;
7use crate::traits::{Bo4eMeta, Bo4eObject};
8
9/// A substituted/replacement value for missing or invalid measurements.
10///
11/// German: Ersatzwert
12///
13/// # Example
14///
15/// ```rust
16/// use bo4e_core::com::SubstitutionValue;
17/// use bo4e_core::enums::Unit;
18/// use chrono::Utc;
19///
20/// let value = SubstitutionValue {
21///     timestamp: Some(Utc::now()),
22///     value: Some(125.5),
23///     unit: Some(Unit::KilowattHour),
24///     substitution_method: Some("Interpolation".to_string()),
25///     ..Default::default()
26/// };
27/// ```
28#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
29#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
30#[cfg_attr(feature = "json-schema", schemars(rename = "Ersatzwert"))]
31#[serde(rename_all = "camelCase")]
32pub struct SubstitutionValue {
33    /// BO4E metadata
34    #[serde(flatten)]
35    pub meta: Bo4eMeta,
36
37    /// Timestamp for the substituted value (Zeitpunkt)
38    #[serde(skip_serializing_if = "Option::is_none")]
39    #[cfg_attr(feature = "json-schema", schemars(rename = "zeitpunkt"))]
40    pub timestamp: Option<DateTime<Utc>>,
41
42    /// The substituted value (Wert)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[cfg_attr(feature = "json-schema", schemars(rename = "wert"))]
45    pub value: Option<f64>,
46
47    /// Unit of the value (Einheit)
48    #[serde(skip_serializing_if = "Option::is_none")]
49    #[cfg_attr(feature = "json-schema", schemars(rename = "einheit"))]
50    pub unit: Option<Unit>,
51
52    /// Method used for substitution (Ersatzwertmethode)
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[cfg_attr(feature = "json-schema", schemars(rename = "ersatzwertmethode"))]
55    pub substitution_method: Option<String>,
56
57    /// Reason for substitution (Grund)
58    #[serde(skip_serializing_if = "Option::is_none")]
59    #[cfg_attr(feature = "json-schema", schemars(rename = "grund"))]
60    pub reason: Option<String>,
61
62    /// Original value that was replaced, if available (Originalwert)
63    #[serde(skip_serializing_if = "Option::is_none")]
64    #[cfg_attr(feature = "json-schema", schemars(rename = "originalwert"))]
65    pub original_value: Option<f64>,
66
67    /// OBIS code (OBIS-Kennzahl)
68    #[serde(skip_serializing_if = "Option::is_none")]
69    #[cfg_attr(feature = "json-schema", schemars(rename = "obisKennzahl"))]
70    pub obis_code: Option<String>,
71}
72
73impl Bo4eObject for SubstitutionValue {
74    fn type_name_german() -> &'static str {
75        "Ersatzwert"
76    }
77
78    fn type_name_english() -> &'static str {
79        "SubstitutionValue"
80    }
81
82    fn meta(&self) -> &Bo4eMeta {
83        &self.meta
84    }
85
86    fn meta_mut(&mut self) -> &mut Bo4eMeta {
87        &mut self.meta
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94    use chrono::TimeZone;
95
96    #[test]
97    fn test_substitution_value() {
98        let value = SubstitutionValue {
99            timestamp: Some(Utc.with_ymd_and_hms(2024, 1, 15, 12, 0, 0).unwrap()),
100            value: Some(125.5),
101            unit: Some(Unit::KilowattHour),
102            substitution_method: Some("Interpolation".to_string()),
103            reason: Some("Meter communication failure".to_string()),
104            ..Default::default()
105        };
106
107        let json = serde_json::to_string(&value).unwrap();
108        assert!(json.contains("125.5"));
109        assert!(json.contains("Interpolation"));
110    }
111
112    #[test]
113    fn test_with_original() {
114        let value = SubstitutionValue {
115            value: Some(100.0),
116            original_value: Some(-50.0),
117            reason: Some("Negative value not allowed".to_string()),
118            ..Default::default()
119        };
120
121        let json = serde_json::to_string(&value).unwrap();
122        assert!(json.contains("100"));
123        assert!(json.contains("-50"));
124    }
125
126    #[test]
127    fn test_roundtrip() {
128        let value = SubstitutionValue {
129            timestamp: Some(Utc::now()),
130            value: Some(999.99),
131            substitution_method: Some("Historical average".to_string()),
132            ..Default::default()
133        };
134
135        let json = serde_json::to_string(&value).unwrap();
136        let parsed: SubstitutionValue = serde_json::from_str(&json).unwrap();
137        assert_eq!(value.value, parsed.value);
138        assert_eq!(value.substitution_method, parsed.substitution_method);
139    }
140
141    #[test]
142    fn test_bo4e_object_impl() {
143        assert_eq!(SubstitutionValue::type_name_german(), "Ersatzwert");
144        assert_eq!(SubstitutionValue::type_name_english(), "SubstitutionValue");
145    }
146}