fhir-types 0.1.115

Generated per-version FHIR types and terminology operation contracts
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// @generated by fhir-codegen from hl7.fhir.r3.core 3.0.2. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: Apache-2.0
//! A digital Signature - XML DigSig, JWT, Graphical image of signature, etc.

/// A digital Signature - XML DigSig, JWT, Graphical image of signature, etc.
///
/// A digital signature along with supporting context. The signature may be
/// electronic/cryptographic in nature, or a graphical image representing a
/// hand-written signature, or a signature process. Different signature
/// approaches have different utilities.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature {
    /// xml:id (or equivalent in JSON)
    ///
    /// 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. In order to make the use of extensions safe
    /// and manageable, there is a strict set of governance applied to the
    /// definition and use of extensions. Though any implementer is allowed to
    /// 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>,
    /// Indication of the reason the entity signed the object(s)
    ///
    /// An indication of the reason that the entity signed this document. This may
    /// be explicitly included as part of the signature information and can be used
    /// when determining accountability for various actions concerning the document.
    pub r#type: Vec<super::coding::Coding>,
    /// When the signature was created
    ///
    /// When the digital signature was signed.
    pub when: super::primitives::Instant,
    /// Who signed
    ///
    /// A reference to an application-usable description of the identity that signed
    /// (e.g. the signature used their private key).
    pub who: SignatureWho,
    /// The party represented
    ///
    /// A reference to an application-usable description of the identity that is
    /// represented by the signature.
    pub on_behalf_of: Option<SignatureOnBehalfOf>,
    /// The technical format of the signature
    ///
    /// A mime type that indicates the technical format of the signature. Important
    /// mime types are application/signature+xml for X ML DigSig, application/jwt
    /// for JWT, and image/\* for a graphical image of a signature, etc.
    pub content_type: Option<super::primitives::Code>,
    /// The actual signature content (XML DigSig. JWT, picture, etc.)
    ///
    /// The base64 encoding of the Signature content. When signature is not recorded
    /// electronically this element would be empty.
    pub blob: Option<super::primitives::Base64Binary>,
}

