use fhir_types::r4::codeable_concept::CodeableConcept;
use fhir_types::r4::coding::Coding;
use fhir_types::r4::primitives;
use openehr_base::containers::NonEmptyVec;
use openehr_rm::v1_2::data_types::text::code_phrase::CodePhrase;
use openehr_rm::v1_2::data_types::text::dv_coded_text::DvCodedText;
use crate::engine::lens::Fallback;
use crate::engine::lens::Lens;
use crate::engine::lens::LensError;
use crate::engine::lens::code_phrase::CodePhraseLens;
use crate::engine::lens::code_phrase::text_of;
use crate::engine::lens::term_mapping::Withheld;
use crate::engine::lens::term_mapping::coding_of;
use crate::engine::lens::term_mapping::equivalents;
use crate::engine::lens::term_mapping::mappings_of;
use crate::engine::lens::term_mapping::uncarried;
use crate::engine::lens::term_mapping::withheld;
use crate::model::ast::keyword::Direction;
pub const CODEABLE_CONCEPT_CELL: &str = "DV_CODED_TEXT against CodeableConcept";
pub const CODING_CELL: &str = "DV_CODED_TEXT against Coding";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CodedTextLens {
binding: Option<String>,
}
impl CodedTextLens {
#[must_use]
pub const fn unbound() -> Self {
Self { binding: None }
}
#[must_use]
pub fn bound(system: impl Into<String>) -> Self {
Self {
binding: Some(system.into()),
}
}
#[must_use]
pub fn binding(&self) -> Option<&str> {
self.binding.as_deref()
}
fn defining(&self, codings: &[Coding]) -> Option<usize> {
let bound = self.binding.as_deref().and_then(|system| {
codings.iter().position(|coding| {
text_of(coding.system.as_ref().map(|uri| &uri.value)) == Some(system)
})
});
bound.or_else(|| codings.first().map(|_first| 0))
}
}
impl Lens<DvCodedText, CodeableConcept> for CodedTextLens {
fn cell(&self) -> &'static str {
CODEABLE_CONCEPT_CELL
}
fn get(&self, source: &DvCodedText) -> Result<CodeableConcept, LensError> {
let mut defining = CodePhraseLens.get(&source.defining_code)?;
defining.display = source
.defining_code
.preferred_term
.clone()
.map(primitives::String::from);
let mut codings = vec![defining];
for mapping in equivalents(source.mappings.iter().flatten()) {
codings.push(coding_of(mapping, CODEABLE_CONCEPT_CELL)?);
}
Ok(CodeableConcept {
coding: codings,
text: Some(primitives::String::from(source.value.clone())),
..CodeableConcept::default()
})
}
fn put(
&self,
view: &CodeableConcept,
existing: Option<&DvCodedText>,
) -> Result<DvCodedText, LensError> {
let absent = || LensError::Missing {
cell: CODEABLE_CONCEPT_CELL,
attribute: "DV_CODED_TEXT.defining_code",
direction: Direction::FhirToOpenehr,
};
let position = self.defining(&view.coding).ok_or_else(absent)?;
let defining = view.coding.get(position).ok_or_else(absent)?;
let held = existing.and_then(|value| value.mappings.as_deref());
let mappings = mappings_of(
view.coding
.iter()
.enumerate()
.filter(|&(index, _coding)| index != position)
.map(|(_index, coding)| coding),
held,
)?;
let value = value_of(
text_of(view.text.as_ref().map(|text| &text.value)),
defining,
CODEABLE_CONCEPT_CELL,
)?;
Ok(DvCodedText {
value: String::from(value),
hyperlink: existing.and_then(|held| held.hyperlink.clone()),
formatting: existing.and_then(|held| held.formatting.clone()),
mappings: NonEmptyVec::new(mappings).ok(),
language: existing.and_then(|held| held.language.clone()),
encoding: existing.and_then(|held| held.encoding.clone()),
defining_code: CodePhraseLens.put(defining, None)?,
})
}
fn fallbacks(&self, view: &CodeableConcept) -> Vec<Fallback> {
if text_of(view.text.as_ref().map(|text| &text.value)).is_some() {
return Vec::new();
}
vec![Fallback::new(CODEABLE_CONCEPT_CELL, "DV_TEXT.value")]
}
fn withheld(&self, source: &DvCodedText) -> Vec<Withheld> {
withheld(source.mappings.iter().flatten())
}
}
impl Lens<DvCodedText, Coding> for CodedTextLens {
fn cell(&self) -> &'static str {
CODING_CELL
}
fn get(&self, source: &DvCodedText) -> Result<Coding, LensError> {
let mut coding = CodePhraseLens.get(&source.defining_code)?;
coding.display = Some(primitives::String::from(source.value.clone()));
Ok(coding)
}
fn put(&self, view: &Coding, existing: Option<&DvCodedText>) -> Result<DvCodedText, LensError> {
let defining = CodePhraseLens.put(view, None)?;
let value = match text_of(view.display.as_ref().map(|display| &display.value)) {
Some(display) => String::from(display),
None => defining.code_string.clone(),
};
Ok(DvCodedText {
value,
hyperlink: existing.and_then(|held| held.hyperlink.clone()),
formatting: existing.and_then(|held| held.formatting.clone()),
mappings: existing.and_then(|held| held.mappings.clone()),
language: existing.and_then(|held| held.language.clone()),
encoding: existing.and_then(|held| held.encoding.clone()),
defining_code: CodePhrase {
preferred_term: existing.and_then(|held| held.defining_code.preferred_term.clone()),
..defining
},
})
}
fn fallbacks(&self, view: &Coding) -> Vec<Fallback> {
if text_of(view.display.as_ref().map(|display| &display.value)).is_some() {
return Vec::new();
}
vec![Fallback::new(CODING_CELL, "DV_TEXT.value")]
}
fn withheld(&self, source: &DvCodedText) -> Vec<Withheld> {
uncarried(source.mappings.iter().flatten())
}
}
fn value_of<'view>(
text: Option<&'view str>,
defining: &'view Coding,
cell: &'static str,
) -> Result<&'view str, LensError> {
text.or_else(|| text_of(defining.display.as_ref().map(|display| &display.value)))
.or_else(|| text_of(defining.code.as_ref().map(|code| &code.value)))
.ok_or(LensError::Missing {
cell,
attribute: "DV_TEXT.value",
direction: Direction::FhirToOpenehr,
})
}
#[cfg(test)]
pub(crate) mod tests {
use super::CODEABLE_CONCEPT_CELL;
use super::CodedTextLens;
use crate::engine::lens::Fallback;
use crate::engine::lens::Lens;
use crate::engine::lens::code_phrase::tests::code_phrase;
use crate::engine::lens::code_phrase::tests::coding;
use fhir_types::r4::codeable_concept::CodeableConcept;
use fhir_types::r4::coding::Coding;
use fhir_types::r4::primitives;
use fhir_types::r4::primitives::Code;
use fhir_types::r4::primitives::Uri;
use openehr_base::containers::NonEmptyVec;
use openehr_base::v1_3::base_types::identification::terminology_id::TerminologyId;
use openehr_rm::v1_2::data_types::text::code_phrase::CodePhrase;
use openehr_rm::v1_2::data_types::text::dv_coded_text::DvCodedText;
use openehr_rm::v1_2::data_types::text::term_mapping::TermMapping;
use proptest::collection::vec;
use proptest::prop_compose;
use proptest::proptest;
prop_compose! {
pub(crate) fn coded_text()(
value in "[A-Za-z ]{1,12}",
defining in code_phrase(),
targets in vec(code_phrase(), 0..3),
) -> DvCodedText {
DvCodedText {
value,
hyperlink: None,
formatting: None,
mappings: NonEmptyVec::new(
targets
.into_iter()
.map(|target| TermMapping { r#match: '=', purpose: None, target })
.collect::<Vec<TermMapping>>(),
)
.ok(),
language: None,
encoding: None,
defining_code: defining,
}
}
}
prop_compose! {
fn displayed_coding()(
system in "[a-z]{1,8}://[a-z.]{1,12}",
version in proptest::option::of("[0-9]{4}"),
code in "[A-Za-z0-9-]{1,10}",
display in "[A-Za-z ]{1,12}",
) -> Coding {
Coding {
system: Some(Uri::from(system)),
version: version.map(primitives::String::from),
code: Some(Code::from(code)),
display: Some(primitives::String::from(display)),
..Coding::default()
}
}
}
prop_compose! {
pub(crate) fn codeable_concept()(
codings in vec(coding(), 1..4),
text in "[A-Za-z ]{1,12}",
) -> CodeableConcept {
CodeableConcept {
coding: codings,
text: Some(primitives::String::from(text)),
..CodeableConcept::default()
}
}
}
proptest! {
#[test]
fn codeable_concept_get_put(source in coded_text()) {
let lens = CodedTextLens::unbound();
let view: CodeableConcept = lens.get(&source).expect("the generated subset is carried");
let back = lens.put(&view, Some(&source)).expect("the view is complete");
assert_eq!(back, source, "GetPut failed for {source:?}");
}
#[test]
fn codeable_concept_put_get(view in codeable_concept()) {
let lens = CodedTextLens::unbound();
let source: DvCodedText = lens.put(&view, None).expect("the generated subset is carried");
let back: CodeableConcept = lens.get(&source).expect("the value is complete");
assert_eq!(back, view, "PutGet failed for {view:?}");
}
#[test]
fn coding_get_put(source in coded_text()) {
let lens = CodedTextLens::unbound();
let view: Coding = lens.get(&source).expect("the generated subset is carried");
let back = lens.put(&view, Some(&source)).expect("the view is complete");
assert_eq!(back, source, "GetPut failed for {source:?}");
}
#[test]
fn coding_put_get(view in displayed_coding()) {
let lens = CodedTextLens::unbound();
let source: DvCodedText = lens.put(&view, None).expect("the generated subset is carried");
let back: Coding = lens.get(&source).expect("the value is complete");
assert_eq!(back, view, "PutGet failed for {view:?}");
}
}
fn code_of(system: &str, code: &str) -> Coding {
Coding {
system: Some(Uri::from(system)),
code: Some(Code::from(code)),
..Coding::default()
}
}
#[test]
fn the_bound_terminology_becomes_the_defining_code() {
let view = CodeableConcept {
coding: vec![
code_of("http://loinc.org", "LP74849-8"),
code_of("http://snomed.info/sct", "386661006"),
],
text: Some(primitives::String::from("Fever")),
..CodeableConcept::default()
};
let lens = CodedTextLens::bound("http://snomed.info/sct");
let source: DvCodedText = lens.put(&view, None).expect("two complete codings");
assert_eq!(
source.defining_code.code_string, "386661006",
"the coding of the bound terminology is the defining code"
);
assert_eq!(
source
.mappings
.as_deref()
.and_then(<[TermMapping]>::first)
.map(|mapping| mapping.target.code_string.clone()),
Some(String::from("LP74849-8")),
"every further coding becomes a term mapping"
);
}
#[test]
fn the_first_coding_wins_when_no_binding_matches() {
let view = CodeableConcept {
coding: vec![
code_of("http://loinc.org", "LP74849-8"),
code_of("http://snomed.info/sct", "386661006"),
],
text: Some(primitives::String::from("Fever")),
..CodeableConcept::default()
};
let lens = CodedTextLens::bound("http://example.org/other");
let source: DvCodedText = lens.put(&view, None).expect("two complete codings");
assert_eq!(
source.defining_code.code_string, "LP74849-8",
"the first coding is the defining code when the binding is absent"
);
}
#[test]
fn an_absent_text_falls_back_to_the_display_and_is_recorded() {
let view = CodeableConcept {
coding: vec![Coding {
display: Some(primitives::String::from("Fever")),
..code_of("http://loinc.org", "LP74849-8")
}],
..CodeableConcept::default()
};
let lens = CodedTextLens::unbound();
let source: DvCodedText = lens.put(&view, None).expect("one complete coding");
assert_eq!(
source.value, "Fever",
"the display fills the mandatory value"
);
assert_eq!(
Lens::<DvCodedText, CodeableConcept>::fallbacks(&lens, &view),
vec![Fallback::new(CODEABLE_CONCEPT_CELL, "DV_TEXT.value")],
"the one-way row is recorded"
);
}
#[test]
fn an_absent_text_and_display_falls_back_to_the_code() {
let view = CodeableConcept {
coding: vec![code_of("http://loinc.org", "LP74849-8")],
..CodeableConcept::default()
};
let source: DvCodedText = CodedTextLens::unbound()
.put(&view, None)
.expect("one complete coding");
assert_eq!(
source.value, "LP74849-8",
"the code fills the mandatory value when the display is absent"
);
}
#[test]
fn a_concept_with_no_coding_is_refused() {
let view = CodeableConcept {
text: Some(primitives::String::from("Fever")),
..CodeableConcept::default()
};
let error = <CodedTextLens as Lens<DvCodedText, CodeableConcept>>::put(
&CodedTextLens::unbound(),
&view,
None,
)
.expect_err("defining_code is mandatory");
assert!(
error.to_string().contains("defining_code"),
"the refusal names the mandatory attribute: {error}"
);
}
fn with_relation(relation: char) -> DvCodedText {
let target = |system: &str, code: &str| CodePhrase {
terminology_id: TerminologyId::new(system, ""),
code_string: String::from(code),
preferred_term: None,
};
DvCodedText {
value: String::from("Fever"),
hyperlink: None,
formatting: None,
mappings: NonEmptyVec::new(vec![
TermMapping {
r#match: relation,
purpose: None,
target: target("http://snomed.info/sct", "386661006"),
},
TermMapping {
r#match: '=',
purpose: None,
target: target("http://hl7.org/fhir/sid/icd-10", "R50.9"),
},
])
.ok(),
language: None,
encoding: None,
defining_code: target("http://loinc.org", "LP74849-8"),
}
}
#[test]
fn a_term_mapping_fhir_cannot_carry_is_withheld_and_the_rest_converts() {
let lens = CodedTextLens::unbound();
for relation in ['>', '<', '?'] {
let source = with_relation(relation);
let view: CodeableConcept = lens.get(&source).expect("the mapping is withheld");
let codes: Vec<Option<&str>> = view
.coding
.iter()
.map(|coding| coding.code.as_ref().and_then(|code| code.value.as_deref()))
.collect();
assert_eq!(
codes,
[Some("LP74849-8"), Some("R50.9")],
"the defining code and the `=` mapping travel past `{relation}`"
);
let taken = Lens::<DvCodedText, CodeableConcept>::withheld(&lens, &source);
let rows: Vec<(char, &str, &str)> = taken
.iter()
.map(|term| (term.match_code(), term.terminology(), term.code()))
.collect();
assert_eq!(
rows,
[(relation, "http://snomed.info/sct", "386661006")],
"the withheld term names its operator and target"
);
}
}
#[test]
fn a_withheld_term_mapping_returns_from_the_held_value() {
let lens = CodedTextLens::unbound();
for relation in ['>', '<', '?'] {
let source = with_relation(relation);
let view: CodeableConcept = lens.get(&source).expect("the mapping is withheld");
let back = lens
.put(&view, Some(&source))
.expect("the view is complete");
assert_eq!(back, source, "GetPut holds past `{relation}`");
}
}
#[test]
fn an_equivalent_term_mapping_is_withheld_from_nothing() {
let source = with_relation('=');
let lens = CodedTextLens::unbound();
let view: CodeableConcept = lens.get(&source).expect("both mappings travel");
assert_eq!(view.coding.len(), 3, "the defining code and both mappings");
assert!(
Lens::<DvCodedText, CodeableConcept>::withheld(&lens, &source).is_empty(),
"`=` is carried"
);
}
}