Skip to main content

fhir_types/r5/
expression.rs

1// @generated by fhir-codegen from hl7.fhir.r5.core 5.0.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//! An expression that can be used to generate a value
6
7/// An expression that can be used to generate a value
8///
9/// A expression that is evaluated in a specified context and returns a value.
10/// The context of use of the expression must specify the context in which the
11/// expression is evaluated, and how the result of the expression is used.
12#[derive(Debug, Clone, Default, PartialEq, Eq)]
13pub struct Expression {
14    /// Unique id for inter-element referencing
15    ///
16    /// Unique id for the element within a resource (for internal references). This
17    /// may be any string value that does not contain spaces.
18    pub id: Option<std::string::String>,
19    /// Additional content defined by implementations
20    ///
21    /// May be used to represent additional information that is not part of the
22    /// basic definition of the element. To make the use of extensions safe and
23    /// managable, there is a strict set of governance applied to the definition and
24    /// use of extensions. Though any implementer can define an extension, there is
25    /// a set of requirements that SHALL be met as part of the definition of the
26    /// extension.
27    pub extension: Vec<super::extension::Extension>,
28    /// Natural language description of the condition
29    ///
30    /// A brief, natural language description of the condition that effectively
31    /// communicates the intended semantics.
32    pub description: Option<super::primitives::String>,
33    /// Short name assigned to expression for reuse
34    ///
35    /// A short name assigned to the expression to allow for multiple reuse of the
36    /// expression in the context where it is defined.
37    pub name: Option<super::primitives::Code>,
38    /// text/cql | text/fhirpath | application/x-fhir-query | etc.
39    ///
40    /// The media type of the language for the expression.
41    pub language: Option<super::primitives::Code>,
42    /// Expression in specified language
43    ///
44    /// An expression in the specified language that returns a value.
45    pub expression: Option<super::primitives::String>,
46    /// Where the expression is found
47    ///
48    /// A URI that defines where the expression is found.
49    pub reference: Option<super::primitives::Uri>,
50}
51
52impl super::super::codec::Json for Expression {
53    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
54        let mut object = super::super::codec::Object::new();
55        if let Some(v) = &self.id {
56            object.insert(
57                std::string::String::from("id"),
58                super::super::codec::Value::String(v.clone()),
59            );
60        }
61        if !self.extension.is_empty() {
62            let mut items = Vec::with_capacity(self.extension.len());
63            for item in &self.extension {
64                items.push(super::super::codec::Value::Object(
65                    super::super::codec::Json::to_json(item)?,
66                ));
67            }
68            object.insert(
69                std::string::String::from("extension"),
70                super::super::codec::Value::Array(items),
71            );
72        }
73        if let Some(item) = &self.description {
74            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
75                object.insert(std::string::String::from("description"), v);
76            }
77            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
78                object.insert(std::string::String::from("_description"), e);
79            }
80        }
81        if let Some(item) = &self.name {
82            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
83                object.insert(std::string::String::from("name"), v);
84            }
85            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
86                object.insert(std::string::String::from("_name"), e);
87            }
88        }
89        if let Some(item) = &self.language {
90            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
91                object.insert(std::string::String::from("language"), v);
92            }
93            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
94                object.insert(std::string::String::from("_language"), e);
95            }
96        }
97        if let Some(item) = &self.expression {
98            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
99                object.insert(std::string::String::from("expression"), v);
100            }
101            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
102                object.insert(std::string::String::from("_expression"), e);
103            }
104        }
105        if let Some(item) = &self.reference {
106            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
107                object.insert(std::string::String::from("reference"), v);
108            }
109            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
110                object.insert(std::string::String::from("_reference"), e);
111            }
112        }
113        Ok(object)
114    }
115
116    fn from_json(
117        object: &super::super::codec::Object,
118        path: &mut super::super::codec::Path,
119    ) -> Result<Self, super::super::codec::DecodeError> {
120        let mut raw_id: Option<&super::super::codec::Value> = None;
121        let mut raw_extension: Option<&super::super::codec::Value> = None;
122        let mut raw_description: Option<&super::super::codec::Value> = None;
123        let mut raw_description_element: Option<&super::super::codec::Value> = None;
124        let mut raw_name: Option<&super::super::codec::Value> = None;
125        let mut raw_name_element: Option<&super::super::codec::Value> = None;
126        let mut raw_language: Option<&super::super::codec::Value> = None;
127        let mut raw_language_element: Option<&super::super::codec::Value> = None;
128        let mut raw_expression: Option<&super::super::codec::Value> = None;
129        let mut raw_expression_element: Option<&super::super::codec::Value> = None;
130        let mut raw_reference: Option<&super::super::codec::Value> = None;
131        let mut raw_reference_element: Option<&super::super::codec::Value> = None;
132        for (key, value) in object {
133            match key.as_str() {
134                "id" => {
135                    raw_id = Some(value);
136                }
137                "extension" => {
138                    raw_extension = Some(value);
139                }
140                "description" => {
141                    raw_description = Some(value);
142                }
143                "_description" => {
144                    raw_description_element = Some(value);
145                }
146                "name" => {
147                    raw_name = Some(value);
148                }
149                "_name" => {
150                    raw_name_element = Some(value);
151                }
152                "language" => {
153                    raw_language = Some(value);
154                }
155                "_language" => {
156                    raw_language_element = Some(value);
157                }
158                "expression" => {
159                    raw_expression = Some(value);
160                }
161                "_expression" => {
162                    raw_expression_element = Some(value);
163                }
164                "reference" => {
165                    raw_reference = Some(value);
166                }
167                "_reference" => {
168                    raw_reference_element = Some(value);
169                }
170                other => {
171                    return path.with(other, |path| {
172                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
173                    });
174                }
175            }
176        }
177        let field_id = raw_id
178            .map(|value| {
179                path.with("id", |path| {
180                    let value = super::super::codec::expect_single(value, path)?;
181                    super::primitives::checked_string(
182                        super::super::codec::expect_string(value, path)?,
183                        path,
184                    )
185                })
186            })
187            .transpose()?;
188        let mut field_extension = Vec::new();
189        if let Some(raw) = raw_extension {
190            for (index, value) in path
191                .with("extension", |path| {
192                    super::super::codec::expect_array(raw, path)
193                })?
194                .iter()
195                .enumerate()
196            {
197                field_extension.push(path.with_index("extension", index, |path| {
198                    super::super::codec::Json::from_json(
199                        super::super::codec::expect_object(value, path)?,
200                        path,
201                    )
202                })?);
203            }
204        }
205        let field_description = match (raw_description, raw_description_element) {
206            (None, None) => None,
207            (value, element) => Some(path.with("description", |path| {
208                super::super::codec::Primitive::from_json_parts(value, element, path)
209            })?),
210        };
211        let field_name = match (raw_name, raw_name_element) {
212            (None, None) => None,
213            (value, element) => Some(path.with("name", |path| {
214                super::super::codec::Primitive::from_json_parts(value, element, path)
215            })?),
216        };
217        let field_language = match (raw_language, raw_language_element) {
218            (None, None) => None,
219            (value, element) => Some(path.with("language", |path| {
220                super::super::codec::Primitive::from_json_parts(value, element, path)
221            })?),
222        };
223        let field_expression = match (raw_expression, raw_expression_element) {
224            (None, None) => None,
225            (value, element) => Some(path.with("expression", |path| {
226                super::super::codec::Primitive::from_json_parts(value, element, path)
227            })?),
228        };
229        let field_reference = match (raw_reference, raw_reference_element) {
230            (None, None) => None,
231            (value, element) => Some(path.with("reference", |path| {
232                super::super::codec::Primitive::from_json_parts(value, element, path)
233            })?),
234        };
235        Ok(Self {
236            id: field_id,
237            extension: field_extension,
238            description: field_description,
239            name: field_name,
240            language: field_language,
241            expression: field_expression,
242            reference: field_reference,
243        })
244    }
245}
246
247impl serde::Serialize for Expression {
248    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
249        let mut map = serde::Serializer::serialize_map(serializer, None)?;
250        if let Some(item) = &self.description {
251            super::super::codec::element_entry(&mut map, "_description", item)?;
252        }
253        if let Some(item) = &self.expression {
254            super::super::codec::element_entry(&mut map, "_expression", item)?;
255        }
256        if let Some(item) = &self.language {
257            super::super::codec::element_entry(&mut map, "_language", item)?;
258        }
259        if let Some(item) = &self.name {
260            super::super::codec::element_entry(&mut map, "_name", item)?;
261        }
262        if let Some(item) = &self.reference {
263            super::super::codec::element_entry(&mut map, "_reference", item)?;
264        }
265        if let Some(item) = &self.description {
266            super::super::codec::value_entry(&mut map, "description", item)?;
267        }
268        if let Some(item) = &self.expression {
269            super::super::codec::value_entry(&mut map, "expression", item)?;
270        }
271        if !self.extension.is_empty() {
272            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
273        }
274        if let Some(v) = &self.id {
275            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
276        }
277        if let Some(item) = &self.language {
278            super::super::codec::value_entry(&mut map, "language", item)?;
279        }
280        if let Some(item) = &self.name {
281            super::super::codec::value_entry(&mut map, "name", item)?;
282        }
283        if let Some(item) = &self.reference {
284            super::super::codec::value_entry(&mut map, "reference", item)?;
285        }
286        serde::ser::SerializeMap::end(map)
287    }
288}
289
290impl<'de> serde::Deserialize<'de> for Expression {
291    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
292        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
293        let mut path = super::super::codec::Path::root("Expression");
294        let object =
295            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
296        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
297    }
298}