impl super::super::codec::Json for Signature {
    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"),
                super::super::codec::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(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(item)?,
                ));
            }
            object.insert(
                std::string::String::from("extension"),
                super::super::codec::Value::Array(items),
            );
        }
        if !self.r#type.is_empty() {
            let mut items = Vec::with_capacity(self.r#type.len());
            for item in &self.r#type {
                items.push(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(item)?,
                ));
            }
            object.insert(
                std::string::String::from("type"),
                super::super::codec::Value::Array(items),
            );
        }
        if let Some(v) = super::super::codec::Primitive::value_json(&self.when)? {
            object.insert(std::string::String::from("when"), v);
        }
        if let Some(e) = super::super::codec::Primitive::element_json(&self.when)? {
            object.insert(std::string::String::from("_when"), e);
        }
        let (suffix, value, element) = self.who.to_json_parts()?;
        if let Some(value) = value {
            object.insert(format!("who{suffix}"), value);
        }
        if let Some(element) = element {
            object.insert(format!("_who{suffix}"), element);
        }
        if let Some(item) = &self.on_behalf_of {
            let (suffix, value, element) = item.to_json_parts()?;
            if let Some(value) = value {
                object.insert(format!("onBehalfOf{suffix}"), value);
            }
            if let Some(element) = element {
                object.insert(format!("_onBehalfOf{suffix}"), element);
            }
        }
        if let Some(item) = &self.content_type {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("contentType"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_contentType"), e);
            }
        }
        if let Some(item) = &self.blob {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("blob"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_blob"), 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<&super::super::codec::Value> = None;
        let mut raw_extension: Option<&super::super::codec::Value> = None;
        let mut raw_type: Option<&super::super::codec::Value> = None;
        let mut raw_when: Option<&super::super::codec::Value> = None;
        let mut raw_when_element: Option<&super::super::codec::Value> = None;
        let mut raw_who = super::super::codec::ChoiceSlot::default();
        let mut raw_on_behalf_of = super::super::codec::ChoiceSlot::default();
        let mut raw_content_type: Option<&super::super::codec::Value> = None;
        let mut raw_content_type_element: Option<&super::super::codec::Value> = None;
        let mut raw_blob: Option<&super::super::codec::Value> = None;
        let mut raw_blob_element: Option<&super::super::codec::Value> = None;
        for (key, value) in object {
            match key.as_str() {
                "id" => {
                    raw_id = Some(value);
                }
                "extension" => {
                    raw_extension = Some(value);
                }
                "type" => {
                    raw_type = Some(value);
                }
                "when" => {
                    raw_when = Some(value);
                }
                "_when" => {
                    raw_when_element = Some(value);
                }
                "whoUri" => {
                    raw_who.value("Uri", value, path)?;
                }
                "_whoUri" => {
                    raw_who.element("Uri", value, path)?;
                }
                "whoReference" => {
                    raw_who.value("Reference", value, path)?;
                }
                "_whoReference" => {
                    raw_who.element("Reference", value, path)?;
                }
                "onBehalfOfUri" => {
                    raw_on_behalf_of.value("Uri", value, path)?;
                }
                "_onBehalfOfUri" => {
                    raw_on_behalf_of.element("Uri", value, path)?;
                }
                "onBehalfOfReference" => {
                    raw_on_behalf_of.value("Reference", value, path)?;
                }
                "_onBehalfOfReference" => {
                    raw_on_behalf_of.element("Reference", value, path)?;
                }
                "contentType" => {
                    raw_content_type = Some(value);
                }
                "_contentType" => {
                    raw_content_type_element = Some(value);
                }
                "blob" => {
                    raw_blob = Some(value);
                }
                "_blob" => {
                    raw_blob_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 mut field_type = Vec::new();
        if let Some(raw) = raw_type {
            for (index, value) in super::super::codec::expect_array(raw, path)?
                .iter()
                .enumerate()
            {
                field_type.push(path.with_index("type", index, |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(value, path)?,
                        path,
                    )
                })?);
            }
        }
        let field_when = path.with("when", |path| {
            super::super::codec::Primitive::from_json_parts(raw_when, raw_when_element, path)
        })?;
        let field_who = path.with("who", |path| {
            let suffix = raw_who
                .suffix
                .ok_or_else(|| path.error(super::super::codec::DecodeErrorKind::MissingProperty))?;
            SignatureWho::from_json_parts(suffix, raw_who.value, raw_who.element, path)
        })?;
        let field_on_behalf_of = match raw_on_behalf_of.suffix {
            None => None,
            Some(suffix) => Some(path.with("onBehalfOf", |path| {
                SignatureOnBehalfOf::from_json_parts(
                    suffix,
                    raw_on_behalf_of.value,
                    raw_on_behalf_of.element,
                    path,
                )
            })?),
        };
        let field_content_type = match (raw_content_type, raw_content_type_element) {
            (None, None) => None,
            (value, element) => Some(path.with("contentType", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_blob = match (raw_blob, raw_blob_element) {
            (None, None) => None,
            (value, element) => Some(path.with("blob", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        Ok(Self {
            id: field_id,
            extension: field_extension,
            r#type: field_type,
            when: field_when,
            who: field_who,
            on_behalf_of: field_on_behalf_of,
            content_type: field_content_type,
            blob: field_blob,
        })
    }
}

impl serde::Serialize for Signature {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut map = serde::Serializer::serialize_map(serializer, None)?;
        if let Some(item) = &self.blob {
            super::super::codec::element_entry(&mut map, "_blob", item)?;
        }
        if let Some(item) = &self.content_type {
            super::super::codec::element_entry(&mut map, "_contentType", item)?;
        }
        if let Some(SignatureOnBehalfOf::Uri(inner)) = &self.on_behalf_of {
            super::super::codec::element_entry(&mut map, "_onBehalfOfUri", inner)?;
        }
        super::super::codec::element_entry(&mut map, "_when", &self.when)?;
        if let SignatureWho::Uri(inner) = &self.who {
            super::super::codec::element_entry(&mut map, "_whoUri", inner)?;
        }
        if let Some(item) = &self.blob {
            super::super::codec::value_entry(&mut map, "blob", item)?;
        }
        if let Some(item) = &self.content_type {
            super::super::codec::value_entry(&mut map, "contentType", item)?;
        }
        if !self.extension.is_empty() {
            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
        }
        if let Some(v) = &self.id {
            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
        }
        match &self.on_behalf_of {
            Some(SignatureOnBehalfOf::Reference(inner)) => {
                serde::ser::SerializeMap::serialize_entry(
                    &mut map,
                    "onBehalfOfReference",
                    inner.as_ref(),
                )?;
            }
            Some(SignatureOnBehalfOf::Uri(inner)) => {
                super::super::codec::value_entry(&mut map, "onBehalfOfUri", inner)?;
            }
            None => {}
        }
        if !self.r#type.is_empty() {
            serde::ser::SerializeMap::serialize_entry(&mut map, "type", &self.r#type)?;
        }
        super::super::codec::value_entry(&mut map, "when", &self.when)?;
        match &self.who {
            SignatureWho::Reference(inner) => {
                serde::ser::SerializeMap::serialize_entry(
                    &mut map,
                    "whoReference",
                    inner.as_ref(),
                )?;
            }
            SignatureWho::Uri(inner) => {
                super::super::codec::value_entry(&mut map, "whoUri", inner)?;
            }
        }
        serde::ser::SerializeMap::end(map)
    }
}

impl<'de> serde::Deserialize<'de> for Signature {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
        let mut path = super::super::codec::Path::root("Signature");
        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)
    }
}

/// The \`onBehalfOf\[x\]\` choice of \`Signature\`.
///
/// A reference to an application-usable description of the identity that is
/// represented by the signature.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SignatureOnBehalfOf {
    /// The `uri` form.
    Uri(super::primitives::Uri),
    /// The `Reference` form.
    Reference(Box<super::reference::Reference>),
}

impl SignatureOnBehalfOf {
    /// The key suffix, the value part, and the `_name` part of this form.
    ///
    /// # Errors
    ///
    /// Returns [`super::super::codec::EncodeError`] when a held value has no JSON form.
    pub fn to_json_parts(
        &self,
    ) -> Result<
        (
            &'static str,
            Option<super::super::codec::Value>,
            Option<super::super::codec::Value>,
        ),
        super::super::codec::EncodeError,
    > {
        match self {
            Self::Uri(inner) => Ok((
                "Uri",
                super::super::codec::Primitive::value_json(inner)?,
                super::super::codec::Primitive::element_json(inner)?,
            )),
            Self::Reference(inner) => Ok((
                "Reference",
                Some(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(inner.as_ref())?,
                )),
                None,
            )),
        }
    }

    /// Decodes the form named by `suffix` from its value and `_name` parts.
    ///
    /// # Errors
    ///
    /// Returns [`super::super::codec::DecodeError`] for an unknown suffix or a malformed part.
    pub fn from_json_parts(
        suffix: &str,
        value: Option<&super::super::codec::Value>,
        element: Option<&super::super::codec::Value>,
        path: &mut super::super::codec::Path,
    ) -> Result<Self, super::super::codec::DecodeError> {
        match suffix {
            "Uri" => Ok(Self::Uri(super::super::codec::Primitive::from_json_parts(
                value, element, path,
            )?)),
            "Reference" => {
                if element.is_some() {
                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
                        expected: "no underscore form for a complex type",
                    }));
                }
                let value = value.ok_or_else(|| {
                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
                })?;
                let inner = super::super::codec::Json::from_json(
                    super::super::codec::expect_object(
                        super::super::codec::expect_single(value, path)?,
                        path,
                    )?,
                    path,
                )?;
                Ok(Self::Reference(Box::new(inner)))
            }
            _ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
        }
    }
}

