Skip to main content

fhir_types/r4/
expression.rs

1// @generated by fhir-codegen from hl7.fhir.r4.core 4.0.1. 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 | etc.
39    ///
40    /// The media type of the language for the expression.
41    pub language: 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(v) = super::super::codec::Primitive::value_json(&self.language)? {
90            object.insert(std::string::String::from("language"), v);
91        }
92        if let Some(e) = super::super::codec::Primitive::element_json(&self.language)? {
93            object.insert(std::string::String::from("_language"), e);
94        }
95        if let Some(item) = &self.expression {
96            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
97                object.insert(std::string::String::from("expression"), v);
98            }
99            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
100                object.insert(std::string::String::from("_expression"), e);
101            }
102        }
103        if let Some(item) = &self.reference {
104            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
105                object.insert(std::string::String::from("reference"), v);
106            }
107            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
108                object.insert(std::string::String::from("_reference"), e);
109            }
110        }
111        Ok(object)
112    }
113
114    fn from_json(
115        object: &super::super::codec::Object,
116        path: &mut super::super::codec::Path,
117    ) -> Result<Self, super::super::codec::DecodeError> {
118        let mut raw_id: Option<&super::super::codec::Value> = None;
119        let mut raw_extension: Option<&super::super::codec::Value> = None;
120        let mut raw_description: Option<&super::super::codec::Value> = None;
121        let mut raw_description_element: Option<&super::super::codec::Value> = None;
122        let mut raw_name: Option<&super::super::codec::Value> = None;
123        let mut raw_name_element: Option<&super::super::codec::Value> = None;
124        let mut raw_language: Option<&super::super::codec::Value> = None;
125        let mut raw_language_element: Option<&super::super::codec::Value> = None;
126        let mut raw_expression: Option<&super::super::codec::Value> = None;
127        let mut raw_expression_element: Option<&super::super::codec::Value> = None;
128        let mut raw_reference: Option<&super::super::codec::Value> = None;
129        let mut raw_reference_element: Option<&super::super::codec::Value> = None;
130        for (key, value) in object {
131            match key.as_str() {
132                "id" => {
133                    raw_id = Some(value);
134                }
135                "extension" => {
136                    raw_extension = Some(value);
137                }
138                "description" => {
139                    raw_description = Some(value);
140                }
141                "_description" => {
142                    raw_description_element = Some(value);
143                }
144                "name" => {
145                    raw_name = Some(value);
146                }
147                "_name" => {
148                    raw_name_element = Some(value);
149                }
150                "language" => {
151                    raw_language = Some(value);
152                }
153                "_language" => {
154                    raw_language_element = Some(value);
155                }
156                "expression" => {
157                    raw_expression = Some(value);
158                }
159                "_expression" => {
160                    raw_expression_element = Some(value);
161                }
162                "reference" => {
163                    raw_reference = Some(value);
164                }
165                "_reference" => {
166                    raw_reference_element = Some(value);
167                }
168                other => {
169                    return path.with(other, |path| {
170                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
171                    });
172                }
173            }
174        }
175        let field_id = raw_id
176            .map(|value| {
177                path.with("id", |path| {
178                    let value = super::super::codec::expect_single(value, path)?;
179                    super::primitives::checked_string(
180                        super::super::codec::expect_string(value, path)?,
181                        path,
182                    )
183                })
184            })
185            .transpose()?;
186        let mut field_extension = Vec::new();
187        if let Some(raw) = raw_extension {
188            for (index, value) in path
189                .with("extension", |path| {
190                    super::super::codec::expect_array(raw, path)
191                })?
192                .iter()
193                .enumerate()
194            {
195                field_extension.push(path.with_index("extension", index, |path| {
196                    super::super::codec::Json::from_json(
197                        super::super::codec::expect_object(value, path)?,
198                        path,
199                    )
200                })?);
201            }
202        }
203        let field_description = match (raw_description, raw_description_element) {
204            (None, None) => None,
205            (value, element) => Some(path.with("description", |path| {
206                super::super::codec::Primitive::from_json_parts(value, element, path)
207            })?),
208        };
209        let field_name = match (raw_name, raw_name_element) {
210            (None, None) => None,
211            (value, element) => Some(path.with("name", |path| {
212                super::super::codec::Primitive::from_json_parts(value, element, path)
213            })?),
214        };
215        let field_language = path.with("language", |path| {
216            super::super::codec::Primitive::from_json_parts(
217                raw_language,
218                raw_language_element,
219                path,
220            )
221        })?;
222        let field_expression = match (raw_expression, raw_expression_element) {
223            (None, None) => None,
224            (value, element) => Some(path.with("expression", |path| {
225                super::super::codec::Primitive::from_json_parts(value, element, path)
226            })?),
227        };
228        let field_reference = match (raw_reference, raw_reference_element) {
229            (None, None) => None,
230            (value, element) => Some(path.with("reference", |path| {
231                super::super::codec::Primitive::from_json_parts(value, element, path)
232            })?),
233        };
234        Ok(Self {
235            id: field_id,
236            extension: field_extension,
237            description: field_description,
238            name: field_name,
239            language: field_language,
240            expression: field_expression,
241            reference: field_reference,
242        })
243    }
244}
245
246impl serde::Serialize for Expression {
247    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
248        let mut map = serde::Serializer::serialize_map(serializer, None)?;
249        if let Some(item) = &self.description {
250            super::super::codec::element_entry(&mut map, "_description", item)?;
251        }
252        if let Some(item) = &self.expression {
253            super::super::codec::element_entry(&mut map, "_expression", item)?;
254        }
255        super::super::codec::element_entry(&mut map, "_language", &self.language)?;
256        if let Some(item) = &self.name {
257            super::super::codec::element_entry(&mut map, "_name", item)?;
258        }
259        if let Some(item) = &self.reference {
260            super::super::codec::element_entry(&mut map, "_reference", item)?;
261        }
262        if let Some(item) = &self.description {
263            super::super::codec::value_entry(&mut map, "description", item)?;
264        }
265        if let Some(item) = &self.expression {
266            super::super::codec::value_entry(&mut map, "expression", item)?;
267        }
268        if !self.extension.is_empty() {
269            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
270        }
271        if let Some(v) = &self.id {
272            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
273        }
274        super::super::codec::value_entry(&mut map, "language", &self.language)?;
275        if let Some(item) = &self.name {
276            super::super::codec::value_entry(&mut map, "name", item)?;
277        }
278        if let Some(item) = &self.reference {
279            super::super::codec::value_entry(&mut map, "reference", item)?;
280        }
281        serde::ser::SerializeMap::end(map)
282    }
283}
284
285impl<'de> serde::Deserialize<'de> for Expression {
286    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
287        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
288        let mut path = super::super::codec::Path::root("Expression");
289        let object =
290            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
291        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
292    }
293}