use std::path::Path;
use openehr_mapping_core::diagnostic::ModelPath;
use openehr_mapping_core::header::metadata::MappingName;
use openehr_mapping_core::position::Position;
use openehr_rm::v1_2::paths::RmPath;
use crate::engine::lens::party_ref::target_of;
use crate::model::ast::Mapping;
use crate::model::ast::ModelMappingFile;
use crate::resolve::error::ResolveCode;
use crate::resolve::extensions::diagnostic;
use crate::resolve::program::target::FhirTarget;
use crate::resolve::program::target::OpenehrTarget;
use crate::resolve::compile::Compiler;
const COMPOSITION_FIELDS: [&str; 5] = ["composer", "context", "language", "territory", "category"];
pub(super) struct Site<'site> {
pub(super) file: &'site Path,
pub(super) owner: &'site MappingName,
pub(super) position: Position,
pub(super) path: &'site ModelPath,
}
impl Compiler<'_> {
pub(super) fn party_refuses_composition(&mut self, site: &Site<'_>, written: &str) -> bool {
let Some(party) = self.party else {
return false;
};
self.refuse(
site,
ResolveCode::PartyCompositionVariable,
format!(
"`{written}` opens with `$composition` in a party context rooted at a `{party}`, \
which has no composition"
),
);
true
}
pub(super) fn party_refuses_field(&mut self, site: &Site<'_>, resolved: &RmPath) -> bool {
let Some(party) = self.party else {
return false;
};
let Some(field) = resolved
.segments
.first()
.map(|segment| segment.attribute.as_str())
.filter(|attribute| resolved.absolute && COMPOSITION_FIELDS.contains(attribute))
else {
return false;
};
self.refuse(
site,
ResolveCode::PartyCompositionField,
format!(
"`{resolved}` names the composition attribute `{field}`, which the composition \
defaults and the `ctx/` vocabulary set, in a party context rooted at a `{party}`"
),
);
true
}
pub(super) fn party_refuses_participation(
&mut self,
file: &Path,
owner: &MappingName,
method: &Mapping,
path: &ModelPath,
) -> bool {
let (Some(party), Some(function)) = (self.party, method.participations_function.as_ref())
else {
return false;
};
let site = Site {
file,
owner,
position: function.position(),
path: &path.field("participationsFunction"),
};
self.refuse(
&site,
ResolveCode::PartyParticipation,
format!(
"`{}` writes a `participationsFunction` in a party context rooted at a \
`{party}`, and a party holds no `EVENT_CONTEXT` or `PARTICIPATION` list",
method.name.value()
),
);
true
}
pub(super) fn party_refuses_link(
&mut self,
file: &Path,
owner: &MappingName,
method: &Mapping,
path: &ModelPath,
) -> bool {
let (Some(party), Some(link)) = (self.party, method.link.as_ref()) else {
return false;
};
let site = Site {
file,
owner,
position: link.position,
path: &path.field("link"),
};
self.refuse(
&site,
ResolveCode::PartyLink,
format!(
"`{}` writes a `link` in a party context rooted at a `{party}`, and a party \
lives in no EHR for `LINK.target` to name",
method.name.value()
),
);
true
}
pub(super) fn party_refuses_entry(
&mut self,
site: &Site<'_>,
context: &MappingName,
start: &MappingName,
slots_composition: bool,
) -> bool {
let Some(party) = self.party.filter(|_| slots_composition) else {
return false;
};
self.refuse(
site,
ResolveCode::PartySubContextComposition,
format!(
"`{context}` starts at `{start}`, which slots a composition model, and the party \
context rooted at a `{party}` has no composition"
),
);
true
}
pub(super) fn party_ref_target(
&mut self,
file: &ModelMappingFile,
mapping: &str,
sides: (Option<&FhirTarget>, Option<&OpenehrTarget>),
method: &Mapping,
path: &ModelPath,
) {
let (Some(target), Some(openehr)) = sides else {
return;
};
if openehr.leaf_class() != Some("PARTY_REF") {
return;
}
let fhir_path = path.field("with").field("fhir");
let site = &Site {
file: file.file(),
owner: file.header().name().value(),
position: method.position,
path: &fhir_path,
};
let profiles = target.resolved().target_profiles();
if target_of(self.table, profiles).is_some() {
return;
}
let admitted = if profiles.is_empty() {
String::from("no target profile")
} else {
profiles.join(", ")
};
self.refuse(
site,
ResolveCode::PartyRefTarget,
format!(
"`{mapping}` maps `{}` onto a PARTY_REF, and its reference admits {admitted}, \
which names no single resource type to read the party back as",
target.resolved().leaf()
),
);
}
fn refuse(&mut self, site: &Site<'_>, code: ResolveCode, message: String) {
self.diagnostics.push(diagnostic(
site.file,
site.owner,
code,
site.position,
site.path,
message,
));
}
}