Skip to main content

fhir_types/r4b/
expression.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//! 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    /// manageable, there is a strict set of governance applied to the definition
24    /// and use of extensions. Though any implementer can define an extension, there
25    /// is 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::Id>,
38    /// text/cql | text/fhirpath | application/x-fhir-query | text/cql-identifier |
39    /// text/cql-expression | etc.
40    ///
41    /// The media type of the language for the expression.
42    pub language: super::primitives::Code,
43    /// Expression in specified language
44    ///
45    /// An expression in the specified language that returns a value.
46    pub expression: Option<super::primitives::String>,
47    /// Where the expression is found
48    ///
49    /// A URI that defines where the expression is found.
50    pub reference: Option<super::primitives::Uri>,
51}
52
53impl super::super::codec::Json for Expression {
54    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
55        let mut object = super::super::codec::Object::new();
56        if let Some(v) = &self.id {
57            object.insert(
58                std::string::String::from("id"),
59                super::super::codec::Value::String(v.clone()),
60            );
61        }
62        if !self.extension.is_empty() {
63            let mut items = Vec::with_capacity(self.extension.len());
64            for item in &self.extension {
65                items.push(super::super::codec::Value::Object(
66                    super::super::codec::Json::to_json(item)?,
67                ));
68            }
69            object.insert(
70                std::string::String::from("extension"),
71                super::super::codec::Value::Array(items),
72            );
73        }
74        if let Some(item) = &self.description {
75            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
76                object.insert(std::string::String::from("description"), v);
77            }
78            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
79                object.insert(std::string::String::from("_description"), e);
80            }
81        }
82        if let Some(item) = &self.name {
83            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
84                object.insert(std::string::String::from("name"), v);
85            }
86            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
87                object.insert(std::string::String::from("_name"), e);
88            }
89        }
90        if let Some(v) = super::super::codec::Primitive::value_json(&self.language)? {
91            object.insert(std::string::String::from("language"), v);
92        }
93        if let Some(e) = super::super::codec::Primitive::element_json(&self.language)? {
94            object.insert(std::string::String::from("_language"), e);
95        }
96        if let Some(item) = &self.expression {
97            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
98                object.insert(std::string::String::from("expression"), v);
99            }
100            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
101                object.insert(std::string::String::from("_expression"), e);
102            }
103        }
104        if let Some(item) = &self.reference {
105            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
106                object.insert(std::string::String::from("reference"), v);
107            }
108            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
109                object.insert(std::string::String::from("_reference"), e);
110            }
111        }
112        Ok(object)
113    }
114
115    fn from_json(
116        object: &super::super::codec::Object,
117        path: &mut super::super::codec::Path,
118    ) -> Result<Self, super::super::codec::DecodeError> {
119        let mut raw_id: Option<&super::super::codec::Value> = None;
120        let mut raw_extension: Option<&super::super::codec::Value> = None;
121        let mut raw_description: Option<&super::super::codec::Value> = None;
122        let mut raw_description_element: Option<&super::super::codec::Value> = None;
123        let mut raw_name: Option<&super::super::codec::Value> = None;
124        let mut raw_name_element: Option<&super::super::codec::Value> = None;
125        let mut raw_language: Option<&super::super::codec::Value> = None;
126        let mut raw_language_element: Option<&super::super::codec::Value> = None;
127        let mut raw_expression: Option<&super::super::codec::Value> = None;
128        let mut raw_expression_element: Option<&super::super::codec::Value> = None;
129        let mut raw_reference: Option<&super::super::codec::Value> = None;
130        let mut raw_reference_element: Option<&super::super::codec::Value> = None;
131        for (key, value) in object {
132            match key.as_str() {
133                "id" => {
134                    raw_id = Some(value);
135                }
136                "extension" => {
137                    raw_extension = Some(value);
138                }
139                "description" => {
140                    raw_description = Some(value);
141                }
142                "_description" => {
143                    raw_description_element = Some(value);
144                }
145                "name" => {
146                    raw_name = Some(value);
147                }
148                "_name" => {
149                    raw_name_element = Some(value);
150                }
151                "language" => {
152                    raw_language = Some(value);
153                }
154                "_language" => {
155                    raw_language_element = Some(value);
156                }
157                "expression" => {
158                    raw_expression = Some(value);
159                }
160                "_expression" => {
161                    raw_expression_element = Some(value);
162                }
163                "reference" => {
164                    raw_reference = Some(value);
165                }
166                "_reference" => {
167                    raw_reference_element = Some(value);
168                }
169                other => {
170                    return path.with(other, |path| {
171                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
172                    });
173                }
174            }
175        }
176        let field_id = raw_id
177            .map(|value| {
178                path.with("id", |path| {
179                    let value = super::super::codec::expect_single(value, path)?;
180                    super::primitives::checked_string(
181                        super::super::codec::expect_string(value, path)?,
182                        path,
183                    )
184                })
185            })
186            .transpose()?;
187        let mut field_extension = Vec::new();
188        if let Some(raw) = raw_extension {
189            for (index, value) in path
190                .with("extension", |path| {
191                    super::super::codec::expect_array(raw, path)
192                })?
193                .iter()
194                .enumerate()
195            {
196                field_extension.push(path.with_index("extension", index, |path| {
197                    super::super::codec::Json::from_json(
198                        super::super::codec::expect_object(value, path)?,
199                        path,
200                    )
201                })?);
202            }
203        }
204        let field_description = match (raw_description, raw_description_element) {
205            (None, None) => None,
206            (value, element) => Some(path.with("description", |path| {
207                super::super::codec::Primitive::from_json_parts(value, element, path)
208            })?),
209        };
210        let field_name = match (raw_name, raw_name_element) {
211            (None, None) => None,
212            (value, element) => Some(path.with("name", |path| {
213                super::super::codec::Primitive::from_json_parts(value, element, path)
214            })?),
215        };
216        let field_language = path.with("language", |path| {
217            super::super::codec::Primitive::from_json_parts(
218                raw_language,
219                raw_language_element,
220                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        super::super::codec::element_entry(&mut map, "_language", &self.language)?;
257        if let Some(item) = &self.name {
258            super::super::codec::element_entry(&mut map, "_name", item)?;
259        }
260        if let Some(item) = &self.reference {
261            super::super::codec::element_entry(&mut map, "_reference", item)?;
262        }
263        if let Some(item) = &self.description {
264            super::super::codec::value_entry(&mut map, "description", item)?;
265        }
266        if let Some(item) = &self.expression {
267            super::super::codec::value_entry(&mut map, "expression", item)?;
268        }
269        if !self.extension.is_empty() {
270            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
271        }
272        if let Some(v) = &self.id {
273            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
274        }
275        super::super::codec::value_entry(&mut map, "language", &self.language)?;
276        if let Some(item) = &self.name {
277            super::super::codec::value_entry(&mut map, "name", item)?;
278        }
279        if let Some(item) = &self.reference {
280            super::super::codec::value_entry(&mut map, "reference", item)?;
281        }
282        serde::ser::SerializeMap::end(map)
283    }
284}
285
286impl<'de> serde::Deserialize<'de> for Expression {
287    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
288        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
289        let mut path = super::super::codec::Path::root("Expression");
290        let object =
291            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
292        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
293    }
294}