use core::fmt;
use fhir_types::codec::Value;
use openehr_mapping_core::composition::CanonicalComposition;
use openehr_mapping_core::index::WebTemplateIndex;
use crate::engine::context::CallContext;
use crate::engine::identity;
use crate::engine::outcome::Location;
use crate::engine::outcome::Loss;
use crate::engine::outcome::Outcome;
use crate::engine::outcome::Warning;
use crate::engine::seam::ReferenceError;
use crate::engine::seam::Seams;
use crate::engine::seam::identity::IdentityRequest;
use crate::engine::seam::identity::IdentitySink;
use crate::engine::traverse;
use crate::engine::traverse::error::EngineError;
use crate::engine::traverse::occurrence;
use crate::operations::error::OperationError;
use crate::operations::run::composition_uid;
use crate::resolve::program::Program;
use crate::tree::element::Table;
pub(super) type MappedOccurrence = (Vec<u32>, String, Outcome<Value>);
struct Anchored<'a> {
versioned_object_uid: &'a str,
entry_path: &'a str,
}
impl fmt::Debug for Anchored<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Anchored")
.field("entry_path", &self.entry_path)
.finish_non_exhaustive()
}
}
impl IdentitySink for Anchored<'_> {
fn identify(&self, request: &IdentityRequest) -> Result<String, ReferenceError> {
Ok(identity::created_id(
self.versioned_object_uid,
self.entry_path,
request,
))
}
}
fn anchored<'a>(container: Option<&'a str>, entry_path: &'a str) -> Option<Anchored<'a>> {
container.map(|versioned_object_uid| Anchored {
versioned_object_uid,
entry_path,
})
}
pub(super) fn occurrences<T: Table + ?Sized>(
program: &Program,
table: &T,
index: &WebTemplateIndex,
composition: &CanonicalComposition,
seams: &Seams<'_>,
context: &CallContext,
) -> Result<(Vec<MappedOccurrence>, Vec<Warning>), OperationError> {
let refused = |source| OperationError::Mapping {
source: Box::new(source),
};
let container = composition_uid(composition).map(identity::versioned_object_uid);
let named = occurrence::occurrences(program, index, composition).map_err(refused)?;
if named.is_empty() {
return Err(refused(EngineError::NotApplicable {
context: String::from(program.context().as_str()),
}));
}
let mut mapped = Vec::new();
let mut inapplicable = 0_usize;
for one in &named {
let sink = anchored(container.as_deref(), one.path());
let run = match sink {
Some(ref sink) => occurrence::to_fhir_at(
program,
table,
index,
composition,
&seams.with_identities(sink),
context,
one,
),
None => occurrence::to_fhir_at(program, table, index, composition, seams, context, one),
};
match run {
Ok(outcome) => mapped.push((one.positions().to_vec(), one.path().to_owned(), outcome)),
Err(EngineError::NotApplicable { .. }) => {
inapplicable = inapplicable.saturating_add(1);
}
Err(error) => return Err(refused(error)),
}
}
if mapped.is_empty() {
return Err(refused(EngineError::NotApplicable {
context: String::from(program.context().as_str()),
}));
}
let mut warnings = Vec::new();
if inapplicable > 0 {
warnings.push(Warning::new(
Loss::InapplicableOccurrences {
archetype: traverse::start_archetype(program),
count: inapplicable,
},
Location::default(),
));
}
Ok((mapped, warnings))
}