Skip to main content

fhir_types/r6/
reference.rs

1// @generated by fhir-codegen from hl7.fhir.r6.core 6.0.0-ballot5. 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 reference from one resource to another
6
7/// A reference from one resource to another.
8#[derive(Debug, Clone, Default, PartialEq, Eq)]
9pub struct Reference {
10    /// Unique id for inter-element referencing
11    ///
12    /// Unique id for the element within a resource (for internal references). This
13    /// may be any string value that does not contain spaces.
14    pub id: Option<std::string::String>,
15    /// Additional content defined by implementations
16    ///
17    /// May be used to represent additional information that is not part of the
18    /// basic definition of the element. To make the use of extensions safe and
19    /// managable, there is a strict set of governance applied to the definition and
20    /// use of extensions. Though any implementer can define an extension, there is
21    /// a set of requirements that SHALL be met as part of the definition of the
22    /// extension.
23    pub extension: Vec<super::extension::Extension>,
24    /// Literal reference, Relative, internal or absolute URL
25    ///
26    /// A reference to a location at which the other resource is found. The
27    /// reference may be a relative reference, in which case it is relative to the
28    /// service base URL, or an absolute URL that resolves to the location where the
29    /// resource is found. The reference may be version specific or not. If the
30    /// reference is not to a FHIR RESTful server, then it should be assumed to be
31    /// version specific. Internal fragment references (start with '\#') refer to
32    /// contained resources.
33    pub reference: Option<super::primitives::String>,
34    /// Type the reference refers to (e.g. "Patient") - must be a resource in
35    /// resources
36    ///
37    /// The expected type of the target of the reference. If both Reference.type and
38    /// Reference.reference are populated and Reference.reference is a FHIR URL,
39    /// both SHALL be consistent. The type is the Canonical URL of Resource
40    /// Definition that is the type this reference refers to. References are URLs
41    /// that are relative to <http://hl7.org/fhir/StructureDefinition/> e.g.
42    /// "Patient" is a reference to
43    /// <http://hl7.org/fhir/StructureDefinition/Patient>. Absolute URLs are only
44    /// allowed for logical models (and can only be used in references in logical
45    /// models, not resources).
46    pub r#type: Option<super::primitives::Uri>,
47    /// Logical reference, when literal reference is not known
48    ///
49    /// An identifier for the target resource. This is used when there is no way to
50    /// reference the other resource directly, either because the entity it
51    /// represents is not available through a FHIR server, or because there is no
52    /// way for the author of the resource to convert a known identifier to an
53    /// actual location. There is no requirement that a Reference.identifier point
54    /// to something that is actually exposed as a FHIR instance, but it SHALL point
55    /// to a business concept that would be expected to be exposed as a FHIR
56    /// instance, and that instance would need to be of a FHIR resource type allowed
57    /// by the reference.
58    pub identifier: Option<Box<super::identifier::Identifier>>,
59    /// Text alternative for the resource
60    ///
61    /// Plain text narrative that identifies the resource in addition to the
62    /// resource reference.
63    pub display: Option<super::primitives::String>,
64}
65
66impl super::super::codec::Json for Reference {
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        if let Some(item) = &self.reference {
88            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
89                object.insert(std::string::String::from("reference"), v);
90            }
91            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
92                object.insert(std::string::String::from("_reference"), e);
93            }
94        }
95        if let Some(item) = &self.r#type {
96            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
97                object.insert(std::string::String::from("type"), v);
98            }
99            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
100                object.insert(std::string::String::from("_type"), e);
101            }
102        }
103        if let Some(item) = &self.identifier {
104            object.insert(
105                std::string::String::from("identifier"),
106                super::super::codec::Value::Object(super::super::codec::Json::to_json(
107                    item.as_ref(),
108                )?),
109            );
110        }
111        if let Some(item) = &self.display {
112            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
113                object.insert(std::string::String::from("display"), v);
114            }
115            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
116                object.insert(std::string::String::from("_display"), e);
117            }
118        }
119        Ok(object)
120    }
121
122    fn from_json(
123        object: &super::super::codec::Object,
124        path: &mut super::super::codec::Path,
125    ) -> Result<Self, super::super::codec::DecodeError> {
126        let mut raw_id: Option<&super::super::codec::Value> = None;
127        let mut raw_extension: 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        let mut raw_type: Option<&super::super::codec::Value> = None;
131        let mut raw_type_element: Option<&super::super::codec::Value> = None;
132        let mut raw_identifier: Option<&super::super::codec::Value> = None;
133        let mut raw_display: Option<&super::super::codec::Value> = None;
134        let mut raw_display_element: Option<&super::super::codec::Value> = None;
135        for (key, value) in object {
136            match key.as_str() {
137                "id" => {
138                    raw_id = Some(value);
139                }
140                "extension" => {
141                    raw_extension = Some(value);
142                }
143                "reference" => {
144                    raw_reference = Some(value);
145                }
146                "_reference" => {
147                    raw_reference_element = Some(value);
148                }
149                "type" => {
150                    raw_type = Some(value);
151                }
152                "_type" => {
153                    raw_type_element = Some(value);
154                }
155                "identifier" => {
156                    raw_identifier = Some(value);
157                }
158                "display" => {
159                    raw_display = Some(value);
160                }
161                "_display" => {
162                    raw_display_element = Some(value);
163                }
164                other => {
165                    return path.with(other, |path| {
166                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
167                    });
168                }
169            }
170        }
171        let field_id = raw_id
172            .map(|value| {
173                path.with("id", |path| {
174                    let value = super::super::codec::expect_single(value, path)?;
175                    super::primitives::checked_string(
176                        super::super::codec::expect_string(value, path)?,
177                        path,
178                    )
179                })
180            })
181            .transpose()?;
182        let mut field_extension = Vec::new();
183        if let Some(raw) = raw_extension {
184            for (index, value) in path
185                .with("extension", |path| {
186                    super::super::codec::expect_array(raw, path)
187                })?
188                .iter()
189                .enumerate()
190            {
191                field_extension.push(path.with_index("extension", index, |path| {
192                    super::super::codec::Json::from_json(
193                        super::super::codec::expect_object(value, path)?,
194                        path,
195                    )
196                })?);
197            }
198        }
199        let field_reference = match (raw_reference, raw_reference_element) {
200            (None, None) => None,
201            (value, element) => Some(path.with("reference", |path| {
202                super::super::codec::Primitive::from_json_parts(value, element, path)
203            })?),
204        };
205        let field_type = match (raw_type, raw_type_element) {
206            (None, None) => None,
207            (value, element) => Some(path.with("type", |path| {
208                super::super::codec::Primitive::from_json_parts(value, element, path)
209            })?),
210        };
211        let field_identifier = raw_identifier
212            .map(|value| {
213                path.with("identifier", |path| {
214                    super::super::codec::Json::from_json(
215                        super::super::codec::expect_object(
216                            super::super::codec::expect_single(value, path)?,
217                            path,
218                        )?,
219                        path,
220                    )
221                    .map(Box::new)
222                })
223            })
224            .transpose()?;
225        let field_display = match (raw_display, raw_display_element) {
226            (None, None) => None,
227            (value, element) => Some(path.with("display", |path| {
228                super::super::codec::Primitive::from_json_parts(value, element, path)
229            })?),
230        };
231        Ok(Self {
232            id: field_id,
233            extension: field_extension,
234            reference: field_reference,
235            r#type: field_type,
236            identifier: field_identifier,
237            display: field_display,
238        })
239    }
240}
241
242impl serde::Serialize for Reference {
243    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
244        let mut map = serde::Serializer::serialize_map(serializer, None)?;
245        if let Some(item) = &self.display {
246            super::super::codec::element_entry(&mut map, "_display", item)?;
247        }
248        if let Some(item) = &self.reference {
249            super::super::codec::element_entry(&mut map, "_reference", item)?;
250        }
251        if let Some(item) = &self.r#type {
252            super::super::codec::element_entry(&mut map, "_type", item)?;
253        }
254        if let Some(item) = &self.display {
255            super::super::codec::value_entry(&mut map, "display", item)?;
256        }
257        if !self.extension.is_empty() {
258            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
259        }
260        if let Some(v) = &self.id {
261            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
262        }
263        if let Some(item) = &self.identifier {
264            serde::ser::SerializeMap::serialize_entry(&mut map, "identifier", item.as_ref())?;
265        }
266        if let Some(item) = &self.reference {
267            super::super::codec::value_entry(&mut map, "reference", item)?;
268        }
269        if let Some(item) = &self.r#type {
270            super::super::codec::value_entry(&mut map, "type", item)?;
271        }
272        serde::ser::SerializeMap::end(map)
273    }
274}
275
276impl<'de> serde::Deserialize<'de> for Reference {
277    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
278        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
279        let mut path = super::super::codec::Path::root("Reference");
280        let object =
281            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
282        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
283    }
284}