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
//! A coded value that is usually a known enum variant but tolerates any code.
//!
//! FHIR elements with a `required` binding draw their value from a value set, so
//! this crate types them as the matching release-specific `codes` enum
//! ([`r4::codes`](crate::r4::codes), [`r5::codes`](crate::r5::codes)). But real
//! data occasionally carries a code outside the set (a newer code, a local
//! extension, or simply invalid data), and a data-exchange library must not fail
//! to parse it. [`Coded<E>`] wraps the enum with an [`Unknown`](Coded::Unknown)
//! fallback so every wire value round-trips. See `spec/05-code-systems.md`.
//!
//! The wrapper itself is release-independent — it is generic over the enum — so
//! it is defined once here and re-exported as [`r4::coded`](crate::r4::coded)
//! and [`r5::coded`](crate::r5::coded).
//!
//! ```
//! use fhir::coded::Coded;
//!
//! // Stands in for a generated `codes` enum such as `AdministrativeGender`.
//! #[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
//! enum Gender {
//! #[serde(rename = "female")]
//! Female,
//! }
//!
//! // A known code parses into the enum variant.
//! let known: Coded<Gender> =
//! serde_json::from_value(serde_json::json!("female")).unwrap();
//! assert_eq!(known, Coded::Known(Gender::Female));
//!
//! // An unrecognized code is preserved as-is rather than rejected.
//! let unknown: Coded<Gender> =
//! serde_json::from_value(serde_json::json!("robot")).unwrap();
//! assert_eq!(unknown, Coded::Unknown("robot".to_string()));
//!
//! // Both round-trip to their original code string.
//! assert_eq!(serde_json::to_value(&known).unwrap(), "female");
//! assert_eq!(serde_json::to_value(&unknown).unwrap(), "robot");
//! ```
use ;
use crate;
/// A coded value: a known `codes` enum variant `E`, or any other code string
/// preserved verbatim.
///
/// Deserialization tries `E` first (untagged) and falls back to
/// [`Unknown`](Self::Unknown); serialization emits the underlying code string in
/// either case.