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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
// @generated by fhirkit-gen from hl7.fhir.r4.core 4.0.1. Do not edit.
//! The FHIR R4 `SubstanceSpecification` 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::*;

/// The detailed description of a substance, typically at a level beyond what is used for prescribing.
///
/// FHIR path: `SubstanceSpecification`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecification {
    /// `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..1): Identifier by which this substance is known
    pub identifier: Option<Identifier>,
    /// `type` (0..1): High level categorization, e.g. polymer or nucleic acid
    pub type_: Option<CodeableConcept>,
    /// `status` (0..1): Status of substance within the catalogue e.g. approved
    pub status: Option<CodeableConcept>,
    /// `domain` (0..1): If the substance applies to only human or veterinary use
    pub domain: Option<CodeableConcept>,
    /// `description` (0..1): Textual description of the substance
    pub description: Option<Primitive<String>>,
    /// `source` (0..*): Supporting literature
    pub source: Vec<Reference>,
    /// `comment` (0..1): Textual comment about this record of a substance
    pub comment: Option<Primitive<String>>,
    /// `moiety` (0..*): Moiety, for structural modifications
    pub moiety: Vec<SubstanceSpecificationMoiety>,
    /// `property` (0..*): General specifications for this substance, including how it is related to other substances
    pub property: Vec<SubstanceSpecificationProperty>,
    /// `referenceInformation` (0..1): General information detailing this substance
    pub reference_information: Option<Reference>,
    /// `structure` (0..1): Structural information
    pub structure: Option<SubstanceSpecificationStructure>,
    /// `code` (0..*): Codes associated with the substance
    pub code: Vec<SubstanceSpecificationCode>,
    /// `name` (0..*): Names applicable to this substance
    pub name: Vec<SubstanceSpecificationName>,
    /// `molecularWeight` (0..*): The molecular weight or weight range (for proteins, polymers or nucleic acids)
    pub molecular_weight: Vec<SubstanceSpecificationStructureIsotopeMolecularWeight>,
    /// `relationship` (0..*): A link between this substance and another, with details of the relationship
    pub relationship: Vec<SubstanceSpecificationRelationship>,
    /// `nucleicAcid` (0..1): Data items specific to nucleic acids
    pub nucleic_acid: Option<Reference>,
    /// `polymer` (0..1): Data items specific to polymers
    pub polymer: Option<Reference>,
    /// `protein` (0..1): Data items specific to proteins
    pub protein: Option<Reference>,
    /// `sourceMaterial` (0..1): Material or taxonomic/anatomical source for the substance
    pub source_material: Option<Reference>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        resource_type(&mut o, d, "SubstanceSpecification");
        let r = SubstanceSpecification {
            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.one(d, "identifier"),
            type_: o.one(d, "type"),
            status: o.one(d, "status"),
            domain: o.one(d, "domain"),
            description: o.prim(d, "description", Kind::String),
            source: o.list(d, "source"),
            comment: o.prim(d, "comment", Kind::String),
            moiety: o.list(d, "moiety"),
            property: o.list(d, "property"),
            reference_information: o.one(d, "referenceInformation"),
            structure: o.one(d, "structure"),
            code: o.list(d, "code"),
            name: o.list(d, "name"),
            molecular_weight: o.list(d, "molecularWeight"),
            relationship: o.list(d, "relationship"),
            nucleic_acid: o.one(d, "nucleicAcid"),
            polymer: o.one(d, "polymer"),
            protein: o.one(d, "protein"),
            source_material: o.one(d, "sourceMaterial"),
        };
        o.finish(d);
        Some(r)
    }

    fn encode(&self) -> Json {
        let mut w = ObjWriter::new();
        w.raw(
            "resourceType",
            Json::String("SubstanceSpecification".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.one("identifier", &self.identifier);
        w.one("type", &self.type_);
        w.one("status", &self.status);
        w.one("domain", &self.domain);
        w.prim("description", &self.description);
        w.list("source", &self.source);
        w.prim("comment", &self.comment);
        w.list("moiety", &self.moiety);
        w.list("property", &self.property);
        w.one("referenceInformation", &self.reference_information);
        w.one("structure", &self.structure);
        w.list("code", &self.code);
        w.list("name", &self.name);
        w.list("molecularWeight", &self.molecular_weight);
        w.list("relationship", &self.relationship);
        w.one("nucleicAcid", &self.nucleic_acid);
        w.one("polymer", &self.polymer);
        w.one("protein", &self.protein);
        w.one("sourceMaterial", &self.source_material);
        w.finish()
    }
}

/// Moiety, for structural modifications
///
/// FHIR path: `SubstanceSpecification.moiety`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationMoiety {
    /// `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>,
    /// `role` (0..1): Role that the moiety is playing
    pub role: Option<CodeableConcept>,
    /// `identifier` (0..1): Identifier by which this moiety substance is known
    pub identifier: Option<Identifier>,
    /// `name` (0..1): Textual name for this moiety substance
    pub name: Option<Primitive<String>>,
    /// `stereochemistry` (0..1): Stereochemistry type
    pub stereochemistry: Option<CodeableConcept>,
    /// `opticalActivity` (0..1): Optical activity type
    pub optical_activity: Option<CodeableConcept>,
    /// `molecularFormula` (0..1): Molecular formula
    pub molecular_formula: Option<Primitive<String>>,
    /// `amount[x]` (0..1): Quantitative value for this moiety
    pub amount: Option<SubstanceSpecificationMoietyAmount>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationMoiety {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            role: o.one(d, "role"),
            identifier: o.one(d, "identifier"),
            name: o.prim(d, "name", Kind::String),
            stereochemistry: o.one(d, "stereochemistry"),
            optical_activity: o.one(d, "opticalActivity"),
            molecular_formula: o.prim(d, "molecularFormula", Kind::String),
            amount: o.choice(d, "amount"),
        };
        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("role", &self.role);
        w.one("identifier", &self.identifier);
        w.prim("name", &self.name);
        w.one("stereochemistry", &self.stereochemistry);
        w.one("opticalActivity", &self.optical_activity);
        w.prim("molecularFormula", &self.molecular_formula);
        w.choice("amount", &self.amount);
        w.finish()
    }
}

