code_systems — Use FHIR R5 code systems as type-safe Rust enums

Run with:

cargo run --example code_systems

Every FHIR CodeSystem is generated into an enum under fhir::r5::codes. Each variant serializes to its canonical FHIR code string, so you get compile-time checking and editor autocomplete instead of stringly-typed codes, while the wire format stays exactly as FHIR specifies.

The program#

use fhir::r5::codes::{AdministrativeGender, ObservationStatus};

fn main() {
    // Enum -> canonical code string.
    let gender = AdministrativeGender::Female;
    println!(
        "AdministrativeGender::Female => {}",
        serde_json::to_value(&gender).unwrap()
    );

    // Canonical code string -> enum.
    let status: ObservationStatus =
        serde_json::from_value("final".into()).expect("known observation status");
    println!("\"final\" => {status:?}");

    // Unknown codes fail to parse, catching typos at the boundary.
    let bad: Result<ObservationStatus, _> = serde_json::from_value("finel".into());
    println!("\"finel\" parses? {}", bad.is_ok());
}

Source: fhir/examples/code_systems.rs in the repository.

Edit this page on GitHub — the repository is the source of truth; this site renders it.