fhirkit 0.1.0

FHIR R4 for Rust: typed models for every resource and datatype generated from the official specification, lossless JSON with path-precise validation, typed search queries, FHIRPath
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
// @generated by fhirkit-gen from hl7.fhir.r4.core 4.0.1. Do not edit.
//! The FHIR R4 `Observation` resource.

#![allow(
    unused_imports,
    clippy::large_enum_variant,
    clippy::too_many_lines,
    clippy::doc_markdown
)]

use crate::decode::{
    choice_key, required, required_list, resource_type, Choice, Decoder, FhirJson, Obj, ObjWriter,
};
use crate::json::Json;
use crate::primitive::{Decimal, Kind, Primitive};
use crate::r4::*;

/// Measurements and simple assertions made about a patient, device or other subject.
///
/// FHIR path: `Observation`.
#[cfg(feature = "clinical")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Observation {
    /// `id` (0..1): Logical id of this artifact
    pub id: Option<String>,
    /// `meta` (0..1): Metadata about the resource
    pub meta: Option<Meta>,
    /// `implicitRules` (0..1): A set of rules under which this content was created
    pub implicit_rules: Option<Primitive<String>>,
    /// `language` (0..1): Language of the resource content
    pub language: Option<Primitive<String>>,
    /// `text` (0..1): Text summary of the resource, for human interpretation
    pub text: Option<Narrative>,
    /// `contained` (0..*): Contained, inline Resources
    pub contained: Vec<Resource>,
    /// `extension` (0..*): Additional content defined by implementations
    pub extension: Vec<Extension>,
    /// `modifierExtension` (0..*): Extensions that cannot be ignored
    pub modifier_extension: Vec<Extension>,
    /// `identifier` (0..*): Business Identifier for observation
    pub identifier: Vec<Identifier>,
    /// `basedOn` (0..*): Fulfills plan, proposal or order
    pub based_on: Vec<Reference>,
    /// `partOf` (0..*): Part of referenced event
    pub part_of: Vec<Reference>,
    /// `status` (1..1): registered | preliminary | final | amended +
    pub status: Option<Primitive<String>>,
    /// `category` (0..*): Classification of type of observation
    pub category: Vec<CodeableConcept>,
    /// `code` (1..1): Type of observation (code / type)
    pub code: Option<CodeableConcept>,
    /// `subject` (0..1): Who and/or what the observation is about
    pub subject: Option<Reference>,
    /// `focus` (0..*): What the observation is about, when it is not about the subject of record
    pub focus: Vec<Reference>,
    /// `encounter` (0..1): Healthcare event during which this observation is made
    pub encounter: Option<Reference>,
    /// `effective[x]` (0..1): Clinically relevant time/time-period for observation
    pub effective: Option<ObservationEffective>,
    /// `issued` (0..1): Date/Time this version was made available
    pub issued: Option<Primitive<String>>,
    /// `performer` (0..*): Who is responsible for the observation
    pub performer: Vec<Reference>,
    /// `value[x]` (0..1): Actual result
    pub value: Option<ObservationValue>,
    /// `dataAbsentReason` (0..1): Why the result is missing
    pub data_absent_reason: Option<CodeableConcept>,
    /// `interpretation` (0..*): High, low, normal, etc.
    pub interpretation: Vec<CodeableConcept>,
    /// `note` (0..*): Comments about the observation
    pub note: Vec<Annotation>,
    /// `bodySite` (0..1): Observed body part
    pub body_site: Option<CodeableConcept>,
    /// `method` (0..1): How it was done
    pub method: Option<CodeableConcept>,
    /// `specimen` (0..1): Specimen used for this observation
    pub specimen: Option<Reference>,
    /// `device` (0..1): (Measurement) Device
    pub device: Option<Reference>,
    /// `referenceRange` (0..*): Provides guide for interpretation
    pub reference_range: Vec<ObservationReferenceRange>,
    /// `hasMember` (0..*): Related resource that belongs to the Observation group
    pub has_member: Vec<Reference>,
    /// `derivedFrom` (0..*): Related measurements the observation is made from
    pub derived_from: Vec<Reference>,
    /// `component` (0..*): Component results
    pub component: Vec<ObservationComponent>,
}

