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