use core::error::Error;
use core::fmt;
use openehr_base::v1_3::base_types::identification::generic_id::GenericId;
use openehr_base::v1_3::base_types::identification::object_id::ObjectId;
use openehr_base::v1_3::base_types::identification::party_ref::PartyRef;
use openehr_rm::v1_2::data_types::basic::dv_identifier::DvIdentifier;
pub const MARKER: &str = "fhir-reference";
const PARTY_TYPE: &str = "PERSON";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartyIdentity {
issuer: String,
resource_type: String,
value: String,
}
impl PartyIdentity {
#[must_use]
pub fn new(issuer: &str, resource_type: &str, id: &str) -> Self {
Self {
issuer: String::from(issuer),
resource_type: String::from(resource_type),
value: format!("{resource_type}/{id}"),
}
}
#[must_use]
pub fn issuer(&self) -> &str {
&self.issuer
}
#[must_use]
pub fn resource_type(&self) -> &str {
&self.resource_type
}
#[must_use]
pub fn value(&self) -> &str {
&self.value
}
#[must_use]
pub fn to_identifier(&self) -> DvIdentifier {
DvIdentifier {
issuer: Some(self.issuer.clone()),
assigner: None,
id: self.value.clone(),
r#type: Some(String::from(MARKER)),
}
}
#[must_use]
pub fn to_party_ref(&self) -> PartyRef {
PartyRef {
namespace: self.issuer.clone(),
r#type: String::from(PARTY_TYPE),
id: ObjectId::GenericId(GenericId {
value: self.value.clone(),
scheme: String::from(MARKER),
}),
}
}
#[must_use]
pub fn from_identifier(identifier: &DvIdentifier) -> Option<Self> {
if identifier.r#type.as_deref() != Some(MARKER) {
return None;
}
Self::from_parts(identifier.issuer.as_deref()?, &identifier.id)
}
#[must_use]
pub fn from_party_ref(reference: &PartyRef) -> Option<Self> {
let ObjectId::GenericId(ref id) = reference.id else {
return None;
};
if id.scheme != MARKER {
return None;
}
Self::from_parts(&reference.namespace, &id.value)
}
fn from_parts(issuer: &str, value: &str) -> Option<Self> {
let (resource_type, id) = value.split_once('/')?;
literal(resource_type, id).then(|| Self::new(issuer, resource_type, id))
}
}
pub trait Demographics: fmt::Debug + Send + Sync {
fn resolve(&self, reference: &str) -> Result<PartyIdentity, DemographicsError>;
fn reference_of(&self, identity: &PartyIdentity) -> Option<String>;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NoDemographics;
impl Demographics for NoDemographics {
fn resolve(&self, reference: &str) -> Result<PartyIdentity, DemographicsError> {
Err(DemographicsError::NoEndpoint {
reference: String::from(reference),
})
}
fn reference_of(&self, _identity: &PartyIdentity) -> Option<String> {
None
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IssuerDemographics {
base: String,
}
impl IssuerDemographics {
pub fn new(base: &str) -> Result<Self, DemographicsError> {
let base = base.trim_end_matches('/');
if namespace(base) {
Ok(Self {
base: String::from(base),
})
} else {
Err(DemographicsError::Issuer {
base: String::from(base),
})
}
}
#[must_use]
pub fn base(&self) -> &str {
&self.base
}
}
impl Demographics for IssuerDemographics {
fn resolve(&self, reference: &str) -> Result<PartyIdentity, DemographicsError> {
let unresolvable = |reason| DemographicsError::Unresolvable {
reference: String::from(reference),
reason,
};
if reference.starts_with("urn:") || reference.starts_with('#') {
return Err(unresolvable(Unresolvable::NotOnAServer));
}
if reference.contains("://") {
let (resource_type, id, prefix) =
split_absolute(reference).ok_or_else(|| unresolvable(Unresolvable::NotALiteral))?;
let issuer = prefix.trim_end_matches('/');
if !namespace(issuer) {
return Err(unresolvable(Unresolvable::Issuer));
}
return Ok(PartyIdentity::new(issuer, resource_type, id));
}
let (resource_type, id) =
split_relative(reference).ok_or_else(|| unresolvable(Unresolvable::NotALiteral))?;
Ok(PartyIdentity::new(&self.base, resource_type, id))
}
fn reference_of(&self, identity: &PartyIdentity) -> Option<String> {
(identity.issuer == self.base).then(|| identity.value.clone())
}
}
fn split_relative(path: &str) -> Option<(&str, &str)> {
let mut segments = path.split('/');
let resource_type = segments.next()?;
let id = segments.next()?;
match (segments.next(), segments.next(), segments.next()) {
(None, _, _) | (Some("_history"), Some(_), None) => {}
_ => return None,
}
literal(resource_type, id).then_some((resource_type, id))
}
fn split_absolute(reference: &str) -> Option<(&str, &str, &str)> {
let without_history = match reference.rsplit_once("/_history/") {
Some((head, version)) if !version.is_empty() && !version.contains('/') => head,
_ => reference,
};
let (rest, id) = without_history.rsplit_once('/')?;
let (prefix, resource_type) = rest.rsplit_once('/')?;
literal(resource_type, id).then_some((resource_type, id, prefix))
}
fn literal(resource_type: &str, id: &str) -> bool {
let typed = resource_type
.chars()
.next()
.is_some_and(|first| first.is_ascii_uppercase())
&& resource_type.chars().all(|c| c.is_ascii_alphanumeric());
let identified = (1..=64).contains(&id.len())
&& id
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.');
typed && identified
}
fn namespace(text: &str) -> bool {
text.chars().next().is_some_and(|c| c.is_ascii_alphabetic())
&& text
.chars()
.all(|c| c.is_ascii_alphanumeric() || "_.:/&?=+-".contains(c))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Unresolvable {
NotOnAServer,
NotALiteral,
Issuer,
}
impl fmt::Display for Unresolvable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match *self {
Self::NotOnAServer => "it names a resource of the request, not of a server",
Self::NotALiteral => "it is no <type>/<id> literal reference",
Self::Issuer => "its server base cannot be a PARTY_REF namespace",
})
}
}
#[derive(Debug)]
pub enum DemographicsError {
NoEndpoint {
reference: String,
},
Unresolvable {
reference: String,
reason: Unresolvable,
},
Issuer {
base: String,
},
Endpoint {
reference: String,
source: Box<dyn Error + Send + Sync>,
},
}
impl fmt::Display for DemographicsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::NoEndpoint { ref reference } => {
write!(f, "no demographics endpoint resolves {reference}")
}
Self::Unresolvable {
ref reference,
reason,
} => write!(f, "{reference} cannot be resolved: {reason}"),
Self::Issuer { ref base } => {
write!(f, "{base} cannot be the issuer of a demographic identity")
}
Self::Endpoint { ref reference, .. } => {
write!(f, "the demographics endpoint failed on {reference}")
}
}
}
}
impl Error for DemographicsError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
Self::Endpoint { ref source, .. } => Some(source.as_ref()),
Self::NoEndpoint { .. } | Self::Unresolvable { .. } | Self::Issuer { .. } => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn bridge() -> IssuerDemographics {
IssuerDemographics::new("https://bridge.example/fhir/").expect("a legal base")
}
#[test]
fn a_relative_reference_is_a_resource_of_the_bridge() {
let identity = bridge().resolve("Practitioner/p-1").expect("resolves");
assert_eq!("https://bridge.example/fhir", identity.issuer());
assert_eq!("Practitioner/p-1", identity.value());
assert_eq!(
Some(String::from("Practitioner/p-1")),
bridge().reference_of(&identity)
);
}
#[test]
fn an_absolute_reference_under_the_base_is_a_resource_of_the_bridge() {
let identity = bridge()
.resolve("https://bridge.example/fhir/Organization/o-1/_history/2")
.expect("resolves");
assert_eq!("https://bridge.example/fhir", identity.issuer());
assert_eq!("Organization/o-1", identity.value());
}
#[test]
fn a_reference_to_another_server_keeps_its_server_and_reads_back_as_none() {
let identity = bridge()
.resolve("https://other.example/r4/Practitioner/x.9")
.expect("resolves");
assert_eq!("https://other.example/r4", identity.issuer());
assert_eq!(None, bridge().reference_of(&identity));
}
#[test]
fn a_request_local_reference_is_refused() {
assert!(matches!(
bridge().resolve("urn:uuid:5b0e0c9c-0000-4000-8000-000000000001"),
Err(DemographicsError::Unresolvable {
reason: Unresolvable::NotOnAServer,
..
})
));
assert!(matches!(
bridge().resolve("practitioner"),
Err(DemographicsError::Unresolvable {
reason: Unresolvable::NotALiteral,
..
})
));
}
#[test]
fn no_endpoint_refuses_every_reference() {
assert!(matches!(
NoDemographics.resolve("Practitioner/p-1"),
Err(DemographicsError::NoEndpoint { .. })
));
}
#[test]
fn both_shapes_carry_the_identity_back() {
let identity = PartyIdentity::new("https://bridge.example/fhir", "Practitioner", "p-1");
assert_eq!(
Some(identity.clone()),
PartyIdentity::from_identifier(&identity.to_identifier())
);
assert_eq!(
Some(identity.clone()),
PartyIdentity::from_party_ref(&identity.to_party_ref())
);
let mut other = identity.to_identifier();
other.r#type = Some(String::from("MR"));
assert_eq!(None, PartyIdentity::from_identifier(&other));
}
#[test]
fn a_base_outside_the_namespace_grammar_is_refused() {
assert!(IssuerDemographics::new("https://bridge.example/fhir%20x").is_err());
}
}