use fhir_types::r4::duration::Duration;
use fhir_types::r4::primitives::Code;
use fhir_types::r4::primitives::Decimal;
use fhir_types::r4::primitives::Uri;
use openehr_base::v1_3::foundation_types::time::iso8601_duration::Iso8601Duration;
use openehr_base::v1_3::foundation_types::time::iso8601_duration_impl::DurationDesignator as Designator;
use openehr_rm::v1_2::data_types::quantity::date_time::dv_duration::DvDuration;
use crate::engine::lens::Fallback;
use crate::engine::lens::Lens;
use crate::engine::lens::LensError;
use crate::engine::lens::code_phrase::text_of;
use crate::engine::lens::number::decimal_text;
use crate::engine::lens::number::point;
use crate::engine::lens::quantity::UCUM;
use crate::model::ast::keyword::Direction;
pub const CELL: &str = "DV_DURATION against Duration";
const fn smaller(designator: Designator) -> Option<(Designator, u128)> {
match designator {
Designator::Years => Some((Designator::Months, 12)),
Designator::Weeks => Some((Designator::Days, 7)),
Designator::Days => Some((Designator::Hours, 24)),
Designator::Hours => Some((Designator::Minutes, 60)),
Designator::Minutes => Some((Designator::Seconds, 60)),
Designator::Months | Designator::Seconds => None,
}
}
pub const BRIDGE: [(&str, Designator, u32); 8] = [
("ms", Designator::Seconds, 3),
("s", Designator::Seconds, 0),
("min", Designator::Minutes, 0),
("h", Designator::Hours, 0),
("d", Designator::Days, 0),
("wk", Designator::Weeks, 0),
("mo", Designator::Months, 0),
("a", Designator::Years, 0),
];
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DurationLens;
impl Lens<DvDuration, Duration> for DurationLens {
fn cell(&self) -> &'static str {
CELL
}
fn get(&self, source: &DvDuration) -> Result<Duration, LensError> {
point(source.magnitude_status.as_deref(), CELL)?;
let (value, code) = reading(&source.value).ok_or_else(|| LensError::Shape {
cell: CELL,
attribute: "DV_DURATION.value",
found: source.value.clone(),
reason: "the duration has no single UCUM code of CommonUCUMCodesForDuration",
direction: Direction::OpenehrToFhir,
})?;
Ok(Duration {
value: Some(Decimal::from(value.as_str())),
system: Some(Uri::from(UCUM)),
code: Some(Code::from(code)),
..Duration::default()
})
}
fn put(&self, view: &Duration, existing: Option<&DvDuration>) -> Result<DvDuration, LensError> {
if let Some(comparator) = text_of(view.comparator.as_ref().map(|code| &code.value)) {
return Err(LensError::Shape {
cell: CELL,
attribute: "Duration.comparator",
found: String::from(comparator),
reason: "the table gives comparator no openEHR row",
direction: Direction::FhirToOpenehr,
});
}
let system = text_of(view.system.as_ref().map(|system| &system.value));
if let Some(other) = system.filter(|&system| system != UCUM) {
return Err(LensError::Shape {
cell: CELL,
attribute: "Duration.system",
found: String::from(other),
reason: "R4 drt-1 fixes a Duration's system at UCUM",
direction: Direction::FhirToOpenehr,
});
}
let code =
text_of(view.code.as_ref().map(|code| &code.value)).ok_or(LensError::Missing {
cell: CELL,
attribute: "Duration.code",
direction: Direction::FhirToOpenehr,
})?;
let &(code, component, shift) = BRIDGE
.iter()
.find(|&&(listed, _, _)| listed == code)
.ok_or_else(|| LensError::Shape {
cell: CELL,
attribute: "Duration.code",
found: String::from(code),
reason: "the code is outside the CommonUCUMCodesForDuration bridge",
direction: Direction::FhirToOpenehr,
})?;
let text = decimal_text(view.value.as_ref(), CELL, "DV_DURATION.value")?;
let amount = shifted(text, shift).ok_or_else(|| LensError::Shape {
cell: CELL,
attribute: "Duration.value",
found: String::from(text),
reason: "an ISO 8601 duration component is a non-negative decimal",
direction: Direction::FhirToOpenehr,
})?;
if component != Designator::Seconds && amount.contains('.') {
return Err(LensError::Shape {
cell: CELL,
attribute: "Duration.value",
found: String::from(text),
reason: "only the seconds of an openEHR duration carry a fraction",
direction: Direction::FhirToOpenehr,
});
}
let written = iso(&amount, component);
let value = existing
.map(|held| held.value.as_str())
.filter(|&held| reading(held).is_some_and(|(read, unit)| read == text && unit == code))
.map_or(written, String::from);
Ok(DvDuration {
normal_status: existing.and_then(|held| held.normal_status.clone()),
normal_range: existing.and_then(|held| held.normal_range.clone()),
other_reference_ranges: existing.and_then(|held| held.other_reference_ranges.clone()),
magnitude_status: existing.and_then(|held| held.magnitude_status.clone()),
accuracy: existing.and_then(|held| held.accuracy),
accuracy_is_percent: existing.and_then(|held| held.accuracy_is_percent),
value,
})
}
fn fallbacks(&self, view: &Duration) -> Vec<Fallback> {
let shifted = text_of(view.code.as_ref().map(|code| &code.value))
.and_then(|code| BRIDGE.iter().find(|&&(listed, _, _)| listed == code))
.is_some_and(|&(_, _, shift)| shift > 0);
if shifted {
return vec![Fallback::new(CELL, "DV_DURATION.value")];
}
Vec::new()
}
}
#[must_use]
pub fn reading(duration: &str) -> Option<(String, &'static str)> {
let duration = Iso8601Duration {
value: String::from(duration),
};
if duration.is_negative()? {
return None;
}
let parts = duration.parts()?;
let (smallest, larger) = parts.split_last()?;
let mut whole: u128 = 0;
let mut unit = parts.first()?.designator;
for part in larger {
whole = descend(whole, unit, part.designator)?.checked_add(part.text.parse().ok()?)?;
unit = part.designator;
}
whole = descend(whole, unit, smallest.designator)?;
let (integer, decimals) = match smallest.text.split_once(['.', ',']) {
Some((integer, decimals)) => (integer, Some(decimals)),
None => (smallest.text, None),
};
let total = whole.checked_add(integer.parse::<u128>().ok()?)?;
let mut value = total.to_string();
if let Some(decimals) = decimals {
value.push('.');
value.push_str(decimals);
}
let code = BRIDGE
.iter()
.find(|&&(_, designator, shift)| designator == smallest.designator && shift == 0)
.map(|&(code, _, _)| code)?;
Some((value, code))
}
fn descend(amount: u128, from: Designator, to: Designator) -> Option<u128> {
let mut unit = from;
let mut count = amount;
while unit != to {
let (next, factor) = smaller(unit)?;
count = count.checked_mul(factor)?;
unit = next;
}
Some(count)
}
fn is_amount(text: &str) -> bool {
let (integer, fraction) = match text.split_once('.') {
Some((integer, fraction)) => (integer, Some(fraction)),
None => (text, None),
};
!integer.is_empty()
&& integer.bytes().all(|byte| byte.is_ascii_digit())
&& fraction.is_none_or(|digits| {
!digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit())
})
}
fn shifted(text: &str, shift: u32) -> Option<String> {
if !is_amount(text) {
return None;
}
if shift == 0 {
return Some(String::from(text));
}
let (integer, fraction) = text.split_once('.').unwrap_or((text, ""));
let places = usize::try_from(shift).ok()?;
let padded = format!("{integer:0>width$}", width = places.checked_add(1)?);
let split = padded.len().checked_sub(places)?;
let (head, tail) = (padded.get(..split)?, padded.get(split..)?);
let head = head.trim_start_matches('0');
let head = if head.is_empty() { "0" } else { head };
let decimals = format!("{tail}{fraction}");
let decimals = decimals.trim_end_matches('0');
if decimals.is_empty() {
return Some(String::from(head));
}
Some(format!("{head}.{decimals}"))
}
fn iso(amount: &str, designator: Designator) -> String {
let time = if designator.is_time() { "T" } else { "" };
format!("P{time}{amount}{}", designator.letter())
}
#[cfg(test)]
mod tests {
use super::BRIDGE;
use super::DurationLens;
use super::reading;
use super::shifted;
use crate::engine::lens::Lens;
use crate::engine::lens::LensError;
use crate::engine::lens::quantity::UCUM;
use fhir_types::r4::duration::Duration;
use fhir_types::r4::primitives::Code;
use fhir_types::r4::primitives::Decimal;
use fhir_types::r4::primitives::Uri;
use openehr_rm::v1_2::data_types::quantity::date_time::dv_duration::DvDuration;
use proptest::prop_compose;
use proptest::proptest;
use proptest::sample::select;
fn duration(value: &str) -> DvDuration {
DvDuration {
normal_status: None,
normal_range: None,
other_reference_ranges: None,
magnitude_status: None,
accuracy: None,
accuracy_is_percent: None,
value: String::from(value),
}
}
fn fhir(value: &str, code: &str) -> Duration {
Duration {
value: Some(Decimal::from(value)),
system: Some(Uri::from(UCUM)),
code: Some(Code::from(code)),
..Duration::default()
}
}
#[test]
fn each_code_of_the_bridge_writes_its_designator() {
let expected = [
("ms", "1500", "PT1.5S"),
("s", "45", "PT45S"),
("min", "30", "PT30M"),
("h", "2", "PT2H"),
("d", "3", "P3D"),
("wk", "4", "P4W"),
("mo", "6", "P6M"),
("a", "1", "P1Y"),
];
assert_eq!(BRIDGE.len(), expected.len(), "one case per bridge entry");
for (code, value, iso) in expected {
let written = DurationLens
.put(&fhir(value, code), None)
.expect("the code is in the bridge");
assert_eq!(written.value, iso, "{value} {code}");
}
}
#[test]
fn each_designator_reads_as_its_code() {
for (iso, value, code) in [
("PT45S", "45", "s"),
("PT30M", "30", "min"),
("PT2H", "2", "h"),
("P3D", "3", "d"),
("P4W", "4", "wk"),
("P6M", "6", "mo"),
("P1Y", "1", "a"),
] {
assert_eq!(reading(iso), Some((String::from(value), code)), "{iso}");
}
}
#[test]
fn several_components_read_in_the_smallest() {
assert_eq!(reading("PT1H30M"), Some((String::from("90"), "min")));
assert_eq!(reading("P1DT2H"), Some((String::from("26"), "h")));
assert_eq!(reading("P1Y6M"), Some((String::from("18"), "mo")));
assert_eq!(reading("PT1M30.5S"), Some((String::from("90.5"), "s")));
}
#[test]
fn a_duration_no_single_code_states_is_refused() {
for iso in [
"P1Y2D",
"P1MT1H",
"PT1.5H30M",
"P",
"PT",
"1D",
"-P1D",
"P1H",
"PT1D",
] {
assert_eq!(reading(iso), None, "{iso}");
assert!(DurationLens.get(&duration(iso)).is_err(), "{iso}");
}
}
#[test]
fn a_fraction_below_the_seconds_is_refused_going_in() {
let error = DurationLens
.put(&fhir("1.5", "d"), None)
.expect_err("a day count carries no fraction");
assert!(
matches!(error, LensError::Shape { attribute, .. } if attribute == "Duration.value"),
"{error}"
);
assert_eq!(reading("P1.5D"), None);
assert_eq!(reading("PT1,5S"), Some((String::from("1.5"), "s")));
}
#[test]
fn a_code_outside_the_bridge_is_a_typed_refusal() {
let error = DurationLens
.put(&fhir("3", "mg"), None)
.expect_err("the bridge is closed");
assert!(
matches!(error, LensError::Shape { attribute, .. } if attribute == "Duration.code"),
"the refusal names the code: {error}"
);
}
#[test]
fn a_comparator_or_another_system_is_refused_going_in() {
let bounded = Duration {
comparator: Some(Code::from("<")),
..fhir("3", "d")
};
assert!(DurationLens.put(&bounded, None).is_err());
let foreign = Duration {
system: Some(Uri::from(
"http://example.org/fhir/CodeSystem/synthetic-units",
)),
..fhir("3", "d")
};
assert!(DurationLens.put(&foreign, None).is_err());
}
#[test]
fn milliseconds_are_a_one_way_row() {
assert_eq!(DurationLens.fallbacks(&fhir("500", "ms")).len(), 1);
assert_eq!(
DurationLens.fallbacks(&fhir("5", "s")),
Vec::<crate::engine::lens::Fallback>::new()
);
assert_eq!(shifted("500", 3).as_deref(), Some("0.5"));
assert_eq!(shifted("5", 3).as_deref(), Some("0.005"));
assert_eq!(shifted("1000", 3).as_deref(), Some("1"));
assert_eq!(shifted("1.5", 3).as_deref(), Some("0.0015"));
}
prop_compose! {
fn held()(
hours in 0u32..100,
minutes in 0u32..60,
shape in select(vec!["H", "HM", "M", "D", "DH", "W", "Y", "YM"]),
) -> DvDuration {
let value = match shape {
"H" => format!("PT{hours}H"),
"HM" => format!("PT{hours}H{minutes}M"),
"M" => format!("PT{minutes}M"),
"D" => format!("P{hours}D"),
"DH" => format!("P{hours}DT{minutes}H"),
"W" => format!("P{hours}W"),
"Y" => format!("P{hours}Y"),
_ => format!("P{hours}Y{minutes}M"),
};
duration(&value)
}
}
proptest! {
#[test]
fn get_put(source in held()) {
let view = DurationLens.get(&source).expect("one code states it");
let back = DurationLens.put(&view, Some(&source)).expect("the view is complete");
assert_eq!(back, source, "GetPut failed for {source:?}");
}
#[test]
fn put_get(source in held()) {
let view = DurationLens.get(&source).expect("one code states it");
let back = DurationLens.put(&view, None).expect("the view is complete");
let again = DurationLens.get(&back).expect("one code states it");
assert_eq!(again, view, "PutGet failed for {view:?}");
}
}
}