/// General specifications for this substance, including how it is related to other substances
///
/// FHIR path: `SubstanceSpecification.property`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationProperty {
    /// `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>,
    /// `category` (0..1): A category for this property, e.g. Physical, Chemical, Enzymatic
    pub category: Option<CodeableConcept>,
    /// `code` (0..1): Property type e.g. viscosity, pH, isoelectric point
    pub code: Option<CodeableConcept>,
    /// `parameters` (0..1): Parameters that were used in the measurement of a property (e.g. for viscosity: measured at 20C with a pH of 7.1)
    pub parameters: Option<Primitive<String>>,
    /// `definingSubstance[x]` (0..1): A substance upon which a defining property depends (e.g. for solubility: in water, in alcohol)
    pub defining_substance: Option<SubstanceSpecificationPropertyDefiningSubstance>,
    /// `amount[x]` (0..1): Quantitative value for this property
    pub amount: Option<SubstanceSpecificationPropertyAmount>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationProperty {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            category: o.one(d, "category"),
            code: o.one(d, "code"),
            parameters: o.prim(d, "parameters", Kind::String),
            defining_substance: o.choice(d, "definingSubstance"),
            amount: o.choice(d, "amount"),
        };
        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("category", &self.category);
        w.one("code", &self.code);
        w.prim("parameters", &self.parameters);
        w.choice("definingSubstance", &self.defining_substance);
        w.choice("amount", &self.amount);
        w.finish()
    }
}