#[cfg(feature = "clinical")]
impl FhirJson for Observation {
    const TYPE_NAME: &'static str = "Observation";

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        resource_type(&mut o, d, "Observation");
        let r = Observation {
            id: o.string(d, "id"),
            meta: o.one(d, "meta"),
            implicit_rules: o.prim(d, "implicitRules", Kind::Uri),
            language: o.prim(d, "language", Kind::Code),
            text: o.one(d, "text"),
            contained: o.list(d, "contained"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            identifier: o.list(d, "identifier"),
            based_on: o.list(d, "basedOn"),
            part_of: o.list(d, "partOf"),
            status: o.prim(d, "status", Kind::Code),
            category: o.list(d, "category"),
            code: o.one(d, "code"),
            subject: o.one(d, "subject"),
            focus: o.list(d, "focus"),
            encounter: o.one(d, "encounter"),
            effective: o.choice(d, "effective"),
            issued: o.prim(d, "issued", Kind::Instant),
            performer: o.list(d, "performer"),
            value: o.choice(d, "value"),
            data_absent_reason: o.one(d, "dataAbsentReason"),
            interpretation: o.list(d, "interpretation"),
            note: o.list(d, "note"),
            body_site: o.one(d, "bodySite"),
            method: o.one(d, "method"),
            specimen: o.one(d, "specimen"),
            device: o.one(d, "device"),
            reference_range: o.list(d, "referenceRange"),
            has_member: o.list(d, "hasMember"),
            derived_from: o.list(d, "derivedFrom"),
            component: o.list(d, "component"),
        };
        required(d, "status", &r.status);
        required(d, "code", &r.code);
        o.finish(d);
        Some(r)
    }

    fn encode(&self) -> Json {
        let mut w = ObjWriter::new();
        w.raw("resourceType", Json::String("Observation".into()));
        w.string("id", &self.id);
        w.one("meta", &self.meta);
        w.prim("implicitRules", &self.implicit_rules);
        w.prim("language", &self.language);
        w.one("text", &self.text);
        w.list("contained", &self.contained);
        w.list("extension", &self.extension);
        w.list("modifierExtension", &self.modifier_extension);
        w.list("identifier", &self.identifier);
        w.list("basedOn", &self.based_on);
        w.list("partOf", &self.part_of);
        w.prim("status", &self.status);
        w.list("category", &self.category);
        w.one("code", &self.code);
        w.one("subject", &self.subject);
        w.list("focus", &self.focus);
        w.one("encounter", &self.encounter);
        w.choice("effective", &self.effective);
        w.prim("issued", &self.issued);
        w.list("performer", &self.performer);
        w.choice("value", &self.value);
        w.one("dataAbsentReason", &self.data_absent_reason);
        w.list("interpretation", &self.interpretation);
        w.list("note", &self.note);
        w.one("bodySite", &self.body_site);
        w.one("method", &self.method);
        w.one("specimen", &self.specimen);
        w.one("device", &self.device);
        w.list("referenceRange", &self.reference_range);
        w.list("hasMember", &self.has_member);
        w.list("derivedFrom", &self.derived_from);
        w.list("component", &self.component);
        w.finish()
    }
}

/// Provides guide for interpretation
///
/// FHIR path: `Observation.referenceRange`.
#[cfg(feature = "clinical")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ObservationReferenceRange {
    /// `id` (0..1): Unique id for inter-element referencing
    pub id: Option<String>,
    /// `extension` (0..*): Additional content defined by implementations
    pub extension: Vec<Extension>,
    /// `modifierExtension` (0..*): Extensions that cannot be ignored even if unrecognized
    pub modifier_extension: Vec<Extension>,
    /// `low` (0..1): Low Range, if relevant
    pub low: Option<Quantity>,
    /// `high` (0..1): High Range, if relevant
    pub high: Option<Quantity>,
    /// `type` (0..1): Reference range qualifier
    pub type_: Option<CodeableConcept>,
    /// `appliesTo` (0..*): Reference range population
    pub applies_to: Vec<CodeableConcept>,
    /// `age` (0..1): Applicable age range, if relevant
    pub age: Option<Range>,
    /// `text` (0..1): Text based reference range in an observation
    pub text: Option<Primitive<String>>,
}

#[cfg(feature = "clinical")]
impl FhirJson for ObservationReferenceRange {
    const TYPE_NAME: &'static str = "ObservationReferenceRange";

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = ObservationReferenceRange {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            low: o.one(d, "low"),
            high: o.one(d, "high"),
            type_: o.one(d, "type"),
            applies_to: o.list(d, "appliesTo"),
            age: o.one(d, "age"),
            text: o.prim(d, "text", Kind::String),
        };
        o.finish(d);
        Some(r)
    }

    fn encode(&self) -> Json {
        let mut w = ObjWriter::new();
        w.string("id", &self.id);
        w.list("extension", &self.extension);
        w.list("modifierExtension", &self.modifier_extension);
        w.one("low", &self.low);
        w.one("high", &self.high);
        w.one("type", &self.type_);
        w.list("appliesTo", &self.applies_to);
        w.one("age", &self.age);
        w.prim("text", &self.text);
        w.finish()
    }
}

