use fhir_types::r4::primitives;
use fhir_types::r4::reference::Reference;
use openehr_base::v1_3::base_types::identification::hier_object_id::HierObjectId;
use openehr_base::v1_3::base_types::identification::object_id::ObjectId;
use openehr_base::v1_3::base_types::identification::party_ref::PartyRef;
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;
use crate::tree::element::Table;
pub const PARTY_REF_CELL: &str = "PARTY_REF against Reference";
pub const NAMESPACE: &str = "demographic";
pub const ACTOR: &str = "ACTOR";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PartyRefLens<'target> {
target: &'target str,
}
impl<'target> PartyRefLens<'target> {
#[must_use]
pub const fn new(target: &'target str) -> Self {
Self { target }
}
fn refuse(attribute: &'static str, found: String, reason: &'static str) -> LensError {
LensError::Shape {
cell: PARTY_REF_CELL,
attribute,
found,
reason,
direction: Direction::FhirToOpenehr,
}
}
}
impl Lens<PartyRef, Reference> for PartyRefLens<'_> {
fn cell(&self) -> &'static str {
PARTY_REF_CELL
}
fn get(&self, source: &PartyRef) -> Result<Reference, LensError> {
let refuse = |attribute, found, reason| LensError::Shape {
cell: PARTY_REF_CELL,
attribute,
found,
reason,
direction: Direction::OpenehrToFhir,
};
if source.namespace != NAMESPACE {
return Err(refuse(
"PARTY_REF.namespace",
source.namespace.clone(),
"only a party of the demographic store is a resource of this server",
));
}
let ObjectId::HierObjectId(ref id) = source.id else {
return Err(refuse(
"PARTY_REF.id",
format!("{:?}", source.id),
"a party's version container is named by a HIER_OBJECT_ID",
));
};
if !fhir_id(id.value()) {
return Err(refuse(
"PARTY_REF.id",
String::from(id.value()),
"a FHIR id is 1 to 64 of [A-Za-z0-9-.]",
));
}
Ok(Reference {
reference: Some(primitives::String::from(format!(
"{}/{}",
self.target,
id.value()
))),
..Reference::default()
})
}
fn put(&self, view: &Reference, _existing: Option<&PartyRef>) -> Result<PartyRef, LensError> {
let bare = Reference {
reference: view.reference.clone(),
..Reference::default()
};
if *view != bare {
return Err(Self::refuse(
"Reference",
String::from("type, identifier, display, id or extension"),
"a PARTY_REF holds the party's id and nothing else",
));
}
let literal = text_of(view.reference.as_ref().map(|reference| &reference.value))
.ok_or_else(|| Self::refuse("Reference.reference", String::new(), "it is absent"))?;
let id = literal
.split_once('/')
.filter(|&(resource_type, id)| resource_type == self.target && fhir_id(id))
.map(|(_, id)| id)
.ok_or_else(|| {
Self::refuse(
"Reference.reference",
String::from(literal),
"it is no <type>/<id> literal of the type the element admits",
)
})?;
Ok(PartyRef {
namespace: String::from(NAMESPACE),
r#type: String::from(ACTOR),
id: ObjectId::HierObjectId(HierObjectId::new(id).map_err(|_malformed| {
Self::refuse(
"Reference.reference",
String::from(literal),
"its id is no HIER_OBJECT_ID, a UID with an optional extension",
)
})?),
})
}
fn fallbacks(&self, _view: &Reference) -> Vec<Fallback> {
Vec::new()
}
}
const BASE: &str = "http://hl7.org/fhir/StructureDefinition/";
#[must_use]
pub fn target_of<T: Table + ?Sized>(
table: &T,
profiles: &'static [&'static str],
) -> Option<&'static str> {
match *profiles {
[only] => only
.strip_prefix(BASE)
.filter(|name| table.is_resource(name)),
_ => None,
}
}
fn fhir_id(id: &str) -> bool {
(1..=64).contains(&id.len())
&& id
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
}
#[cfg(test)]
mod tests {
use super::*;
fn practitioner(id: &str) -> Reference {
Reference {
reference: Some(primitives::String::from(format!("Practitioner/{id}"))),
..Reference::default()
}
}
#[test]
fn a_practitioner_reference_round_trips_through_a_party_ref() {
let lens = PartyRefLens::new("Practitioner");
let id = "8f0e7d4c-0000-4000-8000-000000000001";
let party = lens.put(&practitioner(id), None).expect("it carries");
assert_eq!(party.namespace, NAMESPACE);
assert_eq!(party.r#type, ACTOR);
assert_eq!(
party.id,
ObjectId::HierObjectId(HierObjectId::new(id).expect("a UUID is a UID"))
);
assert_eq!(lens.get(&party).expect("it reads"), practitioner(id));
}
#[test]
fn a_reference_of_another_type_is_refused() {
let refused = PartyRefLens::new("Practitioner").put(
&Reference {
reference: Some(primitives::String::from("Patient/p-1")),
..Reference::default()
},
None,
);
assert!(
matches!(refused, Err(LensError::Shape { .. })),
"{refused:?}"
);
}
#[test]
fn a_display_beside_the_reference_is_refused_rather_than_dropped() {
let mut view = practitioner("p-1");
view.display = Some(primitives::String::from("Dr Synthetic"));
let refused = PartyRefLens::new("Practitioner").put(&view, None);
assert!(
matches!(refused, Err(LensError::Shape { .. })),
"{refused:?}"
);
}
#[test]
fn a_party_ref_outside_the_demographic_namespace_is_refused() {
let party = PartyRef {
namespace: String::from("local"),
r#type: String::from("PERSON"),
id: ObjectId::HierObjectId(
HierObjectId::new("8f0e7d4c-0000-4000-8000-000000000001").expect("a UID"),
),
};
let refused = PartyRefLens::new("Practitioner").get(&party);
assert!(
matches!(refused, Err(LensError::Shape { .. })),
"{refused:?}"
);
}
}