/// Structural information
///
/// FHIR path: `SubstanceSpecification.structure`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationStructure {
    /// `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>,
    /// `stereochemistry` (0..1): Stereochemistry type
    pub stereochemistry: Option<CodeableConcept>,
    /// `opticalActivity` (0..1): Optical activity type
    pub optical_activity: Option<CodeableConcept>,
    /// `molecularFormula` (0..1): Molecular formula
    pub molecular_formula: Option<Primitive<String>>,
    /// `molecularFormulaByMoiety` (0..1): Specified per moiety according to the Hill system, i.e. first C, then H, then alphabetical, each moiety separated by a dot
    pub molecular_formula_by_moiety: Option<Primitive<String>>,
    /// `isotope` (0..*): Applicable for single substances that contain a radionuclide or a non-natural isotopic ratio
    pub isotope: Vec<SubstanceSpecificationStructureIsotope>,
    /// `molecularWeight` (0..1): The molecular weight or weight range (for proteins, polymers or nucleic acids)
    pub molecular_weight: Option<SubstanceSpecificationStructureIsotopeMolecularWeight>,
    /// `source` (0..*): Supporting literature
    pub source: Vec<Reference>,
    /// `representation` (0..*): Molecular structural representation
    pub representation: Vec<SubstanceSpecificationStructureRepresentation>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationStructure {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            stereochemistry: o.one(d, "stereochemistry"),
            optical_activity: o.one(d, "opticalActivity"),
            molecular_formula: o.prim(d, "molecularFormula", Kind::String),
            molecular_formula_by_moiety: o.prim(d, "molecularFormulaByMoiety", Kind::String),
            isotope: o.list(d, "isotope"),
            molecular_weight: o.one(d, "molecularWeight"),
            source: o.list(d, "source"),
            representation: o.list(d, "representation"),
        };
        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("stereochemistry", &self.stereochemistry);
        w.one("opticalActivity", &self.optical_activity);
        w.prim("molecularFormula", &self.molecular_formula);
        w.prim(
            "molecularFormulaByMoiety",
            &self.molecular_formula_by_moiety,
        );
        w.list("isotope", &self.isotope);
        w.one("molecularWeight", &self.molecular_weight);
        w.list("source", &self.source);
        w.list("representation", &self.representation);
        w.finish()
    }
}

/// Applicable for single substances that contain a radionuclide or a non-natural isotopic ratio
///
/// FHIR path: `SubstanceSpecification.structure.isotope`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationStructureIsotope {
    /// `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>,
    /// `identifier` (0..1): Substance identifier for each non-natural or radioisotope
    pub identifier: Option<Identifier>,
    /// `name` (0..1): Substance name for each non-natural or radioisotope
    pub name: Option<CodeableConcept>,
    /// `substitution` (0..1): The type of isotopic substitution present in a single substance
    pub substitution: Option<CodeableConcept>,
    /// `halfLife` (0..1): Half life - for a non-natural nuclide
    pub half_life: Option<Quantity>,
    /// `molecularWeight` (0..1): The molecular weight or weight range (for proteins, polymers or nucleic acids)
    pub molecular_weight: Option<SubstanceSpecificationStructureIsotopeMolecularWeight>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationStructureIsotope {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            identifier: o.one(d, "identifier"),
            name: o.one(d, "name"),
            substitution: o.one(d, "substitution"),
            half_life: o.one(d, "halfLife"),
            molecular_weight: o.one(d, "molecularWeight"),
        };
        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("identifier", &self.identifier);
        w.one("name", &self.name);
        w.one("substitution", &self.substitution);
        w.one("halfLife", &self.half_life);
        w.one("molecularWeight", &self.molecular_weight);
        w.finish()
    }
}

/// The molecular weight or weight range (for proteins, polymers or nucleic acids)
///
/// FHIR path: `SubstanceSpecification.structure.isotope.molecularWeight`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationStructureIsotopeMolecularWeight {
    /// `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>,
    /// `method` (0..1): The method by which the molecular weight was determined
    pub method: Option<CodeableConcept>,
    /// `type` (0..1): Type of molecular weight such as exact, average (also known as. number average), weight average
    pub type_: Option<CodeableConcept>,
    /// `amount` (0..1): Used to capture quantitative values for a variety of elements. If only limits are given, the arithmetic mean would be the average. If only a single definite value for a given element is given, it would be captured in this field
    pub amount: Option<Quantity>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationStructureIsotopeMolecularWeight {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            method: o.one(d, "method"),
            type_: o.one(d, "type"),
            amount: o.one(d, "amount"),
        };
        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("method", &self.method);
        w.one("type", &self.type_);
        w.one("amount", &self.amount);
        w.finish()
    }
}

