Expand description
The Coded<E> wrapper for required-binding coded fields.
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, 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
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
and 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");Enums§
- Coded
- A coded value: a known
codesenum variantE, or any other code string preserved verbatim.