/// The \`who\[x\]\` choice of \`Signature\`.
///
/// A reference to an application-usable description of the identity that signed
/// (e.g. the signature used their private key).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SignatureWho {
    /// The `uri` form.
    Uri(super::primitives::Uri),
    /// The `Reference` form.
    Reference(Box<super::reference::Reference>),
}

impl SignatureWho {
    /// The key suffix, the value part, and the `_name` part of this form.
    ///
    /// # Errors
    ///
    /// Returns [`super::super::codec::EncodeError`] when a held value has no JSON form.
    pub fn to_json_parts(
        &self,
    ) -> Result<
        (
            &'static str,
            Option<super::super::codec::Value>,
            Option<super::super::codec::Value>,
        ),
        super::super::codec::EncodeError,
    > {
        match self {
            Self::Uri(inner) => Ok((
                "Uri",
                super::super::codec::Primitive::value_json(inner)?,
                super::super::codec::Primitive::element_json(inner)?,
            )),
            Self::Reference(inner) => Ok((
                "Reference",
                Some(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(inner.as_ref())?,
                )),
                None,
            )),
        }
    }

    /// Decodes the form named by `suffix` from its value and `_name` parts.
    ///
    /// # Errors
    ///
    /// Returns [`super::super::codec::DecodeError`] for an unknown suffix or a malformed part.
    pub fn from_json_parts(
        suffix: &str,
        value: Option<&super::super::codec::Value>,
        element: Option<&super::super::codec::Value>,
        path: &mut super::super::codec::Path,
    ) -> Result<Self, super::super::codec::DecodeError> {
        match suffix {
            "Uri" => Ok(Self::Uri(super::super::codec::Primitive::from_json_parts(
                value, element, path,
            )?)),
            "Reference" => {
                if element.is_some() {
                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
                        expected: "no underscore form for a complex type",
                    }));
                }
                let value = value.ok_or_else(|| {
                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
                })?;
                let inner = super::super::codec::Json::from_json(
                    super::super::codec::expect_object(
                        super::super::codec::expect_single(value, path)?,
                        path,
                    )?,
                    path,
                )?;
                Ok(Self::Reference(Box::new(inner)))
            }
            _ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
        }
    }
}