/// Molecular structural representation
///
/// FHIR path: `SubstanceSpecification.structure.representation`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationStructureRepresentation {
    /// `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>,
    /// `type` (0..1): The type of structure (e.g. Full, Partial, Representative)
    pub type_: Option<CodeableConcept>,
    /// `representation` (0..1): The structural representation as text string in a format e.g. InChI, SMILES, MOLFILE, CDX
    pub representation: Option<Primitive<String>>,
    /// `attachment` (0..1): An attached file with the structural representation
    pub attachment: Option<Attachment>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationStructureRepresentation {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            type_: o.one(d, "type"),
            representation: o.prim(d, "representation", Kind::String),
            attachment: o.one(d, "attachment"),
        };
        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("type", &self.type_);
        w.prim("representation", &self.representation);
        w.one("attachment", &self.attachment);
        w.finish()
    }
}

/// Codes associated with the substance
///
/// FHIR path: `SubstanceSpecification.code`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationCode {
    /// `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` (0..1): The specific code
    pub code: Option<CodeableConcept>,
    /// `status` (0..1): Status of the code assignment
    pub status: Option<CodeableConcept>,
    /// `statusDate` (0..1): The date at which the code status is changed as part of the terminology maintenance
    pub status_date: Option<Primitive<String>>,
    /// `comment` (0..1): Any comment can be provided in this field, if necessary
    pub comment: Option<Primitive<String>>,
    /// `source` (0..*): Supporting literature
    pub source: Vec<Reference>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationCode {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            code: o.one(d, "code"),
            status: o.one(d, "status"),
            status_date: o.prim(d, "statusDate", Kind::DateTime),
            comment: o.prim(d, "comment", Kind::String),
            source: o.list(d, "source"),
        };
        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.one("status", &self.status);
        w.prim("statusDate", &self.status_date);
        w.prim("comment", &self.comment);
        w.list("source", &self.source);
        w.finish()
    }
}

/// Names applicable to this substance
///
/// FHIR path: `SubstanceSpecification.name`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationName {
    /// `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>,
    /// `name` (1..1): The actual name
    pub name: Option<Primitive<String>>,
    /// `type` (0..1): Name type
    pub type_: Option<CodeableConcept>,
    /// `status` (0..1): The status of the name
    pub status: Option<CodeableConcept>,
    /// `preferred` (0..1): If this is the preferred name for this substance
    pub preferred: Option<Primitive<bool>>,
    /// `language` (0..*): Language of the name
    pub language: Vec<CodeableConcept>,
    /// `domain` (0..*): The use context of this name for example if there is a different name a drug active ingredient as opposed to a food colour additive
    pub domain: Vec<CodeableConcept>,
    /// `jurisdiction` (0..*): The jurisdiction where this name applies
    pub jurisdiction: Vec<CodeableConcept>,
    /// `synonym` (0..*): A synonym of this name
    pub synonym: Vec<SubstanceSpecificationName>,
    /// `translation` (0..*): A translation for this name
    pub translation: Vec<SubstanceSpecificationName>,
    /// `official` (0..*): Details of the official nature of this name
    pub official: Vec<SubstanceSpecificationNameOfficial>,
    /// `source` (0..*): Supporting literature
    pub source: Vec<Reference>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationName {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            name: o.prim(d, "name", Kind::String),
            type_: o.one(d, "type"),
            status: o.one(d, "status"),
            preferred: o.prim(d, "preferred", Kind::Boolean),
            language: o.list(d, "language"),
            domain: o.list(d, "domain"),
            jurisdiction: o.list(d, "jurisdiction"),
            synonym: o.list(d, "synonym"),
            translation: o.list(d, "translation"),
            official: o.list(d, "official"),
            source: o.list(d, "source"),
        };
        required(d, "name", &r.name);
        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.prim("name", &self.name);
        w.one("type", &self.type_);
        w.one("status", &self.status);
        w.prim("preferred", &self.preferred);
        w.list("language", &self.language);
        w.list("domain", &self.domain);
        w.list("jurisdiction", &self.jurisdiction);
        w.list("synonym", &self.synonym);
        w.list("translation", &self.translation);
        w.list("official", &self.official);
        w.list("source", &self.source);
        w.finish()
    }
}

