Skip to main content

fhir_types/r4b/
sampled_data.rs

1// @generated by fhir-codegen from hl7.fhir.r4b.core 4.3.0. DO NOT EDIT.
2// Change the emitter (tools/fhir-codegen) and regenerate.
3// SPDX-FileCopyrightText: Cadasto B.V.
4// SPDX-License-Identifier: Apache-2.0
5//! A series of measurements taken by a device
6
7/// A series of measurements taken by a device
8///
9/// A series of measurements taken by a device, with upper and lower limits.
10/// There may be more than one dimension in the data.
11#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct SampledData {
13    /// Unique id for inter-element referencing
14    ///
15    /// Unique id for the element within a resource (for internal references). This
16    /// may be any string value that does not contain spaces.
17    pub id: Option<std::string::String>,
18    /// Additional content defined by implementations
19    ///
20    /// May be used to represent additional information that is not part of the
21    /// basic definition of the element. To make the use of extensions safe and
22    /// manageable, there is a strict set of governance applied to the definition
23    /// and use of extensions. Though any implementer can define an extension, there
24    /// is a set of requirements that SHALL be met as part of the definition of the
25    /// extension.
26    pub extension: Vec<super::extension::Extension>,
27    /// Zero value and units
28    ///
29    /// The base quantity that a measured value of zero represents. In addition,
30    /// this provides the units of the entire measurement series.
31    pub origin: super::quantity::Quantity,
32    /// Number of milliseconds between samples
33    ///
34    /// The length of time between sampling times, measured in milliseconds.
35    pub period: super::primitives::Decimal,
36    /// Multiply data by this before adding to origin
37    ///
38    /// A correction factor that is applied to the sampled data points before they
39    /// are added to the origin.
40    pub factor: Option<super::primitives::Decimal>,
41    /// Lower limit of detection
42    ///
43    /// The lower limit of detection of the measured points. This is needed if any
44    /// of the data points have the value "L" (lower than detection limit).
45    pub lower_limit: Option<super::primitives::Decimal>,
46    /// Upper limit of detection
47    ///
48    /// The upper limit of detection of the measured points. This is needed if any
49    /// of the data points have the value "U" (higher than detection limit).
50    pub upper_limit: Option<super::primitives::Decimal>,
51    /// Number of sample points at each time point
52    ///
53    /// The number of sample points at each time point. If this value is greater
54    /// than one, then the dimensions will be interlaced - all the sample points for
55    /// a point in time will be recorded at once.
56    pub dimensions: super::primitives::PositiveInt,
57    /// Decimal values with spaces, or "E" | "U" | "L"
58    ///
59    /// A series of data points which are decimal values separated by a single space
60    /// (character u20). The special values "E" (error), "L" (below detection limit)
61    /// and "U" (above detection limit) can also be used in place of a decimal
62    /// value.
63    pub data: Option<super::primitives::String>,
64}
65
66impl super::super::codec::Json for SampledData {
67    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
68        let mut object = super::super::codec::Object::new();
69        if let Some(v) = &self.id {
70            object.insert(
71                std::string::String::from("id"),
72                super::super::codec::Value::String(v.clone()),
73            );
74        }
75        if !self.extension.is_empty() {
76            let mut items = Vec::with_capacity(self.extension.len());
77            for item in &self.extension {
78                items.push(super::super::codec::Value::Object(
79                    super::super::codec::Json::to_json(item)?,
80                ));
81            }
82            object.insert(
83                std::string::String::from("extension"),
84                super::super::codec::Value::Array(items),
85            );
86        }
87        object.insert(
88            std::string::String::from("origin"),
89            super::super::codec::Value::Object(super::super::codec::Json::to_json(&self.origin)?),
90        );
91        if let Some(v) = super::super::codec::Primitive::value_json(&self.period)? {
92            object.insert(std::string::String::from("period"), v);
93        }
94        if let Some(e) = super::super::codec::Primitive::element_json(&self.period)? {
95            object.insert(std::string::String::from("_period"), e);
96        }
97        if let Some(item) = &self.factor {
98            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
99                object.insert(std::string::String::from("factor"), v);
100            }
101            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
102                object.insert(std::string::String::from("_factor"), e);
103            }
104        }
105        if let Some(item) = &self.lower_limit {
106            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
107                object.insert(std::string::String::from("lowerLimit"), v);
108            }
109            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
110                object.insert(std::string::String::from("_lowerLimit"), e);
111            }
112        }
113        if let Some(item) = &self.upper_limit {
114            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
115                object.insert(std::string::String::from("upperLimit"), v);
116            }
117            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
118                object.insert(std::string::String::from("_upperLimit"), e);
119            }
120        }
121        if let Some(v) = super::super::codec::Primitive::value_json(&self.dimensions)? {
122            object.insert(std::string::String::from("dimensions"), v);
123        }
124        if let Some(e) = super::super::codec::Primitive::element_json(&self.dimensions)? {
125            object.insert(std::string::String::from("_dimensions"), e);
126        }
127        if let Some(item) = &self.data {
128            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
129                object.insert(std::string::String::from("data"), v);
130            }
131            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
132                object.insert(std::string::String::from("_data"), e);
133            }
134        }
135        Ok(object)
136    }
137
138    fn from_json(
139        object: &super::super::codec::Object,
140        path: &mut super::super::codec::Path,
141    ) -> Result<Self, super::super::codec::DecodeError> {
142        let mut raw_id: Option<&super::super::codec::Value> = None;
143        let mut raw_extension: Option<&super::super::codec::Value> = None;
144        let mut raw_origin: Option<&super::super::codec::Value> = None;
145        let mut raw_period: Option<&super::super::codec::Value> = None;
146        let mut raw_period_element: Option<&super::super::codec::Value> = None;
147        let mut raw_factor: Option<&super::super::codec::Value> = None;
148        let mut raw_factor_element: Option<&super::super::codec::Value> = None;
149        let mut raw_lower_limit: Option<&super::super::codec::Value> = None;
150        let mut raw_lower_limit_element: Option<&super::super::codec::Value> = None;
151        let mut raw_upper_limit: Option<&super::super::codec::Value> = None;
152        let mut raw_upper_limit_element: Option<&super::super::codec::Value> = None;
153        let mut raw_dimensions: Option<&super::super::codec::Value> = None;
154        let mut raw_dimensions_element: Option<&super::super::codec::Value> = None;
155        let mut raw_data: Option<&super::super::codec::Value> = None;
156        let mut raw_data_element: Option<&super::super::codec::Value> = None;
157        for (key, value) in object {
158            match key.as_str() {
159                "id" => {
160                    raw_id = Some(value);
161                }
162                "extension" => {
163                    raw_extension = Some(value);
164                }
165                "origin" => {
166                    raw_origin = Some(value);
167                }
168                "period" => {
169                    raw_period = Some(value);
170                }
171                "_period" => {
172                    raw_period_element = Some(value);
173                }
174                "factor" => {
175                    raw_factor = Some(value);
176                }
177                "_factor" => {
178                    raw_factor_element = Some(value);
179                }
180                "lowerLimit" => {
181                    raw_lower_limit = Some(value);
182                }
183                "_lowerLimit" => {
184                    raw_lower_limit_element = Some(value);
185                }
186                "upperLimit" => {
187                    raw_upper_limit = Some(value);
188                }
189                "_upperLimit" => {
190                    raw_upper_limit_element = Some(value);
191                }
192                "dimensions" => {
193                    raw_dimensions = Some(value);
194                }
195                "_dimensions" => {
196                    raw_dimensions_element = Some(value);
197                }
198                "data" => {
199                    raw_data = Some(value);
200                }
201                "_data" => {
202                    raw_data_element = Some(value);
203                }
204                other => {
205                    return path.with(other, |path| {
206                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
207                    });
208                }
209            }
210        }
211        let field_id = raw_id
212            .map(|value| {
213                path.with("id", |path| {
214                    let value = super::super::codec::expect_single(value, path)?;
215                    super::primitives::checked_string(
216                        super::super::codec::expect_string(value, path)?,
217                        path,
218                    )
219                })
220            })
221            .transpose()?;
222        let mut field_extension = Vec::new();
223        if let Some(raw) = raw_extension {
224            for (index, value) in path
225                .with("extension", |path| {
226                    super::super::codec::expect_array(raw, path)
227                })?
228                .iter()
229                .enumerate()
230            {
231                field_extension.push(path.with_index("extension", index, |path| {
232                    super::super::codec::Json::from_json(
233                        super::super::codec::expect_object(value, path)?,
234                        path,
235                    )
236                })?);
237            }
238        }
239        let field_origin = path.with("origin", |path| {
240            let value = raw_origin
241                .ok_or_else(|| path.error(super::super::codec::DecodeErrorKind::MissingProperty))?;
242            super::super::codec::Json::from_json(
243                super::super::codec::expect_object(
244                    super::super::codec::expect_single(value, path)?,
245                    path,
246                )?,
247                path,
248            )
249        })?;
250        let field_period = path.with("period", |path| {
251            super::super::codec::Primitive::from_json_parts(raw_period, raw_period_element, path)
252        })?;
253        let field_factor = match (raw_factor, raw_factor_element) {
254            (None, None) => None,
255            (value, element) => Some(path.with("factor", |path| {
256                super::super::codec::Primitive::from_json_parts(value, element, path)
257            })?),
258        };
259        let field_lower_limit = match (raw_lower_limit, raw_lower_limit_element) {
260            (None, None) => None,
261            (value, element) => Some(path.with("lowerLimit", |path| {
262                super::super::codec::Primitive::from_json_parts(value, element, path)
263            })?),
264        };
265        let field_upper_limit = match (raw_upper_limit, raw_upper_limit_element) {
266            (None, None) => None,
267            (value, element) => Some(path.with("upperLimit", |path| {
268                super::super::codec::Primitive::from_json_parts(value, element, path)
269            })?),
270        };
271        let field_dimensions = path.with("dimensions", |path| {
272            super::super::codec::Primitive::from_json_parts(
273                raw_dimensions,
274                raw_dimensions_element,
275                path,
276            )
277        })?;
278        let field_data = match (raw_data, raw_data_element) {
279            (None, None) => None,
280            (value, element) => Some(path.with("data", |path| {
281                super::super::codec::Primitive::from_json_parts(value, element, path)
282            })?),
283        };
284        Ok(Self {
285            id: field_id,
286            extension: field_extension,
287            origin: field_origin,
288            period: field_period,
289            factor: field_factor,
290            lower_limit: field_lower_limit,
291            upper_limit: field_upper_limit,
292            dimensions: field_dimensions,
293            data: field_data,
294        })
295    }
296}
297
298impl serde::Serialize for SampledData {
299    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
300        let mut map = serde::Serializer::serialize_map(serializer, None)?;
301        if let Some(item) = &self.data {
302            super::super::codec::element_entry(&mut map, "_data", item)?;
303        }
304        super::super::codec::element_entry(&mut map, "_dimensions", &self.dimensions)?;
305        if let Some(item) = &self.factor {
306            super::super::codec::element_entry(&mut map, "_factor", item)?;
307        }
308        if let Some(item) = &self.lower_limit {
309            super::super::codec::element_entry(&mut map, "_lowerLimit", item)?;
310        }
311        super::super::codec::element_entry(&mut map, "_period", &self.period)?;
312        if let Some(item) = &self.upper_limit {
313            super::super::codec::element_entry(&mut map, "_upperLimit", item)?;
314        }
315        if let Some(item) = &self.data {
316            super::super::codec::value_entry(&mut map, "data", item)?;
317        }
318        super::super::codec::value_entry(&mut map, "dimensions", &self.dimensions)?;
319        if !self.extension.is_empty() {
320            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
321        }
322        if let Some(item) = &self.factor {
323            super::super::codec::value_entry(&mut map, "factor", item)?;
324        }
325        if let Some(v) = &self.id {
326            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
327        }
328        if let Some(item) = &self.lower_limit {
329            super::super::codec::value_entry(&mut map, "lowerLimit", item)?;
330        }
331        serde::ser::SerializeMap::serialize_entry(&mut map, "origin", &self.origin)?;
332        super::super::codec::value_entry(&mut map, "period", &self.period)?;
333        if let Some(item) = &self.upper_limit {
334            super::super::codec::value_entry(&mut map, "upperLimit", item)?;
335        }
336        serde::ser::SerializeMap::end(map)
337    }
338}
339
340impl<'de> serde::Deserialize<'de> for SampledData {
341    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
342        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
343        let mut path = super::super::codec::Path::root("SampledData");
344        let object =
345            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
346        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
347    }
348}