object_wrapper! {
Ii wraps hl7_3::Ii,
expecting "an II object with \"root\" and \"extension\"",
derive [Eq, Default],
fields {
root: req String => "root",
extension: opt String => "extension",
}
}
object_wrapper! {
Cd wraps hl7_3::Cd,
expecting "a CD object with \"code\", \"codeSystem\", and \"displayName\"",
derive [Eq, Default],
fields {
code: req String => "code",
code_system: opt String => "codeSystem",
display_name: opt String => "displayName",
}
}
object_wrapper! {
Ivl wraps hl7_3::Ivl,
expecting "an IVL object with \"value\", \"low\", and \"high\"",
derive [Eq, Default],
fields {
value: opt String => "value",
low: opt String => "low",
high: opt String => "high",
}
}
object_wrapper! {
Pq wraps hl7_3::Pq,
expecting "a PQ object with \"value\" and \"unit\"",
derive [Eq, Default],
fields {
value: opt String => "value",
unit: opt String => "unit",
}
}
object_wrapper! {
Ed wraps hl7_3::Ed,
expecting "an ED object with \"mediaType\", \"representation\", and \"text\"",
derive [Eq, Default],
fields {
media_type: opt String => "mediaType",
representation: opt String => "representation",
text: opt String => "text",
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Strict;
#[test]
fn ii_round_trips_and_requires_root() {
let ii = Ii(hl7_3::Ii {
root: "1.2.3".into(),
extension: Some("7".into()),
});
let json = serde_json::to_string(&ii).unwrap();
assert_eq!(json, r#"{"root":"1.2.3","extension":"7"}"#);
let back: Ii = serde_json::from_str(&json).unwrap();
assert_eq!(back, ii);
let err = serde_json::from_str::<Ii>(r#"{"extension":"7"}"#).unwrap_err();
assert!(err.to_string().contains("root"), "{err}");
}
#[test]
fn cd_round_trips() {
let cd = Cd(hl7_3::Cd {
code: "OBS".into(),
code_system: Some("2.16.840.1.113883.5.6".into()),
display_name: None,
});
let back: Cd = serde_json::from_str(&serde_json::to_string(&cd).unwrap()).unwrap();
assert_eq!(back, cd);
}
#[test]
fn ivl_pq_and_ed_round_trip_and_default_to_empty() {
let ivl: Ivl = serde_json::from_str("{}").unwrap();
assert_eq!(ivl, Ivl::default());
let pq: Pq = serde_json::from_str(r#"{"value":"5"}"#).unwrap();
assert_eq!(pq.value.as_deref(), Some("5"));
assert_eq!(pq.unit, None);
let ed = Ed(hl7_3::Ed {
media_type: Some("application/pdf".into()),
representation: Some("B64".into()),
text: Some("AAAA".into()),
});
let back: Ed = serde_json::from_str(&serde_json::to_string(&ed).unwrap()).unwrap();
assert_eq!(back, ed);
}
#[test]
fn ignores_unknown_fields_but_strict_does_not() {
let json = r#"{"root":"1","exension":"x"}"#;
let plain: Ii = serde_json::from_str(json).unwrap();
assert_eq!(plain.extension, None, "the typo is silently absent");
let err = serde_json::from_str::<Strict<Ii>>(json).unwrap_err();
assert!(err.to_string().contains("exension"), "{err}");
}
#[test]
fn rejects_a_duplicate_key() {
let err = serde_json::from_str::<Cd>(r#"{"code":"A","code":"B"}"#).unwrap_err();
assert!(err.to_string().contains("duplicate"), "{err}");
}
}