use fhir_types::r4::primitives::Decimal;
use crate::engine::lens::LensError;
use crate::engine::lens::code_phrase::text_of;
use crate::model::ast::keyword::Direction;
#[must_use]
pub(crate) fn render(value: f64, precision: Option<i32>) -> String {
match precision.and_then(|places| usize::try_from(places).ok()) {
Some(places) => format!("{value:.places$}"),
None => format!("{value}"),
}
}
pub(crate) fn exact(
value: f64,
precision: Option<i32>,
cell: &'static str,
attribute: &'static str,
) -> Result<String, LensError> {
let text = render(value, precision);
match text.parse::<f64>() {
Ok(read) if read.to_bits() == value.to_bits() => Ok(text),
_ => Err(LensError::Shape {
cell,
attribute,
found: render(value, None),
reason: "the magnitude has more decimal places than its precision declares",
direction: Direction::OpenehrToFhir,
}),
}
}
#[must_use]
pub(crate) fn places(text: &str) -> Option<i32> {
if text.contains(['e', 'E']) {
return None;
}
let Some((_whole, fraction)) = text.split_once('.') else {
return Some(0);
};
i32::try_from(fraction.chars().count()).ok()
}
#[must_use]
pub(crate) fn precision_of(
text: &str,
magnitude: f64,
held: bool,
held_precision: Option<i32>,
) -> Option<i32> {
if held && render(magnitude, held_precision) == text {
return held_precision;
}
places(text)
}
pub(crate) fn decimal_text<'view>(
value: Option<&'view Decimal>,
cell: &'static str,
attribute: &'static str,
) -> Result<&'view str, LensError> {
value
.and_then(|decimal| text_of(Some(&decimal.value)))
.ok_or(LensError::Missing {
cell,
attribute,
direction: Direction::FhirToOpenehr,
})
}
pub(crate) fn real(
text: &str,
cell: &'static str,
attribute: &'static str,
) -> Result<f64, LensError> {
text.parse::<f64>()
.ok()
.filter(|number| number.is_finite())
.ok_or_else(|| LensError::Shape {
cell,
attribute,
found: String::from(text),
reason: "the reference model takes a real number",
direction: Direction::FhirToOpenehr,
})
}
pub(crate) fn whole(
text: &str,
cell: &'static str,
attribute: &'static str,
) -> Result<i64, LensError> {
text.parse::<i64>().map_err(|_refused| LensError::Shape {
cell,
attribute,
found: String::from(text),
reason: "the reference model takes a whole number",
direction: Direction::FhirToOpenehr,
})
}
pub(crate) fn narrow(
value: i64,
cell: &'static str,
attribute: &'static str,
) -> Result<i32, LensError> {
i32::try_from(value).map_err(|_refused| LensError::Shape {
cell,
attribute,
found: value.to_string(),
reason: "a FHIR integer is a signed 32-bit number",
direction: Direction::OpenehrToFhir,
})
}
pub(crate) fn point(status: Option<&str>, cell: &'static str) -> Result<(), LensError> {
match status {
None | Some(POINT) => Ok(()),
Some(other) => Err(LensError::Shape {
cell,
attribute: "DV_QUANTIFIED.magnitude_status",
found: String::from(other),
reason: "the FHIR element states a point value and carries no comparator",
direction: Direction::OpenehrToFhir,
}),
}
}
pub(crate) const POINT: &str = "=";
#[cfg(test)]
mod tests {
use super::narrow;
use super::places;
use super::point;
use super::precision_of;
use super::render;
#[test]
fn a_precision_fixes_the_places_rendered() {
assert_eq!(render(1.5, Some(2)), "1.50");
assert_eq!(render(1.5, None), "1.5");
assert_eq!(render(1.5, Some(-1)), "1.5");
}
#[test]
fn the_places_are_read_from_the_text() {
assert_eq!(places("1.50"), Some(2));
assert_eq!(places("7"), Some(0));
assert_eq!(places("1.5e3"), None);
}
#[test]
fn a_held_precision_that_renders_the_text_is_kept() {
assert_eq!(precision_of("1.5", 1.5, true, None), None);
assert_eq!(precision_of("1.50", 1.5, true, None), Some(2));
assert_eq!(precision_of("1.5", 1.5, false, None), Some(1));
}
#[test]
fn an_integer64_outside_the_fhir_range_is_refused() {
assert_eq!(narrow(7, "cell", "attribute"), Ok(7));
assert!(narrow(i64::from(i32::MAX) + 1, "cell", "attribute").is_err());
}
#[test]
fn only_a_point_status_has_no_comparator() {
assert!(point(None, "cell").is_ok());
assert!(point(Some("="), "cell").is_ok());
assert!(point(Some("~"), "cell").is_err());
}
}