use openehr_mapping_core::composition::FlatIndex;
use openehr_mapping_core::composition::PositionError;
use openehr_mapping_core::composition::RmPosition;
use crate::tree::Occurrence;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Cardinality {
One,
Many,
}
impl Cardinality {
#[must_use]
pub const fn of(repeats: bool) -> Self {
if repeats { Self::Many } else { Self::One }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Placement {
slots: Vec<Option<usize>>,
dropped: usize,
}
impl Placement {
#[must_use]
pub fn decide(inputs: usize, cardinality: Cardinality, already: usize) -> Self {
match cardinality {
Cardinality::Many => Self {
slots: (0..inputs)
.map(|offset| Some(already.saturating_add(offset)))
.collect(),
dropped: 0,
},
Cardinality::One => {
let last = inputs.saturating_sub(1);
Self {
slots: (0..inputs)
.map(|offset| (offset == last).then_some(0))
.collect(),
dropped: last,
}
}
}
}
#[must_use]
pub fn slots(&self) -> &[Option<usize>] {
&self.slots
}
#[must_use]
pub const fn dropped(&self) -> usize {
self.dropped
}
#[must_use]
pub const fn lossy(&self) -> bool {
self.dropped > 0
}
}
#[must_use]
pub fn under(parent: &Occurrence, index: Option<usize>) -> Occurrence {
let mut indices: Vec<usize> = parent.indices().to_vec();
if let Some(index) = index {
indices.push(index);
}
Occurrence::new(indices)
}
pub fn positions_under(
parent: &[RmPosition],
index: Option<usize>,
) -> Result<Vec<RmPosition>, PositionError> {
let mut positions: Vec<RmPosition> = parent.to_vec();
if let Some(index) = index {
let index = u32::try_from(index).map_err(|_refused| PositionError::Overflow)?;
positions.push(RmPosition::try_from(FlatIndex::new(index))?);
}
Ok(positions)
}
#[cfg(test)]
mod tests {
use super::Cardinality;
use super::Placement;
use super::positions_under;
use super::under;
use crate::tree::Occurrence;
use openehr_mapping_core::composition::RmPosition;
fn writes(parent: &[usize], inputs: usize, cardinality: Cardinality) -> Vec<String> {
let parent = Occurrence::new(parent.to_vec());
Placement::decide(inputs, cardinality, 0)
.slots()
.iter()
.filter_map(|slot| slot.map(|index| under(&parent, Some(index)).to_string()))
.collect()
}
#[test]
fn a_later_write_to_one_value_takes_the_same_place() {
let first = Placement::decide(1, Cardinality::One, 0);
let second = Placement::decide(1, Cardinality::One, 1);
assert_eq!(first.slots(), second.slots(), "both writes take one place");
assert_eq!(first.slots(), [Some(0)]);
}
#[test]
fn two_mappings_into_one_list_append() {
let first = Placement::decide(1, Cardinality::Many, 0);
let second = Placement::decide(1, Cardinality::Many, 1);
assert_eq!(first.slots(), [Some(0)], "the first write starts the list");
assert_eq!(second.slots(), [Some(1)], "the second write appends");
assert!(!first.lossy(), "an append loses nothing");
}
#[test]
fn many_into_one_takes_the_last_and_records_the_rest() {
let placement = Placement::decide(3, Cardinality::One, 0);
assert_eq!(placement.slots(), [None, None, Some(0)]);
assert_eq!(placement.dropped(), 2, "the two earlier entries are a loss");
assert!(placement.lossy(), "the loss is declared");
}
#[test]
fn the_iterated_shape_pairs_the_two_lists() {
assert_eq!(writes(&[0], 1, Cardinality::Many), ["0.0"]);
assert_eq!(writes(&[1], 1, Cardinality::Many), ["1.0"]);
}
#[test]
fn the_unpaired_shape_appends_a_sibling_per_child_mapping() {
let mut appended = Vec::new();
let mut already = 0usize;
for _parent in 0..2 {
for _child in 0..2 {
let placement = Placement::decide(1, Cardinality::Many, already);
for slot in placement.slots().iter().flatten() {
appended.push(*slot);
already = already.saturating_add(1);
}
}
}
assert_eq!(
appended,
[0, 1, 2, 3],
"each child mapping of each parent occurrence appends its own entry"
);
}
#[test]
fn the_double_nesting_counter_example_flattens_the_inner_list() {
let paired: Vec<String> = (0..2)
.flat_map(|outer| writes(&[outer], 2, Cardinality::Many))
.collect();
assert_eq!(paired, ["0.0", "0.1", "1.0", "1.1"], "two entries of two");
let flattened: Vec<String> = writes(&[], 4, Cardinality::Many);
assert_eq!(flattened, ["0", "1", "2", "3"], "four entries of one");
}
#[test]
fn an_openehr_position_counts_from_one() {
let positions = positions_under(&[], Some(0)).expect("0 has a position");
assert_eq!(
positions.first().copied().map(RmPosition::get),
Some(1),
"the first instance is [1]"
);
let deeper = positions_under(&[RmPosition::first()], Some(2)).expect("2 has a position");
assert_eq!(
deeper
.iter()
.map(|position| position.get())
.collect::<Vec<u32>>(),
[1, 3],
"the parent position leads and the index follows"
);
}
#[test]
fn a_write_with_no_repeating_element_keeps_its_parent_occurrence() {
let parent = Occurrence::new([2, 1]);
assert_eq!(under(&parent, None), parent, "nothing is appended");
assert_eq!(under(&parent, Some(0)).to_string(), "2.1.0");
}
}