Skip to main content

fhir_types/stu3/
reference.rs

1// @generated by fhir-codegen from hl7.fhir.r3.core 3.0.2. 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    /// xml:id (or equivalent in JSON)
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. In order to make the use of extensions safe
19    /// and manageable, there is a strict set of governance applied to the
20    /// definition and use of extensions. Though any implementer is allowed to
21    /// define an extension, there is a set of requirements that SHALL be met as
22    /// part of the definition of the 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    /// Logical reference, when literal reference is not known
35    ///
36    /// An identifier for the other resource. This is used when there is no way to
37    /// reference the other resource directly, either because the entity is not
38    /// available through a FHIR server, or because there is no way for the author
39    /// of the resource to convert a known identifier to an actual location. There
40    /// is no requirement that a Reference.identifier point to something that is
41    /// actually exposed as a FHIR instance, but it SHALL point to a business
42    /// concept that would be expected to be exposed as a FHIR instance, and that
43    /// instance would need to be of a FHIR resource type allowed by the reference.
44    pub identifier: Option<Box<super::identifier::Identifier>>,
45    /// Text alternative for the resource
46    ///
47    /// Plain text narrative that identifies the resource in addition to the
48    /// resource reference.
49    pub display: Option<super::primitives::String>,
50}
51
52impl super::super::codec::Json for Reference {
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.reference {
74            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
75                object.insert(std::string::String::from("reference"), v);
76            }
77            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
78                object.insert(std::string::String::from("_reference"), e);
79            }
80        }
81        if let Some(item) = &self.identifier {
82            object.insert(
83                std::string::String::from("identifier"),
84                super::super::codec::Value::Object(super::super::codec::Json::to_json(
85                    item.as_ref(),
86                )?),
87            );
88        }
89        if let Some(item) = &self.display {
90            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
91                object.insert(std::string::String::from("display"), v);
92            }
93            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
94                object.insert(std::string::String::from("_display"), e);
95            }
96        }
97        Ok(object)
98    }
99
100    fn from_json(
101        object: &super::super::codec::Object,
102        path: &mut super::super::codec::Path,
103    ) -> Result<Self, super::super::codec::DecodeError> {
104        let mut raw_id: Option<&super::super::codec::Value> = None;
105        let mut raw_extension: Option<&super::super::codec::Value> = None;
106        let mut raw_reference: Option<&super::super::codec::Value> = None;
107        let mut raw_reference_element: Option<&super::super::codec::Value> = None;
108        let mut raw_identifier: Option<&super::super::codec::Value> = None;
109        let mut raw_display: Option<&super::super::codec::Value> = None;
110        let mut raw_display_element: Option<&super::super::codec::Value> = None;
111        for (key, value) in object {
112            match key.as_str() {
113                "id" => {
114                    raw_id = Some(value);
115                }
116                "extension" => {
117                    raw_extension = Some(value);
118                }
119                "reference" => {
120                    raw_reference = Some(value);
121                }
122                "_reference" => {
123                    raw_reference_element = Some(value);
124                }
125                "identifier" => {
126                    raw_identifier = Some(value);
127                }
128                "display" => {
129                    raw_display = Some(value);
130                }
131                "_display" => {
132                    raw_display_element = Some(value);
133                }
134                other => {
135                    return path.with(other, |path| {
136                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
137                    });
138                }
139            }
140        }
141        let field_id = raw_id
142            .map(|value| {
143                path.with("id", |path| {
144                    let value = super::super::codec::expect_single(value, path)?;
145                    super::super::codec::expect_string(value, path)
146                })
147            })
148            .transpose()?;
149        let mut field_extension = Vec::new();
150        if let Some(raw) = raw_extension {
151            for (index, value) in path
152                .with("extension", |path| {
153                    super::super::codec::expect_array(raw, path)
154                })?
155                .iter()
156                .enumerate()
157            {
158                field_extension.push(path.with_index("extension", index, |path| {
159                    super::super::codec::Json::from_json(
160                        super::super::codec::expect_object(value, path)?,
161                        path,
162                    )
163                })?);
164            }
165        }
166        let field_reference = match (raw_reference, raw_reference_element) {
167            (None, None) => None,
168            (value, element) => Some(path.with("reference", |path| {
169                super::super::codec::Primitive::from_json_parts(value, element, path)
170            })?),
171        };
172        let field_identifier = raw_identifier
173            .map(|value| {
174                path.with("identifier", |path| {
175                    super::super::codec::Json::from_json(
176                        super::super::codec::expect_object(
177                            super::super::codec::expect_single(value, path)?,
178                            path,
179                        )?,
180                        path,
181                    )
182                    .map(Box::new)
183                })
184            })
185            .transpose()?;
186        let field_display = match (raw_display, raw_display_element) {
187            (None, None) => None,
188            (value, element) => Some(path.with("display", |path| {
189                super::super::codec::Primitive::from_json_parts(value, element, path)
190            })?),
191        };
192        Ok(Self {
193            id: field_id,
194            extension: field_extension,
195            reference: field_reference,
196            identifier: field_identifier,
197            display: field_display,
198        })
199    }
200}
201
202impl serde::Serialize for Reference {
203    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
204        let mut map = serde::Serializer::serialize_map(serializer, None)?;
205        if let Some(item) = &self.display {
206            super::super::codec::element_entry(&mut map, "_display", item)?;
207        }
208        if let Some(item) = &self.reference {
209            super::super::codec::element_entry(&mut map, "_reference", item)?;
210        }
211        if let Some(item) = &self.display {
212            super::super::codec::value_entry(&mut map, "display", item)?;
213        }
214        if !self.extension.is_empty() {
215            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
216        }
217        if let Some(v) = &self.id {
218            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
219        }
220        if let Some(item) = &self.identifier {
221            serde::ser::SerializeMap::serialize_entry(&mut map, "identifier", item.as_ref())?;
222        }
223        if let Some(item) = &self.reference {
224            super::super::codec::value_entry(&mut map, "reference", item)?;
225        }
226        serde::ser::SerializeMap::end(map)
227    }
228}
229
230impl<'de> serde::Deserialize<'de> for Reference {
231    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
232        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
233        let mut path = super::super::codec::Path::root("Reference");
234        let object =
235            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
236        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
237    }
238}