use fhir::r5::resources::{AllergyIntolerance, Bundle, Patient};
use fhir::r5::types;
use fhir::r5::types::reference::Reference;
fn main() {
let allergy: AllergyIntolerance = serde_json::from_value(serde_json::json!({
"resourceType": "AllergyIntolerance",
"id": "al-1",
"patient": { "reference": "Patient/pat-1" }
}))
.expect("valid AllergyIntolerance");
let patient_ref: &types::Reference<Patient> = &allergy.patient;
println!(
"AllergyIntolerance.patient -> {}",
patient_ref.reference.as_ref().map_or("?", |r| &r.0)
);
let bundle: Bundle = serde_json::from_value(serde_json::json!({
"resourceType": "Bundle",
"type": "collection",
"entry": [
{ "resource": { "resourceType": "Patient", "id": "pat-1",
"name": [{ "family": "Chalmers" }] } }
]
}))
.expect("valid Bundle");
let resolved = patient_ref.resolve(&bundle).expect("resolves");
println!(
"resolved to a {} named {}",
resolved["resourceType"].as_str().unwrap_or("?"),
resolved["name"][0]["family"].as_str().unwrap_or("?")
);
let wrong: Bundle = serde_json::from_value(serde_json::json!({
"resourceType": "Bundle",
"type": "collection",
"entry": [
{ "resource": { "resourceType": "Observation", "id": "pat-1", "status": "final",
"code": { "text": "not a patient" } } }
]
}))
.expect("valid Bundle");
assert!(patient_ref.resolve(&wrong).is_none());
println!("a matching id under the wrong resourceType is refused");
let any: Reference = serde_json::from_value(serde_json::json!({
"reference": "Patient/pat-1"
}))
.expect("valid Reference");
let typed: Reference<Patient> = any.cast();
assert!(typed.resolve(&bundle).is_some());
println!("cast: Reference<Any> -> Reference<Patient> (wire form unchanged)");
}