/// Component results
///
/// FHIR path: `Observation.component`.
#[cfg(feature = "clinical")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ObservationComponent {
    /// `id` (0..1): Unique id for inter-element referencing
    pub id: Option<String>,
    /// `extension` (0..*): Additional content defined by implementations
    pub extension: Vec<Extension>,
    /// `modifierExtension` (0..*): Extensions that cannot be ignored even if unrecognized
    pub modifier_extension: Vec<Extension>,
    /// `code` (1..1): Type of component observation (code / type)
    pub code: Option<CodeableConcept>,
    /// `value[x]` (0..1): Actual component result
    pub value: Option<ObservationComponentValue>,
    /// `dataAbsentReason` (0..1): Why the component result is missing
    pub data_absent_reason: Option<CodeableConcept>,
    /// `interpretation` (0..*): High, low, normal, etc.
    pub interpretation: Vec<CodeableConcept>,
    /// `referenceRange` (0..*): Provides guide for interpretation of component result
    pub reference_range: Vec<ObservationReferenceRange>,
}

#[cfg(feature = "clinical")]
impl FhirJson for ObservationComponent {
    const TYPE_NAME: &'static str = "ObservationComponent";

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = ObservationComponent {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            code: o.one(d, "code"),
            value: o.choice(d, "value"),
            data_absent_reason: o.one(d, "dataAbsentReason"),
            interpretation: o.list(d, "interpretation"),
            reference_range: o.list(d, "referenceRange"),
        };
        required(d, "code", &r.code);
        o.finish(d);
        Some(r)
    }

    fn encode(&self) -> Json {
        let mut w = ObjWriter::new();
        w.string("id", &self.id);
        w.list("extension", &self.extension);
        w.list("modifierExtension", &self.modifier_extension);
        w.one("code", &self.code);
        w.choice("value", &self.value);
        w.one("dataAbsentReason", &self.data_absent_reason);
        w.list("interpretation", &self.interpretation);
        w.list("referenceRange", &self.reference_range);
        w.finish()
    }
}

/// The types `Observation.effective[x]` allows.
#[cfg(feature = "clinical")]
#[derive(Debug, Clone, PartialEq)]
pub enum ObservationEffective {
    /// `dateTime`
    DateTime(Primitive<String>),
    /// `Period`
    Period(Period),
    /// `Timing`
    Timing(Timing),
    /// `instant`
    Instant(Primitive<String>),
}

#[cfg(feature = "clinical")]
impl Choice for ObservationEffective {
    const TYPES: &'static [&'static str] = &["dateTime", "Period", "Timing", "instant"];

    fn decode_as(code: &str, key: &str, o: &mut Obj<'_>, d: &mut Decoder) -> Option<Self> {
        match code {
            "dateTime" => o.prim(d, key, Kind::DateTime).map(Self::DateTime),
            "Period" => o.one(d, key).map(Self::Period),
            "Timing" => o.one(d, key).map(Self::Timing),
            "instant" => o.prim(d, key, Kind::Instant).map(Self::Instant),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::DateTime(v) => w.prim_value(&choice_key(base, "dateTime"), v),
            Self::Period(v) => w.value(&choice_key(base, "Period"), v),
            Self::Timing(v) => w.value(&choice_key(base, "Timing"), v),
            Self::Instant(v) => w.prim_value(&choice_key(base, "instant"), v),
        }
    }
}

/// The types `Observation.value[x]` allows.
#[cfg(feature = "clinical")]
#[derive(Debug, Clone, PartialEq)]
pub enum ObservationValue {
    /// `Quantity`
    Quantity(Quantity),
    /// `CodeableConcept`
    CodeableConcept(CodeableConcept),
    /// `string`
    String(Primitive<String>),
    /// `boolean`
    Boolean(Primitive<bool>),
    /// `integer`
    Integer(Primitive<i32>),
    /// `Range`
    Range(Range),
    /// `Ratio`
    Ratio(Ratio),
    /// `SampledData`
    SampledData(SampledData),
    /// `time`
    Time(Primitive<String>),
    /// `dateTime`
    DateTime(Primitive<String>),
    /// `Period`
    Period(Period),
}

