use core::str::FromStr;
use std::collections::BTreeSet;
use std::path::Path;
use openehr_mapping_core::diagnostic::Diagnostic;
use openehr_mapping_core::diagnostic::ModelPath;
use openehr_mapping_core::header::metadata::MappingName;
use openehr_mapping_core::position::Located;
use crate::model::ast::ContextMappingFile;
use crate::model::ast::Mapping;
use crate::model::ast::keyword::ContextScope;
use crate::model::ast::keyword::Variable;
use crate::model::error::ModelCode;
use crate::model::load::MappingSet;
use super::FileRules;
use super::error;
pub(super) fn check_slot_context(
rules: FileRules<'_>,
mapping: &Mapping,
path: &ModelPath,
diagnostics: &mut Vec<Diagnostic>,
) {
let Some(ref slot) = mapping.slot_context else {
return;
};
let file = rules.file.file();
let name = rules.name;
require_context(
rules.set,
file,
name,
slot,
&path.field("slotContext"),
diagnostics,
);
let is_reference_variable = mapping
.with
.as_ref()
.and_then(|with| with.openehr.as_ref())
.is_some_and(|value| {
Variable::from_str(value.value()).is_ok_and(|variable| variable == Variable::Reference)
});
if is_reference_variable {
diagnostics.push(error(
file,
name,
ModelCode::SubContextReferenceVariable,
slot.position(),
&path.field("with").field("openehr"),
format!(
"`slotContext: {}` maps into the parent composition, and `openehr: $reference` \
would create a separate composition, which is not supported",
slot.value()
),
));
}
}
pub(super) fn require_context(
set: &MappingSet,
file: &Path,
owner: &MappingName,
reference: &Located<MappingName>,
path: &ModelPath,
diagnostics: &mut Vec<Diagnostic>,
) {
if set.context(reference.value()).is_some() {
return;
}
diagnostics.push(error(
file,
owner,
ModelCode::UnknownMappingReference,
reference.position(),
path,
format!(
"`{}` is no context mapping of the loaded set",
reference.value()
),
));
}
pub(super) fn validate_sub_contexts(set: &MappingSet, diagnostics: &mut Vec<Diagnostic>) {
let listed: BTreeSet<&MappingName> = set
.contexts()
.flat_map(|file| file.context().contexts.iter().map(Located::value))
.collect();
for file in set.contexts() {
let name = file.header().name().value();
let context = file.context();
if context.scope() == ContextScope::Sub && !listed.contains(name) {
let position = context
.scope
.as_ref()
.map_or(context.position, Located::position);
diagnostics.push(error(
file.file(),
name,
ModelCode::UnreferencedSubContext,
position,
&ModelPath::root().field("context").field("scope"),
format!("`{name}` is `scope: sub` and no context lists it under `contexts`"),
));
}
if let Some((reference, index)) = closes_cycle(set, file) {
diagnostics.push(error(
file.file(),
name,
ModelCode::CyclicContextReference,
reference.position(),
&ModelPath::root()
.field("context")
.field("contexts")
.index(index),
format!(
"`{name}` lists `{}` under `contexts`, which reaches `{name}` again",
reference.value()
),
));
}
}
}
fn closes_cycle<'a>(
set: &MappingSet,
file: &'a ContextMappingFile,
) -> Option<(&'a Located<MappingName>, usize)> {
let start = file.header().name().value();
for (index, reference) in file.context().contexts.iter().enumerate() {
let mut seen: BTreeSet<&MappingName> = BTreeSet::new();
let mut pending = vec![reference.value()];
while let Some(next) = pending.pop() {
if next == start {
return Some((reference, index));
}
if !seen.insert(next) {
continue;
}
if let Some(entered) = set.context(next) {
pending.extend(entered.context().contexts.iter().map(Located::value));
}
}
}
None
}