use fhir_types::codec::Value;
use fhir_types::r4::primitives::Code;
use fhir_types::r4::primitives::Decimal;
use fhir_types::r4::primitives::PositiveInt;
use fhir_types::r4::timing::TimingRepeat;
use openehr_rm::v1_2::data_types::quantity::date_time::dv_duration::DvDuration;
use crate::engine::library::datum;
use crate::engine::library::decode;
use crate::engine::library::duration;
use crate::engine::library::duration::TimeUnit;
use crate::engine::library::encode;
use crate::engine::library::port_value;
use crate::engine::library::primitive;
use crate::engine::library::refused;
use crate::engine::traverse::functions::MappingCodeError;
use crate::engine::traverse::functions::Ports;
pub const NON_DAILY: &str = "timingNonDaily";
const NON_DAILY_PERIOD: &str = "items[at0002]";
pub const NON_DAILY_PORTS: &[&str] = &[NON_DAILY_PERIOD];
const DV_DURATION: &str = "DV_DURATION";
const NON_DAILY_UNITS: [TimeUnit; 3] = [TimeUnit::Week, TimeUnit::Month, TimeUnit::Year];
pub fn non_daily_to_openehr(element: &Value) -> Result<Ports, MappingCodeError> {
let code = NON_DAILY;
let repeat: TimingRepeat = decode(code, "Timing.repeat", element)?;
once_per_period(code, &repeat)?;
if repeat.period_max.is_some() {
return Err(refused(
code,
"`periodMax` states a range of intervals, and the archetype holds one",
));
}
let Some(ref period) = repeat.period else {
return Ok(Ports::new());
};
let amount = primitive(
code,
"period",
period.id.as_ref(),
&period.extension,
period.value.as_ref(),
)?;
let unit = repeat
.period_unit
.as_ref()
.map(|unit| {
primitive(
code,
"periodUnit",
unit.id.as_ref(),
&unit.extension,
unit.value.as_ref(),
)
})
.transpose()?
.ok_or_else(|| refused(code, "a period has a unit (R4 `tim-2`)"))?;
let unit = TimeUnit::from_ucum(unit)
.filter(|unit| NON_DAILY_UNITS.contains(unit))
.ok_or_else(|| {
refused(
code,
format!("`{unit}` is no unit of a non-daily period, which is `wk`, `mo` or `a`"),
)
})?;
let produced = duration::to_iso(code, amount, unit)?;
Ok(Ports::new().with(NON_DAILY_PERIOD, datum(DV_DURATION, &produced)))
}
pub fn non_daily_to_fhir(ports: &Ports) -> Result<Option<Value>, MappingCodeError> {
let code = NON_DAILY;
let Some(held) = port_value::<DvDuration>(code, ports, NON_DAILY_PERIOD)? else {
return Ok(None);
};
let (amount, unit) = duration::from_iso(code, &held)?;
if !NON_DAILY_UNITS.contains(&unit) {
return Err(refused(
code,
format!(
"`{}` is an interval of a day or less, which is a daily timing",
held.value
),
));
}
let repeat = TimingRepeat {
frequency: Some(PositiveInt::from(1)),
period: Some(Decimal::from(amount)),
period_unit: Some(Code::from(unit.ucum())),
..TimingRepeat::default()
};
encode(code, &repeat).map(Some)
}
fn once_per_period(code: &str, repeat: &TimingRepeat) -> Result<(), MappingCodeError> {
if repeat.frequency_max.is_some() {
return Err(refused(
code,
"`frequencyMax` states a range of events per period, and the archetype holds one \
interval",
));
}
let Some(ref frequency) = repeat.frequency else {
return Ok(());
};
if !frequency.extension.is_empty() || frequency.id.is_some() {
return Err(refused(
code,
"`frequency` carries an id or an extension, which the archetype has no place for",
));
}
if frequency.value != Some(1) {
return Err(refused(
code,
"the timing occurs more than once per period, and the archetype holds the interval \
between single events",
));
}
Ok(())
}