use fhir_types::r4::primitives::Boolean;
use openehr_rm::v1_2::data_types::basic::dv_boolean::DvBoolean;
use crate::engine::lens::Fallback;
use crate::engine::lens::Lens;
use crate::engine::lens::LensError;
use crate::model::ast::keyword::Direction;
pub const BOOLEAN_CELL: &str = "DV_BOOLEAN against boolean";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BooleanLens;
impl Lens<DvBoolean, Boolean> for BooleanLens {
fn cell(&self) -> &'static str {
BOOLEAN_CELL
}
fn get(&self, source: &DvBoolean) -> Result<Boolean, LensError> {
Ok(Boolean::from(source.value))
}
fn put(&self, view: &Boolean, _existing: Option<&DvBoolean>) -> Result<DvBoolean, LensError> {
let value = view.value.ok_or(LensError::Missing {
cell: BOOLEAN_CELL,
attribute: "DV_BOOLEAN.value",
direction: Direction::FhirToOpenehr,
})?;
Ok(DvBoolean { value })
}
fn fallbacks(&self, _view: &Boolean) -> Vec<Fallback> {
Vec::new()
}
}
#[cfg(test)]
mod tests {
use fhir_types::r4::primitives::Boolean;
use openehr_rm::v1_2::data_types::basic::dv_boolean::DvBoolean;
use super::BOOLEAN_CELL;
use super::BooleanLens;
use crate::engine::lens::Lens;
use crate::engine::lens::LensError;
#[test]
fn both_values_round_trip_both_ways() {
for value in [true, false] {
let fhir = BooleanLens
.get(&DvBoolean { value })
.expect("a boolean reads");
assert_eq!(fhir.value, Some(value));
assert_eq!(
BooleanLens.put(&fhir, None).expect("a boolean writes"),
DvBoolean { value }
);
}
}
#[test]
fn a_boolean_with_no_value_is_refused() {
assert!(matches!(
BooleanLens.put(&Boolean::default(), None),
Err(LensError::Missing {
cell: BOOLEAN_CELL,
..
})
));
}
}