/// Details of the official nature of this name
///
/// FHIR path: `SubstanceSpecification.name.official`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationNameOfficial {
    /// `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>,
    /// `authority` (0..1): Which authority uses this official name
    pub authority: Option<CodeableConcept>,
    /// `status` (0..1): The status of the official name
    pub status: Option<CodeableConcept>,
    /// `date` (0..1): Date of official name change
    pub date: Option<Primitive<String>>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationNameOfficial {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            authority: o.one(d, "authority"),
            status: o.one(d, "status"),
            date: o.prim(d, "date", Kind::DateTime),
        };
        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("authority", &self.authority);
        w.one("status", &self.status);
        w.prim("date", &self.date);
        w.finish()
    }
}

/// A link between this substance and another, with details of the relationship
///
/// FHIR path: `SubstanceSpecification.relationship`.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct SubstanceSpecificationRelationship {
    /// `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>,
    /// `substance[x]` (0..1): A pointer to another substance, as a resource or just a representational code
    pub substance: Option<SubstanceSpecificationRelationshipSubstance>,
    /// `relationship` (0..1): For example "salt to parent", "active moiety", "starting material"
    pub relationship: Option<CodeableConcept>,
    /// `isDefining` (0..1): For example where an enzyme strongly bonds with a particular substance, this is a defining relationship for that enzyme, out of several possible substance relationships
    pub is_defining: Option<Primitive<bool>>,
    /// `amount[x]` (0..1): A numeric factor for the relationship, for instance to express that the salt of a substance has some percentage of the active substance in relation to some other
    pub amount: Option<SubstanceSpecificationRelationshipAmount>,
    /// `amountRatioLowLimit` (0..1): For use when the numeric
    pub amount_ratio_low_limit: Option<Ratio>,
    /// `amountType` (0..1): An operator for the amount, for example "average", "approximately", "less than"
    pub amount_type: Option<CodeableConcept>,
    /// `source` (0..*): Supporting literature
    pub source: Vec<Reference>,
}

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

    fn decode(v: &Json, d: &mut Decoder) -> Option<Self> {
        let mut o = Obj::new(v, d)?;
        let r = SubstanceSpecificationRelationship {
            id: o.string(d, "id"),
            extension: o.list(d, "extension"),
            modifier_extension: o.list(d, "modifierExtension"),
            substance: o.choice(d, "substance"),
            relationship: o.one(d, "relationship"),
            is_defining: o.prim(d, "isDefining", Kind::Boolean),
            amount: o.choice(d, "amount"),
            amount_ratio_low_limit: o.one(d, "amountRatioLowLimit"),
            amount_type: o.one(d, "amountType"),
            source: o.list(d, "source"),
        };
        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.choice("substance", &self.substance);
        w.one("relationship", &self.relationship);
        w.prim("isDefining", &self.is_defining);
        w.choice("amount", &self.amount);
        w.one("amountRatioLowLimit", &self.amount_ratio_low_limit);
        w.one("amountType", &self.amount_type);
        w.list("source", &self.source);
        w.finish()
    }
}

/// The types `SubstanceSpecification.moiety.amount[x]` allows.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq)]
pub enum SubstanceSpecificationMoietyAmount {
    /// `Quantity`
    Quantity(Quantity),
    /// `string`
    String(Primitive<String>),
}

#[cfg(feature = "specialized")]
impl Choice for SubstanceSpecificationMoietyAmount {
    const TYPES: &'static [&'static str] = &["Quantity", "string"];

    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),
            "string" => o.prim(d, key, Kind::String).map(Self::String),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::Quantity(v) => w.value(&choice_key(base, "Quantity"), v),
            Self::String(v) => w.prim_value(&choice_key(base, "string"), v),
        }
    }
}

