use fhir_types::r4::primitives::Time;
use openehr_base::v1_3::foundation_types::time::iso8601_time::Iso8601Time;
use openehr_rm::v1_2::data_types::quantity::date_time::dv_time::DvTime;
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::model::ast::keyword::Direction;
pub const CELL: &str = "DV_TIME against time";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct TimeLens;
impl Lens<DvTime, Time> for TimeLens {
fn cell(&self) -> &'static str {
CELL
}
fn get(&self, source: &DvTime) -> Result<Time, LensError> {
extended(&source.value)
.map(|text| Time::from(text.as_str()))
.ok_or_else(|| LensError::Shape {
cell: CELL,
attribute: "DV_TIME.value",
found: source.value.clone(),
reason: "an R4 time is hh:mm:ss with no zone",
direction: Direction::OpenehrToFhir,
})
}
fn put(&self, view: &Time, existing: Option<&DvTime>) -> Result<DvTime, LensError> {
let text = text_of(Some(&view.value)).ok_or(LensError::Missing {
cell: CELL,
attribute: "DV_TIME.value",
direction: Direction::FhirToOpenehr,
})?;
let value = existing
.map(|held| held.value.as_str())
.filter(|&held| extended(held).as_deref() == Some(text))
.unwrap_or(text);
Ok(DvTime {
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.clone()),
value: String::from(value),
})
}
fn fallbacks(&self, _view: &Time) -> Vec<Fallback> {
Vec::new()
}
}
fn extended(value: &str) -> Option<String> {
let time = Iso8601Time {
value: String::from(value),
};
if time.hour().is_none() || time.is_partial() || time.timezone().is_some() {
return None;
}
Some(time.as_string().replacen(',', ".", 1))
}
#[cfg(test)]
mod tests {
use super::TimeLens;
use super::extended;
use crate::engine::lens::Lens;
use fhir_types::r4::primitives::Time;
use openehr_rm::v1_2::data_types::quantity::date_time::dv_time::DvTime;
use proptest::prop_compose;
use proptest::proptest;
fn time(value: &str) -> DvTime {
DvTime {
normal_status: None,
normal_range: None,
other_reference_ranges: None,
magnitude_status: None,
accuracy: None,
value: String::from(value),
}
}
prop_compose! {
fn clock()(
hours in 0u8..24,
minutes in 0u8..60,
seconds in 0u8..60,
fraction in proptest::option::of(0u16..1000),
basic in proptest::bool::ANY,
status in proptest::option::of(proptest::sample::select(vec!["=", "~"])),
) -> DvTime {
let separator = if basic { "" } else { ":" };
let clock = format!("{hours:02}{separator}{minutes:02}{separator}{seconds:02}");
let value = match fraction {
Some(fraction) => format!("{clock}.{fraction:03}"),
None => clock,
};
DvTime { magnitude_status: status.map(String::from), ..time(&value) }
}
}
proptest! {
#[test]
fn get_put(source in clock()) {
let view = TimeLens.get(&source).expect("the time travels");
let back = TimeLens.put(&view, Some(&source)).expect("the view is complete");
assert_eq!(back, source, "GetPut failed for {source:?}");
}
#[test]
fn put_get(source in clock()) {
let view = TimeLens.get(&source).expect("the time travels");
let back = TimeLens.put(&view, None).expect("the view is complete");
let again = TimeLens.get(&back).expect("the time travels");
assert_eq!(again, view, "PutGet failed for {view:?}");
}
}
#[test]
fn the_basic_form_is_transformed_into_the_fhir_form() {
assert_eq!(extended("101530").as_deref(), Some("10:15:30"));
assert_eq!(extended("10:15:30.25").as_deref(), Some("10:15:30.25"));
}
#[test]
fn a_time_fhir_cannot_state_is_refused() {
for value in [
"10:15",
"10",
"10:15:30Z",
"10:15:30+01:00",
"25:00:00",
"10:15:30.",
] {
assert!(
TimeLens.get(&time(value)).is_err(),
"{value} has no R4 time"
);
}
}
#[test]
fn an_empty_time_is_refused_going_in() {
assert!(TimeLens.put(&Time::default(), None).is_err());
}
}