Skip to main content

fhir_types/r4/
reference.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//! 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    /// manageable, there is a strict set of governance applied to the definition
20    /// and use of extensions. Though any implementer can define an extension, there
21    /// is 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")
35    ///
36    /// The expected type of the target of the reference. If both Reference.type and
37    /// Reference.reference are populated and Reference.reference is a FHIR URL,
38    /// both SHALL be consistent. The type is the Canonical URL of Resource
39    /// Definition that is the type this reference refers to. References are URLs
40    /// that are relative to <http://hl7.org/fhir/StructureDefinition/> e.g.
41    /// "Patient" is a reference to
42    /// <http://hl7.org/fhir/StructureDefinition/Patient>. Absolute URLs are only
43    /// allowed for logical models (and can only be used in references in logical
44    /// models, not resources).
45    pub r#type: Option<super::primitives::Uri>,
46    /// Logical reference, when literal reference is not known
47    ///
48    /// An identifier for the target resource. This is used when there is no way to
49    /// reference the other resource directly, either because the entity it
50    /// represents is not available through a FHIR server, or because there is no
51    /// way for the author of the resource to convert a known identifier to an
52    /// actual location. There is no requirement that a Reference.identifier point
53    /// to something that is actually exposed as a FHIR instance, but it SHALL point
54    /// to a business concept that would be expected to be exposed as a FHIR
55    /// instance, and that instance would need to be of a FHIR resource type allowed
56    /// by the reference.
57    pub identifier: Option<Box<super::identifier::Identifier>>,
58    /// Text alternative for the resource
59    ///
60    /// Plain text narrative that identifies the resource in addition to the
61    /// resource reference.
62    pub display: Option<super::primitives::String>,
63}
64
65impl super::super::codec::Json for Reference {
66    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
67        let mut object = super::super::codec::Object::new();
68        if let Some(v) = &self.id {
69            object.insert(
70                std::string::String::from("id"),
71                super::super::codec::Value::String(v.clone()),
72            );
73        }
74        if !self.extension.is_empty() {
75            let mut items = Vec::with_capacity(self.extension.len());
76            for item in &self.extension {
77                items.push(super::super::codec::Value::Object(
78                    super::super::codec::Json::to_json(item)?,
79                ));
80            }
81            object.insert(
82                std::string::String::from("extension"),
83                super::super::codec::Value::Array(items),
84            );
85        }
86        if let Some(item) = &self.reference {
87            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
88                object.insert(std::string::String::from("reference"), v);
89            }
90            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
91                object.insert(std::string::String::from("_reference"), e);
92            }
93        }
94        if let Some(item) = &self.r#type {
95            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
96                object.insert(std::string::String::from("type"), v);
97            }
98            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
99                object.insert(std::string::String::from("_type"), e);
100            }
101        }
102        if let Some(item) = &self.identifier {
103            object.insert(
104                std::string::String::from("identifier"),
105                super::super::codec::Value::Object(super::super::codec::Json::to_json(
106                    item.as_ref(),
107                )?),
108            );
109        }
110        if let Some(item) = &self.display {
111            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
112                object.insert(std::string::String::from("display"), v);
113            }
114            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
115                object.insert(std::string::String::from("_display"), e);
116            }
117        }
118        Ok(object)
119    }
120
121    fn from_json(
122        object: &super::super::codec::Object,
123        path: &mut super::super::codec::Path,
124    ) -> Result<Self, super::super::codec::DecodeError> {
125        let mut raw_id: Option<&super::super::codec::Value> = None;
126        let mut raw_extension: Option<&super::super::codec::Value> = None;
127        let mut raw_reference: Option<&super::super::codec::Value> = None;
128        let mut raw_reference_element: Option<&super::super::codec::Value> = None;
129        let mut raw_type: Option<&super::super::codec::Value> = None;
130        let mut raw_type_element: Option<&super::super::codec::Value> = None;
131        let mut raw_identifier: Option<&super::super::codec::Value> = None;
132        let mut raw_display: Option<&super::super::codec::Value> = None;
133        let mut raw_display_element: Option<&super::super::codec::Value> = None;
134        for (key, value) in object {
135            match key.as_str() {
136                "id" => {
137                    raw_id = Some(value);
138                }
139                "extension" => {
140                    raw_extension = Some(value);
141                }
142                "reference" => {
143                    raw_reference = Some(value);
144                }
145                "_reference" => {
146                    raw_reference_element = Some(value);
147                }
148                "type" => {
149                    raw_type = Some(value);
150                }
151                "_type" => {
152                    raw_type_element = Some(value);
153                }
154                "identifier" => {
155                    raw_identifier = Some(value);
156                }
157                "display" => {
158                    raw_display = Some(value);
159                }
160                "_display" => {
161                    raw_display_element = Some(value);
162                }
163                other => {
164                    return path.with(other, |path| {
165                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
166                    });
167                }
168            }
169        }
170        let field_id = raw_id
171            .map(|value| {
172                path.with("id", |path| {
173                    let value = super::super::codec::expect_single(value, path)?;
174                    super::primitives::checked_string(
175                        super::super::codec::expect_string(value, path)?,
176                        path,
177                    )
178                })
179            })
180            .transpose()?;
181        let mut field_extension = Vec::new();
182        if let Some(raw) = raw_extension {
183            for (index, value) in path
184                .with("extension", |path| {
185                    super::super::codec::expect_array(raw, path)
186                })?
187                .iter()
188                .enumerate()
189            {
190                field_extension.push(path.with_index("extension", index, |path| {
191                    super::super::codec::Json::from_json(
192                        super::super::codec::expect_object(value, path)?,
193                        path,
194                    )
195                })?);
196            }
197        }
198        let field_reference = match (raw_reference, raw_reference_element) {
199            (None, None) => None,
200            (value, element) => Some(path.with("reference", |path| {
201                super::super::codec::Primitive::from_json_parts(value, element, path)
202            })?),
203        };
204        let field_type = match (raw_type, raw_type_element) {
205            (None, None) => None,
206            (value, element) => Some(path.with("type", |path| {
207                super::super::codec::Primitive::from_json_parts(value, element, path)
208            })?),
209        };
210        let field_identifier = raw_identifier
211            .map(|value| {
212                path.with("identifier", |path| {
213                    super::super::codec::Json::from_json(
214                        super::super::codec::expect_object(
215                            super::super::codec::expect_single(value, path)?,
216                            path,
217                        )?,
218                        path,
219                    )
220                    .map(Box::new)
221                })
222            })
223            .transpose()?;
224        let field_display = match (raw_display, raw_display_element) {
225            (None, None) => None,
226            (value, element) => Some(path.with("display", |path| {
227                super::super::codec::Primitive::from_json_parts(value, element, path)
228            })?),
229        };
230        Ok(Self {
231            id: field_id,
232            extension: field_extension,
233            reference: field_reference,
234            r#type: field_type,
235            identifier: field_identifier,
236            display: field_display,
237        })
238    }
239}
240
241impl serde::Serialize for Reference {
242    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
243        let mut map = serde::Serializer::serialize_map(serializer, None)?;
244        if let Some(item) = &self.display {
245            super::super::codec::element_entry(&mut map, "_display", item)?;
246        }
247        if let Some(item) = &self.reference {
248            super::super::codec::element_entry(&mut map, "_reference", item)?;
249        }
250        if let Some(item) = &self.r#type {
251            super::super::codec::element_entry(&mut map, "_type", item)?;
252        }
253        if let Some(item) = &self.display {
254            super::super::codec::value_entry(&mut map, "display", item)?;
255        }
256        if !self.extension.is_empty() {
257            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
258        }
259        if let Some(v) = &self.id {
260            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
261        }
262        if let Some(item) = &self.identifier {
263            serde::ser::SerializeMap::serialize_entry(&mut map, "identifier", item.as_ref())?;
264        }
265        if let Some(item) = &self.reference {
266            super::super::codec::value_entry(&mut map, "reference", item)?;
267        }
268        if let Some(item) = &self.r#type {
269            super::super::codec::value_entry(&mut map, "type", item)?;
270        }
271        serde::ser::SerializeMap::end(map)
272    }
273}
274
275impl<'de> serde::Deserialize<'de> for Reference {
276    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
277        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
278        let mut path = super::super::codec::Path::root("Reference");
279        let object =
280            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
281        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
282    }
283}