/// The types `SubstanceSpecification.property.definingSubstance[x]` allows.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq)]
pub enum SubstanceSpecificationPropertyDefiningSubstance {
    /// `Reference`
    Reference(Reference),
    /// `CodeableConcept`
    CodeableConcept(CodeableConcept),
}

#[cfg(feature = "specialized")]
impl Choice for SubstanceSpecificationPropertyDefiningSubstance {
    const TYPES: &'static [&'static str] = &["Reference", "CodeableConcept"];

    fn decode_as(code: &str, key: &str, o: &mut Obj<'_>, d: &mut Decoder) -> Option<Self> {
        match code {
            "Reference" => o.one(d, key).map(Self::Reference),
            "CodeableConcept" => o.one(d, key).map(Self::CodeableConcept),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::Reference(v) => w.value(&choice_key(base, "Reference"), v),
            Self::CodeableConcept(v) => w.value(&choice_key(base, "CodeableConcept"), v),
        }
    }
}

/// The types `SubstanceSpecification.property.amount[x]` allows.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq)]
pub enum SubstanceSpecificationPropertyAmount {
    /// `Quantity`
    Quantity(Quantity),
    /// `string`
    String(Primitive<String>),
}

#[cfg(feature = "specialized")]
impl Choice for SubstanceSpecificationPropertyAmount {
    const TYPES: &'static [&'static str] = &["Quantity", "string"];

    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),
            "string" => o.prim(d, key, Kind::String).map(Self::String),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::Quantity(v) => w.value(&choice_key(base, "Quantity"), v),
            Self::String(v) => w.prim_value(&choice_key(base, "string"), v),
        }
    }
}

/// The types `SubstanceSpecification.relationship.substance[x]` allows.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq)]
pub enum SubstanceSpecificationRelationshipSubstance {
    /// `Reference`
    Reference(Reference),
    /// `CodeableConcept`
    CodeableConcept(CodeableConcept),
}

#[cfg(feature = "specialized")]
impl Choice for SubstanceSpecificationRelationshipSubstance {
    const TYPES: &'static [&'static str] = &["Reference", "CodeableConcept"];

    fn decode_as(code: &str, key: &str, o: &mut Obj<'_>, d: &mut Decoder) -> Option<Self> {
        match code {
            "Reference" => o.one(d, key).map(Self::Reference),
            "CodeableConcept" => o.one(d, key).map(Self::CodeableConcept),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::Reference(v) => w.value(&choice_key(base, "Reference"), v),
            Self::CodeableConcept(v) => w.value(&choice_key(base, "CodeableConcept"), v),
        }
    }
}

/// The types `SubstanceSpecification.relationship.amount[x]` allows.
#[cfg(feature = "specialized")]
#[derive(Debug, Clone, PartialEq)]
pub enum SubstanceSpecificationRelationshipAmount {
    /// `Quantity`
    Quantity(Quantity),
    /// `Range`
    Range(Range),
    /// `Ratio`
    Ratio(Ratio),
    /// `string`
    String(Primitive<String>),
}

#[cfg(feature = "specialized")]
impl Choice for SubstanceSpecificationRelationshipAmount {
    const TYPES: &'static [&'static str] = &["Quantity", "Range", "Ratio", "string"];

    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),
            "Range" => o.one(d, key).map(Self::Range),
            "Ratio" => o.one(d, key).map(Self::Ratio),
            "string" => o.prim(d, key, Kind::String).map(Self::String),
            _ => None,
        }
    }

    fn encode_into(&self, base: &str, w: &mut ObjWriter) {
        match self {
            Self::Quantity(v) => w.value(&choice_key(base, "Quantity"), v),
            Self::Range(v) => w.value(&choice_key(base, "Range"), v),
            Self::Ratio(v) => w.value(&choice_key(base, "Ratio"), v),
            Self::String(v) => w.prim_value(&choice_key(base, "string"), v),
        }
    }
}