use fhir_types::schema::Kind;
use fhir_types::schema::ValueKind;
use openehr_rm::v1_2::model;
use openehr_rm::v1_2::typing::RmType;
use openehr_rm::v1_2::typing::attribute_type;
use openehr_rm::v1_2::typing::base_name;
use crate::model::ast::keyword::DataType;
use crate::model::ast::keyword::Direction;
pub const PAIRS: &[(&str, &[&str])] = &[
("DV_TEXT", &["string", "Coding", "CodeableConcept"]),
("DV_CODED_TEXT", &["CodeableConcept", "Coding"]),
("CODE_PHRASE", &["Coding"]),
("DV_DATE", &["date"]),
("DV_DATE_TIME", &["dateTime", "Period", "instant"]),
("DV_INTERVAL<DV_DATE_TIME>", &["Period"]),
("DV_INTERVAL<DV_QUANTITY>", &["Range"]),
("DV_INTERVAL", &["Period", "Range"]),
("PARTY_IDENTIFIED", &["Reference"]),
("PARTY_PROXY", &["Reference"]),
("DV_IDENTIFIER", &["Identifier"]),
("DV_PROPORTION", &["Quantity"]),
("DV_QUANTITY", &["Quantity", "decimal", "integer"]),
(
"DV_COUNT",
&["integer", "unsignedInt", "positiveInt", "Quantity", "Count"],
),
("DV_ORDINAL", &["CodeableConcept"]),
("DV_TIME", &["time"]),
("DV_DURATION", &["Duration"]),
("DV_URI", &["url"]),
("DV_MULTIMEDIA", &["Attachment"]),
("DV_PARSABLE", &["Attachment"]),
("DV_BOOLEAN", &["boolean"]),
("PARTY_REF", &["Reference"]),
];
pub const TEXT: &str = "string";
pub const STRUCTURAL: &str = "PATHABLE";
#[must_use]
pub fn type_argument(rm_type: &str) -> Option<RmType> {
rm_type
.parse::<RmType>()
.ok()
.and_then(|parsed| parsed.params().first().cloned())
}
#[must_use]
pub fn attribute_class(rm_type: &str, attribute: &model::RmAttribute) -> &'static str {
let declared = attribute.declared_type;
let Ok(instantiated) = rm_type.parse::<RmType>() else {
return declared;
};
attribute_type(&instantiated, attribute.name)
.ok()
.and_then(|typed| model::class(typed.item.name()))
.map_or(declared, |class| class.name)
}
#[must_use]
pub fn pairs_in(class: &str, code: &str, direction: Option<Direction>) -> bool {
if pairs(class).contains(&code) {
return true;
}
direction == Some(Direction::OpenehrToFhir)
&& PAIRS
.iter()
.any(|&(row, codes)| row != class && model::is_a(class, row) && codes.contains(&code))
}
#[must_use]
pub fn pairs(class: &str) -> &'static [&'static str] {
let row = |wanted: &str| {
PAIRS
.iter()
.find(|&&(name, _)| name == wanted)
.map(|&(_, codes)| codes)
};
row(class)
.or_else(|| {
type_argument(class)
.is_none()
.then(|| row(base_name(class)))
.flatten()
})
.unwrap_or(&[])
}
#[must_use]
pub fn element(class: &str, code: &'static str, text: bool) -> &'static str {
if text && !pairs(class).contains(&code) {
return TEXT;
}
code
}
#[must_use]
pub fn carried(class: &str, suffix: &str, kind: Kind) -> Option<&'static str> {
match kind {
Kind::Complex(name) => Some(name),
Kind::Primitive(ValueKind::Text) | Kind::Attribute | Kind::Xhtml => Some(
pairs(class)
.iter()
.find(|code| code.eq_ignore_ascii_case(suffix))
.copied()
.unwrap_or(TEXT),
),
Kind::Primitive(ValueKind::Boolean | ValueKind::Integer | ValueKind::Decimal) => {
pairs(class)
.iter()
.find(|code| code.eq_ignore_ascii_case(suffix))
.copied()
}
Kind::Resource | Kind::Choice(_) => None,
}
}
#[must_use]
pub const fn declared(data_type: DataType) -> Option<&'static str> {
match data_type {
DataType::Quantity | DataType::Proportion => Some("Quantity"),
DataType::DateTime => Some("dateTime"),
DataType::CodeableConcept => Some("CodeableConcept"),
DataType::Coding => Some("Coding"),
DataType::String => Some("string"),
DataType::Dosage => Some("Dosage"),
DataType::Id => Some("id"),
DataType::Identifier => Some("Identifier"),
DataType::None => None,
}
}
#[must_use]
pub fn suffix_of(code: &str, variants: &[(&'static str, Kind)]) -> Option<&'static str> {
variants
.iter()
.find(|&&(suffix, _)| suffix.eq_ignore_ascii_case(code))
.map(|&(suffix, _)| suffix)
}
#[must_use]
pub fn alternative(class: &str, variants: &[(&'static str, Kind)]) -> Option<&'static str> {
pairs(class)
.iter()
.find_map(|code| suffix_of(code, variants))
}
#[cfg(test)]
mod tests {
use fhir_types::schema::Kind;
use fhir_types::schema::ValueKind;
use super::STRUCTURAL;
use super::alternative;
use super::attribute_class;
use super::carried;
use super::element;
use super::type_argument;
use crate::model::ast::keyword::Direction;
use openehr_rm::v1_2::model;
use openehr_rm::v1_2::typing::RmType;
#[test]
fn a_date_time_element_against_a_date_time_node_converts_as_a_date_time() {
assert_eq!(element("DV_DATE_TIME", "dateTime", true), "dateTime");
}
#[test]
fn a_date_time_element_against_a_text_node_converts_as_text() {
assert_eq!(element("DV_TEXT", "dateTime", true), "string");
}
#[test]
fn a_complex_element_keeps_its_type_whatever_the_class() {
assert_eq!(element("DV_TEXT", "Annotation", false), "Annotation");
}
#[test]
fn the_structural_classes_are_the_pathable_ones() {
for class in [
"EVALUATION",
"CLUSTER",
"COMPOSITION",
"SECTION",
"EVENT_CONTEXT",
] {
assert!(model::is_a(class, STRUCTURAL), "{class} holds other nodes");
}
for class in [
"DV_CODED_TEXT",
"CODE_PHRASE",
"PARTY_PROXY",
"DV_DATE_TIME",
] {
assert!(!model::is_a(class, STRUCTURAL), "{class} is a value");
}
}
const VALUES: &[(&str, Kind)] = &[
("CodeableConcept", Kind::Complex("CodeableConcept")),
("Coding", Kind::Complex("Coding")),
("DateTime", Kind::Primitive(ValueKind::Text)),
("Period", Kind::Complex("Period")),
("String", Kind::Primitive(ValueKind::Text)),
];
#[test]
fn a_choice_is_written_as_the_first_pair_it_admits() {
assert_eq!(
alternative("DV_CODED_TEXT", VALUES),
Some("CodeableConcept")
);
assert_eq!(alternative("CODE_PHRASE", VALUES), Some("Coding"));
assert_eq!(alternative("DV_TEXT", VALUES), Some("String"));
assert_eq!(alternative("DV_DATE_TIME", VALUES), Some("DateTime"));
}
#[test]
fn a_choice_admitting_no_pair_of_the_class_has_no_alternative() {
assert_eq!(alternative("DV_IDENTIFIER", VALUES), None);
assert_eq!(alternative("DV_BOOLEAN", VALUES), None);
}
#[test]
fn the_alternative_a_document_carries_names_its_type_code() {
assert_eq!(
carried("DV_DATE_TIME", "DateTime", Kind::Primitive(ValueKind::Text)),
Some("dateTime")
);
assert_eq!(
carried("DV_CODED_TEXT", "Coding", Kind::Complex("Coding")),
Some("Coding")
);
assert_eq!(
carried("DV_CODED_TEXT", "Code", Kind::Primitive(ValueKind::Text)),
Some("string")
);
assert_eq!(
carried("DV_TEXT", "Boolean", Kind::Primitive(ValueKind::Boolean)),
None
);
}
#[test]
fn a_number_alternative_converts_only_through_a_pair_of_the_class() {
assert_eq!(
carried("DV_COUNT", "Integer", Kind::Primitive(ValueKind::Integer)),
Some("integer")
);
assert_eq!(
carried(
"DV_QUANTITY",
"Decimal",
Kind::Primitive(ValueKind::Decimal)
),
Some("decimal")
);
assert_eq!(
carried("DV_TEXT", "Integer", Kind::Primitive(ValueKind::Integer)),
None
);
}
#[test]
fn a_quantity_choice_is_written_in_the_order_of_its_page() {
let values: &[(&str, Kind)] = &[
("Quantity", Kind::Complex("Quantity")),
("Integer", Kind::Primitive(ValueKind::Integer)),
];
assert_eq!(alternative("DV_QUANTITY", values), Some("Quantity"));
assert_eq!(alternative("DV_COUNT", values), Some("Integer"));
}
#[test]
fn a_generic_node_names_its_parameter() {
assert_eq!(
type_argument("DV_INTERVAL<DV_QUANTITY>")
.as_ref()
.map(RmType::name),
Some("DV_QUANTITY")
);
assert_eq!(type_argument("DV_QUANTITY"), None);
}
#[test]
fn a_bound_of_a_generic_interval_is_its_parameter() {
let lower = model::attribute("DV_INTERVAL", "lower").expect("the model has the bound");
assert_eq!(
attribute_class("DV_INTERVAL<DV_QUANTITY>", lower),
"DV_QUANTITY"
);
assert_eq!(attribute_class("DV_INTERVAL", lower), "DV_ORDERED");
let flag =
model::attribute("DV_INTERVAL", "lower_included").expect("the model has the flag");
assert_eq!(attribute_class("DV_INTERVAL<DV_QUANTITY>", flag), "Boolean");
}
#[test]
fn an_argument_outside_the_bound_keeps_the_declared_type() {
let lower = model::attribute("DV_INTERVAL", "lower").expect("the model has the bound");
assert_eq!(attribute_class("DV_INTERVAL<DV_TEXT>", lower), "DV_ORDERED");
}
#[test]
fn an_interval_choice_is_picked_by_the_parameter() {
let onset: &[(&str, Kind)] = &[
("Period", Kind::Complex("Period")),
("Range", Kind::Complex("Range")),
];
assert_eq!(
alternative("DV_INTERVAL<DV_QUANTITY>", onset),
Some("Range")
);
assert_eq!(
alternative("DV_INTERVAL<DV_DATE_TIME>", onset),
Some("Period")
);
assert_eq!(alternative("DV_INTERVAL", onset), Some("Period"));
}
#[test]
fn an_interval_of_a_class_no_cell_carries_has_no_pair() {
assert!(
super::pairs("DV_INTERVAL<DV_COUNT>").is_empty(),
"no cell carries a count interval"
);
assert_eq!(super::pairs("DV_INTERVAL<DV_QUANTITY>"), &["Range"]);
}
#[test]
fn a_coded_text_reads_through_the_text_row_only_going_out() {
assert!(super::pairs_in(
"DV_CODED_TEXT",
"string",
Some(Direction::OpenehrToFhir)
));
assert!(!super::pairs_in(
"DV_CODED_TEXT",
"string",
Some(Direction::FhirToOpenehr)
));
assert!(!super::pairs_in("DV_CODED_TEXT", "string", None));
assert!(
!super::pairs_in("DV_CODED_TEXT", "Period", Some(Direction::OpenehrToFhir)),
"no inherited row pairs a Period"
);
}
}