pub mod binding;
pub mod condition;
pub mod hierarchy;
pub mod manual;
pub mod mapping;
pub mod port;
pub mod target;
use core::fmt;
use openehr_mapping_core::diagnostic::Diagnostic;
use openehr_mapping_core::header::archetype::ArchetypeId;
use openehr_mapping_core::header::metadata::MappingName;
use openehr_mapping_core::index::paths::FlatId;
use crate::resolve::program::binding::ModelBinding;
use crate::resolve::program::binding::ProfileBinding;
use crate::resolve::program::binding::ResourceType;
use crate::resolve::program::binding::TemplateBinding;
use crate::resolve::program::condition::Condition;
use crate::resolve::program::hierarchy::Hierarchy;
use crate::resolve::program::hierarchy::Preprocessor;
use crate::resolve::program::mapping::Mapping;
use crate::resolve::program::mapping::Method;
use crate::resolve::program::mapping::render_preprocessor;
use openehr_mapping_core::party::PartyClass;
pub const SLOT_DEPTH_LIMIT: usize = 8;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Program {
context: MappingName,
profile: ProfileBinding,
template: TemplateBinding,
resource: ResourceType,
start: MappingName,
models: Vec<ModelBinding>,
operational: Vec<MappingName>,
preprocessors: Vec<Preprocessor>,
mappings: Vec<Mapping>,
warnings: Vec<Diagnostic>,
root_axes: Vec<FlatId>,
root: Option<FlatId>,
party: Option<PartyClass>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProgramParts {
pub context: MappingName,
pub profile: ProfileBinding,
pub template: TemplateBinding,
pub resource: ResourceType,
pub start: MappingName,
pub models: Vec<ModelBinding>,
pub operational: Vec<MappingName>,
pub preprocessors: Vec<Preprocessor>,
pub mappings: Vec<Mapping>,
}
impl Program {
#[must_use]
pub fn new(parts: ProgramParts) -> Self {
Self {
context: parts.context,
profile: parts.profile,
template: parts.template,
resource: parts.resource,
start: parts.start,
models: parts.models,
operational: parts.operational,
preprocessors: parts.preprocessors,
mappings: parts.mappings,
warnings: Vec::new(),
root_axes: Vec::new(),
root: None,
party: None,
}
}
#[must_use]
pub(crate) fn with_root_axes(mut self, axes: Vec<FlatId>) -> Self {
self.root_axes = axes;
self
}
#[must_use]
pub(crate) fn with_root(mut self, root: Option<FlatId>) -> Self {
self.root = root;
self
}
#[must_use]
pub const fn root(&self) -> Option<&FlatId> {
self.root.as_ref()
}
#[must_use]
pub fn root_axes(&self) -> &[FlatId] {
&self.root_axes
}
#[must_use]
pub(crate) const fn with_party(mut self, party: Option<PartyClass>) -> Self {
self.party = party;
self
}
#[must_use]
pub const fn party(&self) -> Option<PartyClass> {
self.party
}
#[must_use]
pub(crate) fn with_warnings(mut self, warnings: Vec<Diagnostic>) -> Self {
self.warnings = warnings;
self
}
#[must_use]
pub fn warnings(&self) -> &[Diagnostic] {
&self.warnings
}
#[must_use]
pub const fn context(&self) -> &MappingName {
&self.context
}
#[must_use]
pub const fn profile(&self) -> &ProfileBinding {
&self.profile
}
#[must_use]
pub const fn template(&self) -> &TemplateBinding {
&self.template
}
#[must_use]
pub const fn resource(&self) -> &ResourceType {
&self.resource
}
#[must_use]
pub const fn start(&self) -> &MappingName {
&self.start
}
#[must_use]
pub fn models(&self) -> &[ModelBinding] {
&self.models
}
#[must_use]
pub fn operational(&self) -> &[MappingName] {
&self.operational
}
#[must_use]
pub fn preprocessors(&self) -> &[Preprocessor] {
&self.preprocessors
}
#[must_use]
pub fn preprocessor(&self) -> Option<&Preprocessor> {
self.preprocessors
.iter()
.find(|preprocessor| preprocessor.model() == &self.start)
}
#[must_use]
pub fn hierarchy(&self) -> Option<&Hierarchy> {
self.preprocessor().and_then(Preprocessor::hierarchy)
}
#[must_use]
pub fn fhir_condition(&self) -> Option<&Condition> {
self.preprocessor().and_then(Preprocessor::fhir_condition)
}
#[must_use]
pub fn openehr_condition(&self) -> Option<&Condition> {
self.preprocessor()
.and_then(Preprocessor::openehr_condition)
}
#[must_use]
pub fn mappings(&self) -> &[Mapping] {
&self.mappings
}
#[must_use]
pub fn mapping_named(&self, name: &str) -> Option<&Mapping> {
walk_mappings(&self.mappings).find(|mapping| mapping.name() == name)
}
}
fn walk_mappings(mappings: &[Mapping]) -> impl Iterator<Item = &Mapping> {
let mut found: Vec<&Mapping> = Vec::new();
let mut stack: Vec<&Mapping> = mappings.iter().rev().collect();
while let Some(mapping) = stack.pop() {
found.push(mapping);
let nested = match *mapping.method() {
Method::Reference { ref mappings, .. }
| Method::Slot { ref mappings, .. }
| Method::SlotContext { ref mappings, .. } => mappings.as_slice(),
Method::Value
| Method::Link { .. }
| Method::Programmed { .. }
| Method::Participation { .. } => &[][..],
};
for child in nested.iter().chain(mapping.followed_by()).rev() {
stack.push(child);
}
}
found.into_iter()
}
impl fmt::Display for Program {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "program {}", self.context)?;
writeln!(f, " profile {}", self.profile)?;
writeln!(f, " template {}", self.template)?;
writeln!(f, " resource {}", self.resource)?;
writeln!(f, " start {}", self.start)?;
if let Some(party) = self.party {
writeln!(f, " party {party}")?;
}
if !self.root_axes.is_empty() {
let axes: Vec<&str> = self.root_axes.iter().map(FlatId::as_str).collect();
writeln!(f, " root axes [{}]", axes.join(", "))?;
}
for model in &self.models {
writeln!(
f,
" model {} version {} archetype {} revision {}",
model.name(),
model.version(),
model
.archetype()
.map_or_else(|| String::from("-"), ArchetypeId::to_string),
model.revision()
)?;
for extension in model.extensions() {
writeln!(f, " extension {extension}")?;
}
}
for operational in &self.operational {
writeln!(f, " operational {operational}")?;
}
for preprocessor in &self.preprocessors {
render_preprocessor(f, " ", preprocessor)?;
}
for mapping in &self.mappings {
mapping.render(f, 1)?;
}
for warning in &self.warnings {
writeln!(f, " warning {}: {}", warning.code(), warning.message())?;
}
Ok(())
}
}