use core::fmt;
use openehr_mapping_core::index::ResolvedNode;
use openehr_mapping_core::index::paths::FlatId;
use openehr_rm::v1_2::paths::RmPath;
use crate::tree::element::Location;
use crate::tree::element::Move;
use crate::tree::element::Resolved;
use crate::tree::path::FhirPath;
use crate::tree::path::Writability;
const ELEMENT: &str = "ELEMENT";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FhirTarget {
expression: FhirPath,
resolved: Resolved,
level: usize,
}
impl FhirTarget {
#[must_use]
pub const fn new(expression: FhirPath, resolved: Resolved) -> Self {
Self {
expression,
resolved,
level: 0,
}
}
#[must_use]
pub const fn at_level(mut self, level: usize) -> Self {
self.level = level;
self
}
#[must_use]
pub const fn level(&self) -> usize {
self.level
}
#[must_use]
pub const fn expression(&self) -> &FhirPath {
&self.expression
}
#[must_use]
pub const fn resolved(&self) -> &Resolved {
&self.resolved
}
#[must_use]
pub const fn writability(&self) -> &Writability {
self.expression.writability()
}
#[must_use]
pub fn repeats(&self) -> bool {
let mut repeats = false;
for step in self.resolved.moves() {
match *step {
Move::Member(ref field) | Move::Choice { ref field, .. } => {
repeats = field.repeats();
}
Move::Extension { .. } | Move::Ordinal(_) | Move::Index(_) | Move::Predicate(_) => {
repeats = false;
}
Move::Resolve { .. } => {}
}
}
repeats
}
}
impl fmt::Display for FhirTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let writability = match *self.writability() {
Writability::Writable => "writable",
Writability::ReadOnly { .. } => "read-only",
};
write!(
f,
"{} -> {} {} {}",
self.expression,
self.resolved.leaf(),
location_name(self.resolved.location()),
writability
)
}
}
fn location_name(location: &Location) -> &'static str {
match *location {
Location::Complex(_) => "complex",
Location::Primitive(_) => "primitive",
Location::Attribute => "attribute",
Location::PrimitiveElement => "primitive-element",
Location::Choice(_) => "choice",
Location::Resource => "resource",
Location::Deferred => "deferred",
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OpenehrTarget {
path: RmPath,
node: ResolvedNode,
tail: RmPath,
occurrences: Vec<FlatId>,
lists: Vec<ListAxis>,
leaf_class: Option<String>,
on_element: bool,
value_class: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListAxis {
node: FlatId,
on_element: bool,
attributes: Vec<String>,
}
impl ListAxis {
#[must_use]
pub fn depth(&self) -> usize {
self.attributes.len().saturating_sub(1)
}
#[must_use]
pub fn attributes(&self) -> &[String] {
&self.attributes
}
#[must_use]
pub fn key(&self) -> String {
let side = if self.on_element { "element" } else { "value" };
format!(
"{}|{side}|{}",
self.node.as_str(),
self.attributes.join("/")
)
}
}
impl OpenehrTarget {
#[must_use]
pub fn new(path: RmPath, node: ResolvedNode, tail: RmPath, occurrences: Vec<FlatId>) -> Self {
let mut target = Self {
path,
node,
tail,
occurrences,
lists: Vec::new(),
leaf_class: None,
on_element: false,
value_class: None,
};
target.lists = target.list_axes();
target
}
fn list_axes(&self) -> Vec<ListAxis> {
let segments: Vec<&str> = self
.tail
.segments
.iter()
.map(|segment| segment.attribute.as_str())
.collect();
let steps = crate::engine::rm::carried::list_steps(self.tail_class(), &segments)
.unwrap_or_default();
steps
.into_iter()
.map(|depth| ListAxis {
node: self.node.flat_id().clone(),
on_element: self.on_element,
attributes: segments
.get(..=depth)
.unwrap_or_default()
.iter()
.map(|segment| String::from(*segment))
.collect(),
})
.collect()
}
#[must_use]
pub fn lists(&self) -> &[ListAxis] {
&self.lists
}
#[must_use]
pub fn ends_on_list(&self) -> bool {
self.lists
.last()
.is_some_and(|axis| axis.attributes.len() == self.tail.segments.len())
}
#[must_use]
pub fn with_element_tail(mut self) -> Self {
self.on_element = true;
self.lists = self.list_axes();
self
}
#[must_use]
pub const fn on_element(&self) -> bool {
self.on_element
}
#[must_use]
pub fn tail_class(&self) -> &str {
if self.on_element {
ELEMENT
} else {
self.node.rm_type()
}
}
#[must_use]
pub fn with_value_class(mut self, class: impl Into<String>) -> Self {
self.value_class = Some(class.into());
self
}
#[must_use]
pub fn value_class(&self) -> &str {
self.value_class
.as_deref()
.unwrap_or_else(|| self.node.rm_type())
}
#[must_use]
pub fn with_leaf_class(mut self, class: impl Into<String>) -> Self {
self.leaf_class = Some(class.into());
self
}
#[must_use]
pub fn leaf_class(&self) -> Option<&str> {
self.leaf_class.as_deref()
}
#[must_use]
pub const fn path(&self) -> &RmPath {
&self.path
}
#[must_use]
pub const fn node(&self) -> &ResolvedNode {
&self.node
}
#[must_use]
pub const fn tail(&self) -> &RmPath {
&self.tail
}
#[must_use]
pub fn occurrences(&self) -> &[FlatId] {
&self.occurrences
}
}
impl fmt::Display for OpenehrTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} -> {} {}",
self.path,
self.node.aql_path().as_str(),
self.node.rm_type()
)?;
if !self.tail.segments.is_empty() {
write!(f, " tail {}", self.tail)?;
}
if !self.occurrences.is_empty() {
let axes: Vec<&str> = self.occurrences.iter().map(FlatId::as_str).collect();
write!(f, " occurrences [{}]", axes.join(", "))?;
}
if !self.lists.is_empty() {
let lists: Vec<String> = self
.lists
.iter()
.map(|axis| axis.attributes.join("/"))
.collect();
write!(f, " lists [{}]", lists.join(", "))?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Target {
Fhir(Box<FhirTarget>),
Openehr(Box<OpenehrTarget>),
}
impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::Fhir(ref target) => write!(f, "fhir {target}"),
Self::Openehr(ref target) => write!(f, "openehr {target}"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Attachment {
Element,
Ancestor,
Descendant,
Unrelated,
}
impl fmt::Display for Attachment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::Element => f.write_str("element"),
Self::Ancestor => f.write_str("ancestor"),
Self::Descendant => f.write_str("descendant"),
Self::Unrelated => f.write_str("unrelated"),
}
}
}