#[cfg(feature = "clinical")]
impl Choice for ObservationValue {
    const TYPES: &'static [&'static str] = &[
        "Quantity",
        "CodeableConcept",
        "string",
        "boolean",
        "integer",
        "Range",
        "Ratio",
        "SampledData",
        "time",
        "dateTime",
        "Period",
    ];

    fn decode_as(code: &str, key: &str, o: &mut Obj<'_>, d: &mut Decoder) -> Option<Self> {
        match code {
            "Quantity" => o.one(d, key).map(Self::Quantity),
            "CodeableConcept" => o.one(d, key).map(Self::CodeableConcept),
            "string" => o.prim(d, key, Kind::String).map(Self::String),
            "boolean" => o.prim(d, key, Kind::Boolean).map(Self::Boolean),
            "integer" => o.prim(d, key, Kind::Integer).map(Self::Integer),
            "Range" => o.one(d, key).map(Self::Range),
            "Ratio" => o.one(d, key).map(Self::Ratio),
            "SampledData" => o.one(d, key).map(Self::SampledData),
            "time" => o.prim(d, key, Kind::Time).map(Self::Time),
            "dateTime" => o.prim(d, key, Kind::DateTime).map(Self::DateTime),
            "Period" => o.one(d, key).map(Self::Period),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::Quantity(v) => w.value(&choice_key(base, "Quantity"), v),
            Self::CodeableConcept(v) => w.value(&choice_key(base, "CodeableConcept"), v),
            Self::String(v) => w.prim_value(&choice_key(base, "string"), v),
            Self::Boolean(v) => w.prim_value(&choice_key(base, "boolean"), v),
            Self::Integer(v) => w.prim_value(&choice_key(base, "integer"), v),
            Self::Range(v) => w.value(&choice_key(base, "Range"), v),
            Self::Ratio(v) => w.value(&choice_key(base, "Ratio"), v),
            Self::SampledData(v) => w.value(&choice_key(base, "SampledData"), v),
            Self::Time(v) => w.prim_value(&choice_key(base, "time"), v),
            Self::DateTime(v) => w.prim_value(&choice_key(base, "dateTime"), v),
            Self::Period(v) => w.value(&choice_key(base, "Period"), v),
        }
    }
}

/// The types `Observation.component.value[x]` allows.
#[cfg(feature = "clinical")]
#[derive(Debug, Clone, PartialEq)]
pub enum ObservationComponentValue {
    /// `Quantity`
    Quantity(Quantity),
    /// `CodeableConcept`
    CodeableConcept(CodeableConcept),
    /// `string`
    String(Primitive<String>),
    /// `boolean`
    Boolean(Primitive<bool>),
    /// `integer`
    Integer(Primitive<i32>),
    /// `Range`
    Range(Range),
    /// `Ratio`
    Ratio(Ratio),
    /// `SampledData`
    SampledData(SampledData),
    /// `time`
    Time(Primitive<String>),
    /// `dateTime`
    DateTime(Primitive<String>),
    /// `Period`
    Period(Period),
}

#[cfg(feature = "clinical")]
impl Choice for ObservationComponentValue {
    const TYPES: &'static [&'static str] = &[
        "Quantity",
        "CodeableConcept",
        "string",
        "boolean",
        "integer",
        "Range",
        "Ratio",
        "SampledData",
        "time",
        "dateTime",
        "Period",
    ];

    fn decode_as(code: &str, key: &str, o: &mut Obj<'_>, d: &mut Decoder) -> Option<Self> {
        match code {
            "Quantity" => o.one(d, key).map(Self::Quantity),
            "CodeableConcept" => o.one(d, key).map(Self::CodeableConcept),
            "string" => o.prim(d, key, Kind::String).map(Self::String),
            "boolean" => o.prim(d, key, Kind::Boolean).map(Self::Boolean),
            "integer" => o.prim(d, key, Kind::Integer).map(Self::Integer),
            "Range" => o.one(d, key).map(Self::Range),
            "Ratio" => o.one(d, key).map(Self::Ratio),
            "SampledData" => o.one(d, key).map(Self::SampledData),
            "time" => o.prim(d, key, Kind::Time).map(Self::Time),
            "dateTime" => o.prim(d, key, Kind::DateTime).map(Self::DateTime),
            "Period" => o.one(d, key).map(Self::Period),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::Quantity(v) => w.value(&choice_key(base, "Quantity"), v),
            Self::CodeableConcept(v) => w.value(&choice_key(base, "CodeableConcept"), v),
            Self::String(v) => w.prim_value(&choice_key(base, "string"), v),
            Self::Boolean(v) => w.prim_value(&choice_key(base, "boolean"), v),
            Self::Integer(v) => w.prim_value(&choice_key(base, "integer"), v),
            Self::Range(v) => w.value(&choice_key(base, "Range"), v),
            Self::Ratio(v) => w.value(&choice_key(base, "Ratio"), v),
            Self::SampledData(v) => w.value(&choice_key(base, "SampledData"), v),
            Self::Time(v) => w.prim_value(&choice_key(base, "time"), v),
            Self::DateTime(v) => w.prim_value(&choice_key(base, "dateTime"), v),
            Self::Period(v) => w.value(&choice_key(base, "Period"), v),
        }
    }
}