use fhir_types::r4::identifier::Identifier;
use fhir_types::r4::primitives::Code;
use fhir_types::r4::primitives::Instant;
use fhir_types::r4::provenance::Provenance;
use fhir_types::r4::provenance::ProvenanceAgent;
use fhir_types::r4::provenance::ProvenanceEntity;
use fhir_types::r4::reference::Reference;
use crate::engine::context::CallContext;
pub const DERIVATION: &str = "derivation";
#[must_use]
pub fn of_run(
targets: &[String],
recorded: &str,
device: &str,
composition: &CompositionSource,
context: Option<&CallContext>,
) -> Provenance {
let who = context
.and_then(CallContext::who)
.cloned()
.unwrap_or_else(|| literal(device));
Provenance {
target: targets.iter().map(|target| literal(target)).collect(),
recorded: Instant::from(recorded),
agent: vec![ProvenanceAgent {
who,
on_behalf_of: context.and_then(CallContext::on_behalf_of).cloned(),
..ProvenanceAgent::default()
}],
entity: vec![ProvenanceEntity {
role: Code::from(DERIVATION),
what: composition.reference(),
..ProvenanceEntity::default()
}],
..Provenance::default()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompositionSource {
template_id: String,
uid: Option<String>,
}
impl CompositionSource {
#[must_use]
pub fn new(template_id: impl Into<String>) -> Self {
Self {
template_id: template_id.into(),
uid: None,
}
}
#[must_use]
pub fn with_uid(mut self, uid: impl Into<String>) -> Self {
self.uid = Some(uid.into());
self
}
#[must_use]
pub fn uid(&self) -> Option<&str> {
self.uid.as_deref()
}
#[must_use]
fn reference(&self) -> Reference {
Reference {
identifier: self.uid.as_ref().map(|uid| {
Box::new(Identifier {
value: Some(fhir_types::r4::primitives::String::from(uid.as_str())),
..Identifier::default()
})
}),
display: Some(fhir_types::r4::primitives::String::from(
self.template_id.as_str(),
)),
..Reference::default()
}
}
}
fn literal(text: &str) -> Reference {
Reference {
reference: Some(fhir_types::r4::primitives::String::from(text)),
..Reference::default()
}
}
#[cfg(test)]
mod tests {
use super::CompositionSource;
use super::of_run;
use crate::engine::context::CallContext;
use fhir_types::r4::reference::Reference;
fn reference(text: &str) -> Reference {
Reference {
reference: Some(fhir_types::r4::primitives::String::from(text)),
..Reference::default()
}
}
#[test]
fn the_agent_defaults_to_the_engines_own_device() {
let source = CompositionSource::new("ferrobridge.diagnose.v1");
let provenance = of_run(
&[String::from("Condition/one")],
"2026-09-15T10:00:00Z",
"Device/ferrobridge",
&source,
None,
);
let who = provenance
.agent
.first()
.and_then(|agent| agent.who.reference.as_ref())
.and_then(|value| value.value.clone());
assert_eq!(who, Some(String::from("Device/ferrobridge")));
}
#[test]
fn the_call_context_overrides_the_agent_and_names_the_institution() {
let source = CompositionSource::new("ferrobridge.diagnose.v1");
let context = CallContext::new()
.with_who(reference("Practitioner/456"))
.with_on_behalf_of(reference("Organization/charite"));
let provenance = of_run(
&[String::from("Condition/one")],
"2026-09-15T10:00:00Z",
"Device/ferrobridge",
&source,
Some(&context),
);
let agent = provenance.agent.first().expect("one agent");
assert_eq!(
agent.who.reference.as_ref().and_then(|v| v.value.clone()),
Some(String::from("Practitioner/456"))
);
assert_eq!(
agent
.on_behalf_of
.as_ref()
.and_then(|value| value.reference.as_ref())
.and_then(|value| value.value.clone()),
Some(String::from("Organization/charite"))
);
}
#[test]
fn the_entity_is_a_derivation_naming_the_composition_uid() {
let source = CompositionSource::new("ferrobridge.diagnose.v1")
.with_uid("8849182c-82ad-4088-a07f-48ead4180515::ferrobridge::1");
let provenance = of_run(
&[String::from("Condition/one")],
"2026-09-15T10:00:00Z",
"Device/ferrobridge",
&source,
None,
);
let entity = provenance.entity.first().expect("one entity");
assert_eq!(entity.role.value.as_deref(), Some("derivation"));
assert_eq!(
entity
.what
.identifier
.as_ref()
.and_then(|identifier| identifier.value.as_ref())
.and_then(|value| value.value.clone()),
Some(String::from(
"8849182c-82ad-4088-a07f-48ead4180515::ferrobridge::1"
))
);
}
}