use crate::report::{GroupPath, GroupStep, ValidationIssue};
use crate::validator::ValidationLayer;
use crate::{EdifactError, Segment};
use smallvec::SmallVec;
use std::collections::HashMap;
use std::ops::Range;
use std::sync::Arc;
const MAX_DEPTH: usize = 64;
#[derive(Clone)]
enum Text {
Static(&'static str),
Shared(Arc<str>),
}
impl Text {
fn as_str(&self) -> &str {
match self {
Self::Static(s) => s,
Self::Shared(s) => s,
}
}
}
impl std::fmt::Debug for Text {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self.as_str(), f)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Rule {
OneOf {
codes: Arc<[Arc<str>]>,
optional: bool,
accept_unclaimed: bool,
},
NotUsed,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Discriminator {
element: u16,
component: u16,
rule: Rule,
}
impl Discriminator {
#[must_use]
pub fn one_of<I, S>(element: u16, component: u16, codes: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<Arc<str>>,
{
Self {
element,
component,
rule: Rule::OneOf {
codes: codes.into_iter().map(Into::into).collect(),
optional: false,
accept_unclaimed: false,
},
}
}
#[must_use]
pub const fn not_used(element: u16, component: u16) -> Self {
Self {
element,
component,
rule: Rule::NotUsed,
}
}
#[must_use]
pub fn optional(mut self) -> Self {
if let Rule::OneOf { optional, .. } = &mut self.rule {
*optional = true;
}
self
}
#[must_use]
pub fn accept_unclaimed(mut self) -> Self {
if let Rule::OneOf {
accept_unclaimed, ..
} = &mut self.rule
{
*accept_unclaimed = true;
}
self
}
#[must_use]
pub fn element(&self) -> u16 {
self.element
}
#[must_use]
pub fn component(&self) -> u16 {
self.component
}
#[must_use]
pub fn codes(&self) -> &[Arc<str>] {
match &self.rule {
Rule::OneOf { codes, .. } => codes,
Rule::NotUsed => &[],
}
}
#[must_use]
pub fn is_not_used(&self) -> bool {
matches!(self.rule, Rule::NotUsed)
}
#[must_use]
pub fn is_optional(&self) -> bool {
matches!(self.rule, Rule::OneOf { optional: true, .. })
}
#[must_use]
pub fn accepts_unclaimed(&self) -> bool {
matches!(
self.rule,
Rule::OneOf {
accept_unclaimed: true,
..
}
)
}
fn admits(&self, tag: &str, segment: &Segment<'_>, tables: &Tables) -> bool {
let value = segment
.component_str(usize::from(self.element), usize::from(self.component))
.filter(|v| !v.is_empty());
match (&self.rule, value) {
(Rule::NotUsed, value) => value.is_none(),
(Rule::OneOf { optional, .. }, None) => *optional,
(
Rule::OneOf {
codes,
accept_unclaimed,
..
},
Some(value),
) => {
codes.iter().any(|c| &**c == value)
|| (*accept_unclaimed
&& !tables.claimed(tag, self.element, self.component, value))
}
}
}
}
#[derive(Debug, Clone)]
pub struct SegmentPlace {
tag: Text,
id: Option<Text>,
discriminators: Option<Arc<[Discriminator]>>,
min: u32,
max: Option<u32>,
}
impl SegmentPlace {
#[must_use]
pub const fn new(tag: &'static str) -> Self {
Self {
tag: Text::Static(tag),
id: None,
discriminators: None,
min: 0,
max: None,
}
}
#[must_use]
pub fn owned(tag: impl Into<Arc<str>>) -> Self {
Self {
tag: Text::Shared(tag.into()),
..Self::new("")
}
}
#[must_use]
pub fn with_id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(Text::Shared(id.into()));
self
}
#[must_use]
pub fn when(mut self, discriminator: Discriminator) -> Self {
let mut all: Vec<Discriminator> = self.discriminators().to_vec();
all.push(discriminator);
self.discriminators = Some(all.into());
self
}
#[must_use]
pub fn with_min(mut self, min: u32) -> Self {
self.min = min;
self
}
#[must_use]
pub fn with_max(mut self, max: u32) -> Self {
self.max = Some(max);
self
}
#[must_use]
pub fn tag(&self) -> &str {
self.tag.as_str()
}
#[must_use]
pub fn id(&self) -> Option<&str> {
self.id.as_ref().map(Text::as_str)
}
#[must_use]
pub fn discriminators(&self) -> &[Discriminator] {
self.discriminators.as_deref().unwrap_or(&[])
}
#[must_use]
pub fn min(&self) -> u32 {
self.min
}
#[must_use]
pub fn max(&self) -> Option<u32> {
self.max
}
fn matches(&self, segment: &Segment<'_>, tables: &Tables) -> bool {
let tag = self.tag();
segment.tag() == tag
&& self
.discriminators()
.iter()
.all(|d| d.admits(tag, segment, tables))
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum Entry {
Segment(SegmentPlace),
Group(GroupDef),
}
#[derive(Debug, Clone)]
enum Body {
Static(&'static [GroupDef]),
Open(Arc<[Entry]>),
Ordered(Arc<[Entry]>),
}
#[derive(Debug, Clone)]
pub struct GroupDef {
name: Text,
id: Option<Text>,
trigger: SegmentPlace,
body: Body,
min: u32,
max: Option<u32>,
}
impl GroupDef {
#[must_use]
pub const fn new(name: &'static str, trigger: &'static str) -> Self {
Self::with_children(name, trigger, &[])
}
#[must_use]
pub const fn with_children(
name: &'static str,
trigger: &'static str,
children: &'static [GroupDef],
) -> Self {
Self {
name: Text::Static(name),
id: None,
trigger: SegmentPlace::new(trigger),
body: Body::Static(children),
min: 0,
max: None,
}
}
pub fn builder(name: impl Into<Arc<str>>, trigger: SegmentPlace) -> GroupDefBuilder {
GroupDefBuilder {
name: name.into(),
id: None,
trigger,
entries: Vec::new(),
ordered: false,
min: 0,
max: None,
}
}
#[must_use]
pub fn name(&self) -> &str {
self.name.as_str()
}
#[must_use]
pub fn id(&self) -> Option<&str> {
self.id.as_ref().map(Text::as_str)
}
#[must_use]
pub fn trigger(&self) -> &SegmentPlace {
&self.trigger
}
#[must_use]
pub fn trigger_tag(&self) -> &str {
self.trigger.tag()
}
#[must_use]
pub fn is_ordered(&self) -> bool {
matches!(self.body, Body::Ordered(_))
}
#[must_use]
pub fn entries(&self) -> &[Entry] {
match &self.body {
Body::Ordered(entries) => entries,
Body::Static(_) | Body::Open(_) => &[],
}
}
pub fn children(&self) -> impl Iterator<Item = &GroupDef> + '_ {
let (groups, entries): (&[GroupDef], &[Entry]) = match &self.body {
Body::Static(groups) => (groups, &[]),
Body::Open(entries) | Body::Ordered(entries) => (&[], entries),
};
groups.iter().chain(entries.iter().filter_map(|e| match e {
Entry::Group(g) => Some(g),
Entry::Segment(_) => None,
}))
}
#[must_use]
pub fn min(&self) -> u32 {
self.min
}
#[must_use]
pub fn max(&self) -> Option<u32> {
self.max
}
fn members(&self) -> (Members<'_>, bool) {
match &self.body {
Body::Static(groups) => (Members::Groups(groups), false),
Body::Open(entries) => (Members::Entries(entries), false),
Body::Ordered(entries) => (Members::Entries(entries), true),
}
}
}
#[derive(Debug, Clone)]
#[must_use]
pub struct GroupDefBuilder {
name: Arc<str>,
id: Option<Arc<str>>,
trigger: SegmentPlace,
entries: Vec<Entry>,
ordered: bool,
min: u32,
max: Option<u32>,
}
impl GroupDefBuilder {
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn segment(mut self, place: SegmentPlace) -> Self {
self.ordered = true;
self.entries.push(Entry::Segment(place));
self
}
pub fn ordered(mut self) -> Self {
self.ordered = true;
self
}
pub fn group(mut self, group: GroupDef) -> Self {
self.entries.push(Entry::Group(group));
self
}
pub fn min(mut self, min: u32) -> Self {
self.min = min;
self
}
pub fn max(mut self, max: u32) -> Self {
self.max = Some(max);
self
}
pub fn build(self) -> Result<GroupDef, EdifactError> {
let body = if self.ordered {
let mut entries = Vec::with_capacity(self.entries.len() + 1);
entries.push(Entry::Segment(self.trigger.clone()));
entries.extend(self.entries);
Body::Ordered(entries.into())
} else {
Body::Open(self.entries.into())
};
let group = GroupDef {
name: Text::Shared(self.name),
id: self.id.map(Text::Shared),
trigger: self.trigger,
body,
min: self.min,
max: self.max,
};
check_group(&group, 1)?;
Ok(group)
}
}
fn invalid(group: &str, reason: &'static str) -> EdifactError {
EdifactError::InvalidGroupSchema {
group: group.to_owned(),
reason,
}
}
fn check_bounds(group: &str, min: u32, max: Option<u32>) -> Result<(), EdifactError> {
match max {
Some(0) => Err(invalid(group, "max is 0")),
Some(max) if min > max => Err(invalid(group, "min exceeds max")),
_ => Ok(()),
}
}
fn check_place(group: &str, place: &SegmentPlace) -> Result<(), EdifactError> {
if !crate::tokenizer::is_valid_segment_tag(place.tag()) {
return Err(invalid(
group,
"segment tag is not three upper-case ASCII letters",
));
}
if place.discriminators().iter().any(|d| match &d.rule {
Rule::OneOf { codes, .. } => codes.is_empty(),
Rule::NotUsed => false,
}) {
return Err(invalid(group, "one_of discriminator admits no code"));
}
check_bounds(group, place.min, place.max)
}
fn check_group(group: &GroupDef, depth: usize) -> Result<(), EdifactError> {
let name = group.name();
if depth > MAX_DEPTH {
return Err(invalid(name, "groups nest deeper than 64 levels"));
}
if name.is_empty() {
return Err(invalid(name, "group name is empty"));
}
check_bounds(name, group.min, group.max)?;
check_place(name, &group.trigger)?;
let (members, _) = group.members();
for j in 0..members.len() {
match members.get(j) {
Cand::Seg(place) => check_place(name, place)?,
Cand::Group(child) => check_group(child, depth + 1)?,
}
}
Ok(())
}
#[derive(Debug)]
#[non_exhaustive]
pub struct SegmentGroupIndexed<'a> {
pub definition: &'a str,
pub def: Option<&'a GroupDef>,
pub total_span: Range<usize>,
pub children: Vec<SegmentGroupIndexed<'a>>,
pub occurrence_index: usize,
pub unplaced: Vec<usize>,
}
impl<'a> SegmentGroupIndexed<'a> {
pub fn direct_segment_indices(&self) -> impl Iterator<Item = usize> + '_ {
DirectIndices {
pos: self.total_span.start,
end: self.total_span.end,
children: &self.children,
unplaced: &self.unplaced,
}
}
#[must_use]
pub fn segments<'s, 'd>(&self, all: &'s [Segment<'d>]) -> &'s [Segment<'d>] {
all.get(self.total_span.clone()).unwrap_or(&[])
}
pub fn descendants(&self) -> Descendants<'_, 'a> {
Descendants { stack: vec![self] }
}
pub fn find<'q>(
&'q self,
name: &'q str,
) -> impl Iterator<Item = &'q SegmentGroupIndexed<'a>> + 'q {
self.descendants().filter(move |g| g.definition == name)
}
}
#[cfg(test)]
thread_local! {
static STEPS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
#[inline(always)]
fn step() {
#[cfg(test)]
STEPS.with(|steps| steps.set(steps.get() + 1));
}
struct DirectIndices<'t, 'a> {
pos: usize,
end: usize,
children: &'t [SegmentGroupIndexed<'a>],
unplaced: &'t [usize],
}
impl Iterator for DirectIndices<'_, '_> {
type Item = usize;
fn next(&mut self) -> Option<usize> {
while self.pos < self.end {
step();
while let Some((first, rest)) = self.children.split_first() {
step();
if first.total_span.end <= self.pos {
self.children = rest;
} else {
break;
}
}
if let Some(child) = self.children.first()
&& child.total_span.start <= self.pos
{
self.pos = child.total_span.end;
continue;
}
while let Some((&first, rest)) = self.unplaced.split_first() {
step();
if first < self.pos {
self.unplaced = rest;
} else {
break;
}
}
let pos = self.pos;
self.pos += 1;
if self.unplaced.first() == Some(&pos) {
continue;
}
return Some(pos);
}
None
}
}
pub struct Descendants<'t, 'a> {
stack: Vec<&'t SegmentGroupIndexed<'a>>,
}
impl<'t, 'a> Iterator for Descendants<'t, 'a> {
type Item = &'t SegmentGroupIndexed<'a>;
fn next(&mut self) -> Option<Self::Item> {
let node = self.stack.pop()?;
self.stack.extend(node.children.iter().rev());
Some(node)
}
}
pub fn group_segments_indexed<'g>(
segments: &[Segment<'_>],
schema: &'g [GroupDef],
root_name: &'g str,
) -> SegmentGroupIndexed<'g> {
let root = Members::Groups(schema);
let tables = Tables::build(root);
let resolved = resolve(segments, root, false, root_name, &tables, false);
resolved.into_tree()
}
#[derive(Debug)]
struct SchemaInner {
root_name: Arc<str>,
ordered: bool,
entries: Vec<Entry>,
tables: Tables,
}
#[derive(Debug, Clone)]
pub struct GroupSchema {
inner: Arc<SchemaInner>,
}
impl GroupSchema {
pub fn new(
root_name: impl Into<Arc<str>>,
groups: impl IntoIterator<Item = GroupDef>,
) -> Result<Self, EdifactError> {
Self::compile(
root_name.into(),
false,
groups.into_iter().map(Entry::Group),
)
}
pub fn ordered(
root_name: impl Into<Arc<str>>,
entries: impl IntoIterator<Item = Entry>,
) -> Result<Self, EdifactError> {
Self::compile(root_name.into(), true, entries)
}
fn compile(
root_name: Arc<str>,
ordered: bool,
entries: impl IntoIterator<Item = Entry>,
) -> Result<Self, EdifactError> {
let entries: Vec<Entry> = entries.into_iter().collect();
if root_name.is_empty() {
return Err(invalid("", "root name is empty"));
}
for entry in &entries {
match entry {
Entry::Segment(place) => check_place(&root_name, place)?,
Entry::Group(group) => check_group(group, 1)?,
}
}
let tables = Tables::build(Members::Entries(&entries));
Ok(Self {
inner: Arc::new(SchemaInner {
root_name,
ordered,
entries,
tables,
}),
})
}
#[must_use]
pub fn root_name(&self) -> &str {
&self.inner.root_name
}
#[must_use]
pub fn is_ordered(&self) -> bool {
self.inner.ordered
}
#[must_use]
pub fn entries(&self) -> &[Entry] {
&self.inner.entries
}
#[must_use]
pub fn resolve<'s>(&'s self, segments: &[Segment<'_>]) -> GroupResolution<'s> {
let inner = &*self.inner;
let resolved = resolve(
segments,
Members::Entries(&inner.entries),
inner.ordered,
&inner.root_name,
&inner.tables,
true,
);
GroupResolution::new(resolved)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UnplacedReason {
OutOfOrder,
NotHere,
Unknown,
}
impl UnplacedReason {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::OutOfOrder => "out_of_order",
Self::NotHere => "not_here",
Self::Unknown => "unknown",
}
}
}
impl std::fmt::Display for UnplacedReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::OutOfOrder => "out of order: it fits a place already passed",
Self::NotHere => "it fits a place elsewhere in the message, not here",
Self::Unknown => "it fits no place in the schema",
})
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Unplaced {
pub index: usize,
pub reason: UnplacedReason,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub struct Placement<'s> {
pub place: &'s SegmentPlace,
pub group: Option<&'s GroupDef>,
pub occurrence: u32,
}
#[derive(Debug)]
pub struct GroupResolution<'s> {
tree: SegmentGroupIndexed<'s>,
nodes: Vec<NodeInfo<'s>>,
slots: Vec<Slot<'s>>,
unplaced: Vec<Unplaced>,
overflows: Vec<Overflow<'s>>,
}
#[derive(Debug)]
struct NodeInfo<'s> {
def: Option<&'s GroupDef>,
name: &'s str,
parent: Option<usize>,
occurrence: usize,
members: Members<'s>,
first: usize,
counts: SmallVec<[u32; 8]>,
}
impl<'s> GroupResolution<'s> {
fn new(resolved: Resolved<'s>) -> Self {
let Resolved {
nodes,
slots,
unplaced,
overflows,
} = resolved;
let mut infos = Vec::with_capacity(nodes.len());
let mut children = Vec::with_capacity(nodes.len());
let mut trees: Vec<Option<SegmentGroupIndexed<'s>>> = Vec::with_capacity(nodes.len());
for mut node in nodes {
children.push(std::mem::take(&mut node.children));
infos.push(NodeInfo {
def: node.def,
name: node.name,
parent: node.parent,
occurrence: node.occurrence,
members: node.members,
first: node.first,
counts: node.counts.clone(),
});
trees.push(Some(node.into_indexed()));
}
let tree = assemble(trees, &children);
Self {
tree,
nodes: infos,
slots,
unplaced,
overflows,
}
}
#[must_use]
pub fn tree(&self) -> &SegmentGroupIndexed<'s> {
&self.tree
}
#[must_use]
pub fn into_tree(self) -> SegmentGroupIndexed<'s> {
self.tree
}
#[must_use]
pub fn placement(&self, index: usize) -> Option<Placement<'s>> {
let slot = self.slots.get(index)?;
let place = slot.place?;
Some(Placement {
place,
group: self.nodes[slot.instance].def,
occurrence: slot.occurrence,
})
}
#[must_use]
pub fn unplaced(&self) -> &[Unplaced] {
&self.unplaced
}
#[must_use]
pub fn group_path(&self, index: usize) -> Option<GroupPath> {
self.instance_path(self.slots.get(index)?.instance)
}
fn instance_path(&self, mut instance: usize) -> Option<GroupPath> {
let mut steps = Vec::new();
while let Some(parent) = self.nodes[instance].parent {
let node = &self.nodes[instance];
steps.push(GroupStep::new(node.name, node.occurrence)?);
instance = parent;
}
steps.reverse();
GroupPath::new(steps)
}
#[must_use]
pub fn findings(&self, segments: &[Segment<'_>]) -> Vec<ValidationIssue> {
let mut keyed: Vec<(usize, ValidationIssue)> = Vec::new();
let positions = crate::validator::MessagePositions::new(segments);
let finish = |issue: ValidationIssue,
index: Option<usize>,
path: Option<GroupPath>,
id: Option<&str>| {
let mut issue = issue.with_layer(ValidationLayer::Structure);
if let Some(path) = path {
issue = issue.with_group_path(path);
}
if let Some(position) = index.and_then(|i| positions.position(i)) {
issue = issue.with_segment_position(position);
}
if let Some(id) = id {
issue = issue.with_context_entry("place", id);
}
issue
};
for unplaced in &self.unplaced {
let Some(segment) = segments.get(unplaced.index) else {
continue;
};
let err = EdifactError::SegmentNotPlaced {
tag: segment.tag().to_owned(),
reason: unplaced.reason,
span: segment.span,
};
let path = self.group_path(unplaced.index);
keyed.push((
unplaced.index,
finish(
crate::validator::issue_from_error(err),
Some(unplaced.index),
path,
None,
),
));
}
for overflow in &self.overflows {
let Some(segment) = segments.get(overflow.index) else {
continue;
};
let (err, id) = match overflow.cand {
Cand::Seg(place) => (
EdifactError::TooManySegmentOccurrences {
tag: segment.tag().to_owned(),
max: place.max.unwrap_or(u32::MAX),
span: segment.span,
},
place.id(),
),
Cand::Group(group) => (
EdifactError::TooManyGroupOccurrences {
group: group.name().to_owned(),
max: group.max.unwrap_or(u32::MAX),
span: segment.span,
},
group.id(),
),
};
let path = self.instance_path(overflow.instance);
keyed.push((
overflow.index,
finish(
crate::validator::issue_from_error(err),
Some(overflow.index),
path,
id,
),
));
}
for (instance, node) in self.nodes.iter().enumerate() {
for j in node.first..node.members.len() {
let cand = node.members.get(j);
let (min, tag, id) = match cand {
Cand::Seg(place) => (place.min, place.tag(), place.id()),
Cand::Group(group) => (group.min, group.trigger_tag(), group.id()),
};
if node.counts.get(j).copied().unwrap_or(0) >= min {
continue;
}
let path = self.instance_path(instance);
let err = EdifactError::MissingSegment {
tag: tag.to_owned(),
expected_position: path
.as_ref()
.map_or_else(|| node.name.to_owned(), ToString::to_string),
};
keyed.push((
usize::MAX,
finish(crate::validator::issue_from_error(err), None, path, id),
));
}
}
keyed.sort_by_key(|(key, _)| *key);
keyed.into_iter().map(|(_, issue)| issue).collect()
}
}
#[derive(Debug, Clone, Copy)]
enum Members<'g> {
Groups(&'g [GroupDef]),
Entries(&'g [Entry]),
}
impl<'g> Members<'g> {
fn len(self) -> usize {
match self {
Self::Groups(g) => g.len(),
Self::Entries(e) => e.len(),
}
}
fn get(self, j: usize) -> Cand<'g> {
match self {
Self::Groups(g) => Cand::Group(&g[j]),
Self::Entries(e) => match &e[j] {
Entry::Segment(place) => Cand::Seg(place),
Entry::Group(group) => Cand::Group(group),
},
}
}
}
#[derive(Debug, Clone, Copy)]
enum Cand<'g> {
Seg(&'g SegmentPlace),
Group(&'g GroupDef),
}
impl Cand<'_> {
fn matches(self, segment: &Segment<'_>, tables: &Tables) -> bool {
match self {
Self::Seg(place) => place.matches(segment, tables),
Self::Group(group) => group.trigger.matches(segment, tables),
}
}
fn max(self) -> Option<u32> {
match self {
Self::Seg(place) => place.max,
Self::Group(group) => group.max,
}
}
}
#[derive(Debug, Default)]
struct Tables {
by_tag: HashMap<Box<str>, TagInfo>,
}
#[derive(Debug, Default)]
struct TagInfo {
places: Vec<SegmentPlace>,
claimed: Vec<(u16, u16, Arc<str>)>,
}
impl Tables {
fn build(root: Members<'_>) -> Self {
let mut tables = Self::default();
let mut stack: Vec<(Members<'_>, usize, usize)> = vec![(root, 0, 0)];
while let Some((members, from, depth)) = stack.pop() {
for j in from..members.len() {
match members.get(j) {
Cand::Seg(place) => tables.add(place),
Cand::Group(group) => {
tables.add(&group.trigger);
if depth < MAX_DEPTH {
let (inner, ordered) = group.members();
stack.push((inner, usize::from(ordered), depth + 1));
}
}
}
}
}
tables
}
fn add(&mut self, place: &SegmentPlace) {
let info = self.by_tag.entry(place.tag().into()).or_default();
for d in place.discriminators() {
if let Rule::OneOf { codes, .. } = &d.rule {
for code in codes.iter() {
info.claimed
.push((d.element, d.component, Arc::clone(code)));
}
}
}
info.places.push(place.clone());
}
fn claimed(&self, tag: &str, element: u16, component: u16, value: &str) -> bool {
self.by_tag.get(tag).is_some_and(|info| {
info.claimed
.iter()
.any(|(e, c, code)| *e == element && *c == component && &**code == value)
})
}
fn any_place_matches(&self, segment: &Segment<'_>) -> bool {
self.by_tag
.get(segment.tag())
.is_some_and(|info| info.places.iter().any(|p| p.matches(segment, self)))
}
}
#[derive(Debug)]
struct Node<'g> {
def: Option<&'g GroupDef>,
name: &'g str,
parent: Option<usize>,
start: usize,
end: usize,
occurrence: usize,
children: Vec<usize>,
unplaced: Vec<usize>,
names: SmallVec<[(&'g str, usize); 4]>,
members: Members<'g>,
ordered: bool,
first: usize,
cursor: usize,
counts: SmallVec<[u32; 8]>,
}
impl<'g> Node<'g> {
fn new(
def: Option<&'g GroupDef>,
name: &'g str,
parent: Option<usize>,
start: usize,
occurrence: usize,
(members, ordered): (Members<'g>, bool),
) -> Self {
let first = usize::from(ordered && def.is_some());
let mut counts: SmallVec<[u32; 8]> = SmallVec::from_elem(0, members.len());
if first == 1 {
counts[0] = 1;
}
Self {
def,
name,
parent,
start,
end: start,
occurrence,
children: Vec::new(),
unplaced: Vec::new(),
names: SmallVec::new(),
members,
ordered,
first,
cursor: 0,
counts,
}
}
fn candidate(&self, segment: &Segment<'_>, tables: &Tables) -> Option<usize> {
if !self.ordered {
return (0..self.members.len()).find(|&j| {
step();
matches!(self.members.get(j), Cand::Group(_))
&& self.members.get(j).matches(segment, tables)
});
}
let mut full = None;
for j in self.cursor.max(self.first)..self.members.len() {
step();
let cand = self.members.get(j);
if !cand.matches(segment, tables) {
continue;
}
if cand.max().is_some_and(|max| self.counts[j] >= max) {
full = full.or(Some(j));
continue;
}
return Some(j);
}
full
}
fn passed(&self, segment: &Segment<'_>, tables: &Tables) -> bool {
self.ordered
&& (0..self.cursor.max(self.first)).any(|j| {
step();
self.members.get(j).matches(segment, tables)
})
}
fn into_indexed(self) -> SegmentGroupIndexed<'g> {
SegmentGroupIndexed {
definition: self.name,
def: self.def,
total_span: self.start..self.end,
children: Vec::new(),
occurrence_index: self.occurrence,
unplaced: self.unplaced,
}
}
}
#[derive(Debug, Clone, Copy)]
struct Slot<'g> {
instance: usize,
place: Option<&'g SegmentPlace>,
occurrence: u32,
}
#[derive(Debug, Clone, Copy)]
struct Overflow<'g> {
index: usize,
instance: usize,
cand: Cand<'g>,
}
#[derive(Debug)]
struct Resolved<'g> {
nodes: Vec<Node<'g>>,
slots: Vec<Slot<'g>>,
unplaced: Vec<Unplaced>,
overflows: Vec<Overflow<'g>>,
}
impl<'g> Resolved<'g> {
fn into_tree(self) -> SegmentGroupIndexed<'g> {
let mut children = Vec::with_capacity(self.nodes.len());
let mut trees = Vec::with_capacity(self.nodes.len());
for mut node in self.nodes {
children.push(std::mem::take(&mut node.children));
trees.push(Some(node.into_indexed()));
}
assemble(trees, &children)
}
}
fn assemble<'g>(
mut trees: Vec<Option<SegmentGroupIndexed<'g>>>,
children: &[Vec<usize>],
) -> SegmentGroupIndexed<'g> {
for i in (1..trees.len()).rev() {
let kids: Vec<SegmentGroupIndexed<'g>> = children[i]
.iter()
.filter_map(|&c| trees[c].take())
.collect();
if let Some(node) = trees[i].as_mut() {
node.children = kids;
}
}
let kids: Vec<SegmentGroupIndexed<'g>> = children
.first()
.map(|c| c.iter().filter_map(|&c| trees[c].take()).collect())
.unwrap_or_default();
let mut root = trees
.first_mut()
.and_then(Option::take)
.expect("the root instance always exists");
root.children = kids;
root
}
fn resolve<'g>(
segments: &[Segment<'_>],
root: Members<'g>,
root_ordered: bool,
root_name: &'g str,
tables: &Tables,
track: bool,
) -> Resolved<'g> {
let mut nodes: Vec<Node<'g>> =
vec![Node::new(None, root_name, None, 0, 0, (root, root_ordered))];
let mut stack: Vec<usize> = vec![0];
let mut slots: Vec<Slot<'g>> = Vec::with_capacity(if track { segments.len() } else { 0 });
let mut unplaced: Vec<Unplaced> = Vec::new();
let mut overflows: Vec<Overflow<'g>> = Vec::new();
for (i, segment) in segments.iter().enumerate() {
let found = stack.iter().enumerate().rev().find_map(|(depth, &node)| {
step();
nodes[node]
.candidate(segment, tables)
.map(|j| (depth, node, j))
});
let slot = if let Some((depth, node, j)) = found {
while stack.len() > depth + 1 {
if let Some(closed) = stack.pop() {
nodes[closed].end = i;
}
}
let level = &mut nodes[node];
level.cursor = j;
level.counts[j] = level.counts[j].saturating_add(1);
let count = level.counts[j];
let cand = level.members.get(j);
let over = cand.max().is_some_and(|max| count > max);
match cand {
Cand::Seg(place) => {
if over {
overflows.push(Overflow {
index: i,
instance: node,
cand,
});
}
Slot {
instance: node,
place: Some(place),
occurrence: count - 1,
}
}
Cand::Group(group) => {
let name = group.name();
let occurrence = match level.names.iter_mut().find(|(n, _)| *n == name) {
Some((_, c)) => {
*c += 1;
*c - 1
}
None => {
level.names.push((name, 1));
0
}
};
let child = nodes.len();
nodes[node].children.push(child);
nodes.push(Node::new(
Some(group),
name,
Some(node),
i,
occurrence,
group.members(),
));
stack.push(child);
if over {
overflows.push(Overflow {
index: i,
instance: child,
cand,
});
}
Slot {
instance: child,
place: Some(&group.trigger),
occurrence: 0,
}
}
}
} else {
let node = *stack.last().expect("the root is never popped");
if nodes[node].ordered {
let reason = if stack.iter().any(|&n| nodes[n].passed(segment, tables)) {
UnplacedReason::OutOfOrder
} else if tables.any_place_matches(segment) {
UnplacedReason::NotHere
} else {
UnplacedReason::Unknown
};
nodes[node].unplaced.push(i);
unplaced.push(Unplaced { index: i, reason });
}
Slot {
instance: node,
place: None,
occurrence: 0,
}
};
if track {
slots.push(slot);
}
}
let end = segments.len();
for node in stack {
nodes[node].end = end;
}
Resolved {
nodes,
slots,
unplaced,
overflows,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::Element;
fn seg(tag: &'static str) -> Segment<'static> {
Segment::new(tag, vec![Element::of(&["x"])])
}
static SG2: &[GroupDef] = &[GroupDef::new("SG2", "CTA")];
static SCHEMA: &[GroupDef] = &[
GroupDef::with_children("SG1", "NAD", SG2),
GroupDef::new("SG3", "LIN"),
];
static SG12: &[GroupDef] = &[GroupDef::new("SG12", "NAD")];
static NESTED_SAME_TRIGGER: &[GroupDef] = &[
GroupDef::new("SG2", "NAD"),
GroupDef::with_children("SG4", "IDE", SG12),
];
fn placement_steps(n: usize) -> (u64, u64) {
let sg1 = GroupDef::builder("SG1", SegmentPlace::new("NAD"))
.segment(SegmentPlace::new("CTA"))
.build()
.expect("valid group");
let schema = GroupSchema::ordered(
"ROOT",
[
Entry::Segment(SegmentPlace::new("UNH")),
Entry::Group(sg1),
Entry::Segment(SegmentPlace::new("UNT")),
],
)
.expect("valid schema");
let mut segments = vec![seg("UNH")];
for _ in 0..n {
segments.extend([seg("NAD"), seg("CTA")]);
}
segments.push(seg("UNT"));
let reset = || STEPS.with(|steps| steps.replace(0));
reset();
let resolution = schema.resolve(&segments);
let resolve = reset();
let direct = resolution.tree().direct_segment_indices().count();
assert_eq!(direct, 2, "UNH and UNT");
(resolve, reset())
}
#[test]
fn placement_work_is_linear() {
let counts: Vec<_> = [10_000, 20_000, 40_000]
.into_iter()
.map(placement_steps)
.collect();
for pair in counts.windows(2) {
let ((r1, d1), (r2, d2)) = (pair[0], pair[1]);
assert!(r1 > 0 && d1 > 0, "steps are counted");
assert!(r2 * 10 <= r1 * 22, "resolve: {r1} -> {r2} steps");
assert!(d2 * 10 <= d1 * 22, "direct walk: {d1} -> {d2} steps");
}
}
#[test]
fn a_nested_group_wins_over_an_ancestors_sibling_with_the_same_trigger() {
let segs = vec![
seg("UNH"),
seg("BGM"),
seg("NAD"),
seg("NAD"),
seg("IDE"),
seg("NAD"),
seg("DTM"),
seg("NAD"),
];
let tree = group_segments_indexed(&segs, NESTED_SAME_TRIGGER, "ROOT");
let top: Vec<&str> = tree.children.iter().map(|c| c.definition).collect();
assert_eq!(
top,
["SG2", "SG2", "SG4"],
"the two message-level NADs are SG2"
);
let sg4 = tree
.children
.iter()
.find(|c| c.definition == "SG4")
.expect("SG4 opens on IDE");
assert_eq!(sg4.total_span, 4..8);
let nested: Vec<&str> = sg4.children.iter().map(|c| c.definition).collect();
assert_eq!(
nested,
["SG12", "SG12"],
"NAD inside SG4 nests as SG12 rather than reopening SG2",
);
assert_eq!(sg4.children[0].total_span, 5..7); assert_eq!(sg4.children[1].total_span, 7..8); assert_eq!(sg4.children[0].occurrence_index, 0);
assert_eq!(sg4.children[1].occurrence_index, 1);
}
#[test]
fn descendants_walk_the_whole_subtree_in_document_order() {
let segs = vec![
seg("NAD"),
seg("IDE"),
seg("NAD"),
seg("DTM"),
seg("IDE"),
seg("NAD"),
];
let tree = group_segments_indexed(&segs, NESTED_SAME_TRIGGER, "ROOT");
let walked: Vec<&str> = tree.descendants().map(|g| g.definition).collect();
assert_eq!(
walked,
["ROOT", "SG2", "SG4", "SG12", "SG4", "SG12"],
"pre-order: a parent before its children, siblings in wire order",
);
let sg12: Vec<usize> = tree.find("SG12").map(|g| g.total_span.start).collect();
assert_eq!(sg12, [2, 5]);
let first = tree.find("SG12").next().unwrap();
assert_eq!(
first
.segments(&segs)
.iter()
.map(Segment::tag)
.collect::<Vec<_>>(),
["NAD", "DTM"],
);
}
#[test]
fn a_repeated_trigger_still_reopens_a_sibling_rather_than_nesting_forever() {
let segs = vec![seg("IDE"), seg("NAD"), seg("NAD"), seg("NAD")];
let tree = group_segments_indexed(&segs, NESTED_SAME_TRIGGER, "ROOT");
let sg4 = &tree.children[0];
assert_eq!(sg4.definition, "SG4");
assert_eq!(
sg4.children.len(),
3,
"three sibling SG12s, not one nest of three"
);
assert!(
sg4.children.iter().all(|c| c.children.is_empty()),
"SG12 has no children, so nothing may nest inside it",
);
}
#[test]
fn a_tag_no_nested_definition_accepts_still_closes_the_group() {
let segs = vec![seg("IDE"), seg("NAD"), seg("IDE"), seg("NAD")];
let tree = group_segments_indexed(&segs, NESTED_SAME_TRIGGER, "ROOT");
let top: Vec<&str> = tree.children.iter().map(|c| c.definition).collect();
assert_eq!(top, ["SG4", "SG4"]);
assert_eq!(tree.children[0].total_span, 0..2);
assert_eq!(tree.children[1].total_span, 2..4);
}
#[test]
fn nesting_is_preferred_at_every_depth() {
static L3: &[GroupDef] = &[GroupDef::new("L3", "CTA")];
static L2: &[GroupDef] = &[GroupDef::with_children("L2", "NAD", L3)];
static DEEP: &[GroupDef] = &[
GroupDef::new("L1", "NAD"),
GroupDef::with_children("A", "IDE", L2),
];
let segs = vec![seg("NAD"), seg("IDE"), seg("NAD"), seg("CTA")];
let tree = group_segments_indexed(&segs, DEEP, "ROOT");
assert_eq!(tree.children[0].definition, "L1");
let a = &tree.children[1];
assert_eq!(a.definition, "A");
assert_eq!(a.children[0].definition, "L2");
assert_eq!(a.children[0].children[0].definition, "L3");
}
#[test]
fn root_segments_before_first_trigger() {
let segs = vec![seg("UNH"), seg("BGM"), seg("NAD")];
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let direct: Vec<_> = tree.direct_segment_indices().collect();
assert_eq!(direct, vec![0, 1], "UNH + BGM should be direct in root");
assert_eq!(tree.children.len(), 1);
assert_eq!(tree.children[0].definition, "SG1");
}
#[test]
fn repeated_trigger_creates_multiple_children() {
let segs = vec![seg("UNH"), seg("NAD"), seg("NAD"), seg("UNT")];
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
assert_eq!(
tree.children
.iter()
.filter(|c| c.definition == "SG1")
.count(),
2
);
}
#[test]
fn repeated_trigger_occurrence_index_is_stamped() {
let segs = vec![seg("NAD"), seg("NAD"), seg("NAD")];
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let indices: Vec<_> = tree.children.iter().map(|c| c.occurrence_index).collect();
assert_eq!(indices, vec![0, 1, 2]);
}
#[test]
fn nested_child_groups() {
let segs = vec![seg("NAD"), seg("CTA"), seg("CTA")];
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let sg1 = &tree.children[0];
assert_eq!(sg1.definition, "SG1");
assert_eq!(sg1.children.len(), 2);
assert!(sg1.children.iter().all(|c| c.definition == "SG2"));
}
#[test]
fn total_span_covers_all_segments() {
let segs = vec![seg("UNH"), seg("NAD"), seg("CTA")];
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let all_tags: Vec<_> = segs[tree.total_span.clone()]
.iter()
.map(|s| s.tag())
.collect();
assert!(all_tags.contains(&"UNH"));
assert!(all_tags.contains(&"NAD"));
assert!(all_tags.contains(&"CTA"));
}
fn parse(input: &'static str) -> Vec<Segment<'static>> {
crate::from_bytes(input.as_bytes())
.collect::<Result<_, _>>()
.expect("fixture parses")
}
fn shape<'g>(node: &SegmentGroupIndexed<'g>) -> Vec<(&'g str, usize, Range<usize>)> {
node.children
.iter()
.map(|c| (c.definition, c.occurrence_index, c.total_span.clone()))
.collect()
}
fn place(tag: &'static str, code: &'static str) -> SegmentPlace {
SegmentPlace::new(tag).when(Discriminator::one_of(0, 0, [code]))
}
fn only(name: &str, trigger: SegmentPlace) -> GroupDef {
GroupDef::builder(name, trigger).ordered().build().unwrap()
}
const SHARED_RFF_BODY: &str = "IDE+24+X'RFF+Z13:1'SEQ+Z01'RFF+Z19:ABC'CCI+Z30'SEQ+Z02'QTY+1:1'";
#[test]
fn shared_rff_with_members() {
let sg8 = GroupDef::builder("SG8", SegmentPlace::new("SEQ"))
.segment(SegmentPlace::new("RFF"))
.segment(SegmentPlace::new("CCI"))
.segment(SegmentPlace::new("QTY"))
.build()
.unwrap();
let sg4 = GroupDef::builder("SG4", SegmentPlace::new("IDE"))
.group(GroupDef::new("SG6", "RFF"))
.group(sg8)
.build()
.unwrap();
assert!(!sg4.is_ordered(), "SG4 declares no member segment: open");
let schema = [sg4];
let segs = parse(SHARED_RFF_BODY);
let tree = group_segments_indexed(&segs, &schema, "ROOT");
assert_eq!(shape(&tree), [("SG4", 0, 0..7)]);
assert_eq!(
shape(&tree.children[0]),
[("SG6", 0, 1..2), ("SG8", 0, 2..5), ("SG8", 1, 5..7)],
"RFF+Z19 and CCI+Z30 belong to SG8#0",
);
assert!(tree.descendants().all(|g| g.unplaced.is_empty()));
assert!(
tree.children[0].children[1]
.def
.is_some_and(GroupDef::is_ordered)
);
}
#[test]
fn shared_rff_open_schema() {
static SG4: &[GroupDef] = &[GroupDef::new("SG6", "RFF"), GroupDef::new("SG8", "SEQ")];
static SCHEMA: &[GroupDef] = &[GroupDef::with_children("SG4", "IDE", SG4)];
let segs = parse(SHARED_RFF_BODY);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
assert_eq!(
shape(&tree.children[0]),
[
("SG6", 0, 1..2),
("SG8", 0, 2..3),
("SG6", 1, 3..5),
("SG8", 1, 5..7)
],
);
}
fn utilmd_sg4() -> GroupDef {
let sg8 = GroupDef::builder("SG8", SegmentPlace::new("SEQ"))
.segment(SegmentPlace::new("RFF"))
.segment(SegmentPlace::new("CCI"))
.build()
.unwrap();
GroupDef::builder("SG4", SegmentPlace::new("IDE"))
.group(only("SG5", place("LOC", "Z16")))
.group(only("SG5", place("LOC", "Z17")))
.group(GroupDef::new("SG6", "RFF"))
.group(sg8)
.ordered()
.build()
.unwrap()
}
#[test]
fn sg6_sg8_rff_ordered() {
let schema = GroupSchema::new("ROOT", [utilmd_sg4()]).unwrap();
let segs = parse(
"IDE+24+X'LOC+Z16+1'LOC+Z17+2'RFF+Z13:1'SEQ+Z01'RFF+Z18:1'CCI+Z30'SEQ+Z02'RFF+Z19:2'",
);
let resolution = schema.resolve(&segs);
let path = |i: usize| resolution.group_path(i).map(|p| p.to_string());
assert!(resolution.unplaced().is_empty());
assert_eq!(path(1).as_deref(), Some("SG4[0]/SG5[0]"));
assert_eq!(path(2).as_deref(), Some("SG4[0]/SG5[1]"));
assert_eq!(path(3).as_deref(), Some("SG4[0]/SG6[0]"), "RFF+Z13 is SG6");
assert_eq!(
path(5).as_deref(),
Some("SG4[0]/SG8[0]"),
"RFF after SEQ is SG8"
);
assert_eq!(path(6).as_deref(), Some("SG4[0]/SG8[0]"));
assert_eq!(path(8).as_deref(), Some("SG4[0]/SG8[1]"));
assert_eq!(path(0).as_deref(), Some("SG4[0]"));
}
fn sg5_places(with_dtm: bool) -> GroupSchema {
let mut sg4 = GroupDef::builder("SG4", SegmentPlace::new("IDE"))
.group(only("SG5", place("LOC", "Z16").with_id("00047")))
.group(only("SG5", place("LOC", "Z17").with_id("00048")))
.ordered();
if with_dtm {
sg4 = sg4.segment(SegmentPlace::new("DTM"));
}
GroupSchema::new("ROOT", [sg4.build().unwrap()]).unwrap()
}
#[test]
fn sg5_places_by_code() {
let schema = sg5_places(false);
let segs = parse("IDE+24+V1'LOC+Z16+1'LOC+Z17+2'");
let resolution = schema.resolve(&segs);
assert!(resolution.unplaced().is_empty());
let first = resolution.placement(1).unwrap();
let second = resolution.placement(2).unwrap();
assert_eq!(first.place.id(), Some("00047"));
assert_eq!(second.place.id(), Some("00048"));
assert_eq!(first.group.map(GroupDef::name), Some("SG5"));
assert_eq!(
resolution.group_path(1).unwrap().to_string(),
"SG4[0]/SG5[0]"
);
assert_eq!(
resolution.group_path(2).unwrap().to_string(),
"SG4[0]/SG5[1]"
);
let schema = sg5_places(true);
let segs = parse("IDE+24+V1'LOC+Z17+2'LOC+Z16+1'DTM+92:20261001:303'");
let resolution = schema.resolve(&segs);
assert_eq!(
resolution.unplaced(),
[Unplaced {
index: 2,
reason: UnplacedReason::OutOfOrder
}]
);
assert!(resolution.placement(2).is_none());
let dtm = resolution.placement(3).expect("DTM is placed");
assert_eq!(dtm.place.tag(), "DTM");
assert_eq!(resolution.group_path(3).unwrap().to_string(), "SG4[0]");
let sg5 = &resolution.tree().children[0].children[0];
assert_eq!(sg5.unplaced, [2]);
}
#[test]
fn same_tag_places_told_apart_by_code() {
let one = |p: SegmentPlace| Entry::Segment(p.with_max(1));
let dtm = |code: &'static str| {
SegmentPlace::new("DTM")
.when(Discriminator::one_of(0, 0, [code]))
.with_max(1)
};
let sg4 = GroupDef::builder("SG4", place("IDE", "24").with_id("00018"))
.segment(dtm("92").with_id("00020"))
.segment(dtm("93").with_id("00021"))
.group(only("SG5", place("LOC", "Z16").with_id("00047")))
.group(only("SG5", place("LOC", "Z22").with_id("00048")))
.max(99_999)
.build()
.unwrap();
let schema = GroupSchema::ordered(
"UTILMD",
[
one(SegmentPlace::new("UNH").with_id("00003")),
one(SegmentPlace::new("BGM").with_id("00004")),
Entry::Group(only("SG2", place("NAD", "MS").with_id("00008"))),
Entry::Group(only("SG2", place("NAD", "MR").with_id("00009"))),
Entry::Group(sg4),
one(SegmentPlace::new("UNT").with_id("00493")),
],
)
.unwrap();
let segs = parse(
"UNH+1+UTILMD:D:11A:UN:S2.2'BGM+E01+1'NAD+MS+1'NAD+MR+2'IDE+24+V1'DTM+93:20261001:303'LOC+Z22+1'LOC+Z16+2'UNT+8+1'",
);
let resolution = schema.resolve(&segs);
let nr = |i: usize| resolution.placement(i).and_then(|p| p.place.id());
assert_eq!(nr(3), Some("00009"), "NAD+MR is the second SG2");
assert_eq!(nr(5), Some("00021"), "DTM+93 is Ende zum");
assert_eq!(nr(6), Some("00048"), "LOC+Z22 is the ruhende Marktlokation");
assert_eq!(nr(7), None);
assert_eq!(
resolution.unplaced(),
[Unplaced {
index: 7,
reason: UnplacedReason::OutOfOrder
}]
);
assert_eq!(
nr(8),
Some("00493"),
"the UNT after it still finds its place"
);
}
#[test]
fn accept_unclaimed() {
let schema = |accept: bool| {
let z16 = Discriminator::one_of(0, 0, ["Z16"]);
let z16 = if accept { z16.accept_unclaimed() } else { z16 };
let sg4 = GroupDef::builder("SG4", SegmentPlace::new("IDE"))
.group(only("SG5", SegmentPlace::new("LOC").when(z16)))
.group(only("SG5", place("LOC", "Z17")))
.ordered()
.build()
.unwrap();
GroupSchema::new("ROOT", [sg4]).unwrap()
};
let segs = parse("IDE+24+V1'LOC+Z99+1'LOC+Z17+2'");
let lenient = schema(true);
let resolution = lenient.resolve(&segs);
assert!(
resolution.unplaced().is_empty(),
"Z99 is claimed by no place"
);
assert_eq!(
resolution.group_path(1).unwrap().to_string(),
"SG4[0]/SG5[0]"
);
assert_eq!(
resolution.group_path(2).unwrap().to_string(),
"SG4[0]/SG5[1]"
);
let segs_z17 = parse("IDE+24+V1'LOC+Z17+2'");
let resolution = lenient.resolve(&segs_z17);
assert_eq!(
resolution.placement(1).unwrap().place.discriminators()[0].codes()[0].as_ref(),
"Z17"
);
let strict = schema(false);
let resolution = strict.resolve(&segs);
assert_eq!(
resolution.unplaced(),
[Unplaced {
index: 1,
reason: UnplacedReason::Unknown
}]
);
}
#[test]
fn optional_and_not_used_discriminators() {
let sg4 = GroupDef::builder("SG4", SegmentPlace::new("IDE"))
.segment(
SegmentPlace::new("CCI")
.with_id("plain")
.when(Discriminator::not_used(0, 0)),
)
.segment(
SegmentPlace::new("CCI")
.with_id("coded")
.when(Discriminator::one_of(0, 0, ["Z18"]).optional()),
)
.build()
.unwrap();
let schema = GroupSchema::new("ROOT", [sg4]).unwrap();
let segs = parse("IDE+24'CCI+++ZB3'CCI+Z18'");
let resolution = schema.resolve(&segs);
let id = |i: usize| resolution.placement(i).and_then(|p| p.place.id());
assert_eq!(
id(1),
Some("plain"),
"nothing at element 0: not_used matches"
);
assert_eq!(
id(2),
Some("coded"),
"a value at element 0 rules out not_used"
);
assert!(resolution.unplaced().is_empty());
}
#[test]
fn a_place_at_max_yields_to_a_later_matching_entry() {
let sg4 = GroupDef::builder("SG4", SegmentPlace::new("IDE"))
.segment(SegmentPlace::new("DTM").with_id("first").with_max(1))
.segment(SegmentPlace::new("DTM").with_id("second").with_max(1))
.build()
.unwrap();
let schema = GroupSchema::new("ROOT", [sg4]).unwrap();
let segs = parse("IDE+24'DTM+1'DTM+2'DTM+3'");
let resolution = schema.resolve(&segs);
let id = |i: usize| resolution.placement(i).and_then(|p| p.place.id());
assert_eq!(
(id(1), id(2), id(3)),
(Some("first"), Some("second"), Some("second"))
);
let counts: Vec<_> = resolution
.findings(&segs)
.into_iter()
.map(|f| f.error_code().map(str::to_owned))
.collect();
assert_eq!(counts, [Some("E069".to_owned())]);
}
#[test]
fn own_trigger_opens_the_next_occurrence() {
let sg8 = GroupDef::builder("SG8", SegmentPlace::new("SEQ"))
.segment(SegmentPlace::new("QTY"))
.build()
.unwrap();
let schema = [sg8];
let segs = parse("SEQ+1'QTY+1'SEQ+2'SEQ+3'QTY+2'");
let tree = group_segments_indexed(&segs, &schema, "ROOT");
assert_eq!(
shape(&tree),
[("SG8", 0, 0..2), ("SG8", 1, 2..3), ("SG8", 2, 3..5)]
);
}
#[test]
fn unplaced_segments_are_not_direct_segments() {
let sg1 = GroupDef::builder("SG1", SegmentPlace::new("AAA"))
.segment(SegmentPlace::new("BBB"))
.build()
.unwrap();
let schema = [sg1];
let segs = parse("AAA+1'CCC+1'BBB+1'");
let tree = group_segments_indexed(&segs, &schema, "ROOT");
let sg1 = &tree.children[0];
assert_eq!(sg1.total_span, 0..3);
assert_eq!(sg1.unplaced, [1]);
assert_eq!(sg1.direct_segment_indices().collect::<Vec<_>>(), [0, 2]);
}
#[test]
fn empty_input_is_an_empty_root() {
let tree = group_segments_indexed(&[], SCHEMA, "ROOT");
assert_eq!(tree.total_span, 0..0);
assert!(tree.children.is_empty() && tree.unplaced.is_empty());
let schema =
GroupSchema::ordered("ROOT", [Entry::Segment(SegmentPlace::new("UNH"))]).unwrap();
assert!(schema.resolve(&[]).unplaced().is_empty());
}
#[test]
fn owned_schema_from_strings() {
struct Profile {
groups: Vec<GroupDef>,
}
fn load(names: &[String]) -> Profile {
let sg6 = GroupDef::builder(names[4].as_str(), SegmentPlace::owned(names[5].as_str()))
.build()
.unwrap();
Profile {
groups: vec![
GroupDef::builder(names[0].clone(), SegmentPlace::owned(names[1].clone()))
.build()
.unwrap(),
GroupDef::builder(names[2].clone(), SegmentPlace::owned(names[3].clone()))
.group(sg6)
.build()
.unwrap(),
],
}
}
let names: Vec<String> = ["SG1", "RFF", "SG5", "LOC", "SG6", "QTY"]
.iter()
.map(|s| (*s).to_owned())
.collect();
let profile = load(&names);
drop(names);
let segs = parse("RFF+ON:1'LOC+172+DE1'DTM+163:20260101:102'QTY+220:5'");
let tree = group_segments_indexed(&segs, &profile.groups, "ROOT");
assert_eq!(shape(&tree), [("SG1", 0, 0..1), ("SG5", 0, 1..4)]);
assert_eq!(shape(&tree.children[1]), [("SG6", 0, 3..4)]);
}
fn reason(result: Result<GroupDef, EdifactError>) -> &'static str {
match result {
Err(EdifactError::InvalidGroupSchema { reason, .. }) => reason,
other => panic!("expected InvalidGroupSchema, got {other:?}"),
}
}
#[test]
fn invalid_group_schemas_are_refused() {
let b = || GroupDef::builder("SG1", SegmentPlace::new("NAD"));
assert_eq!(reason(b().min(2).max(1).build()), "min exceeds max");
assert_eq!(reason(b().max(0).build()), "max is 0");
assert_eq!(
reason(b().segment(SegmentPlace::new("CT")).build()),
"segment tag is not three upper-case ASCII letters"
);
assert_eq!(
reason(
b().segment(SegmentPlace::new("CTA").with_min(3).with_max(2))
.build()
),
"min exceeds max"
);
assert_eq!(
reason(GroupDef::builder("SG1", SegmentPlace::new("nad")).build()),
"segment tag is not three upper-case ASCII letters"
);
assert_eq!(
reason(
b().segment(SegmentPlace::new("CTA").when(Discriminator::one_of(
0,
0,
Vec::<&str>::new()
)))
.build()
),
"one_of discriminator admits no code"
);
assert_eq!(
reason(GroupDef::builder("", SegmentPlace::new("NAD")).build()),
"group name is empty"
);
let mut group = GroupDef::builder("G", SegmentPlace::new("AAA"))
.build()
.unwrap();
for _ in 1..64 {
group = GroupDef::builder("G", SegmentPlace::new("AAA"))
.group(group)
.build()
.unwrap();
}
assert_eq!(
reason(
GroupDef::builder("G", SegmentPlace::new("AAA"))
.group(group)
.build()
),
"groups nest deeper than 64 levels"
);
static BAD: &[GroupDef] = &[GroupDef::new("SG1", "nad")];
let err = GroupSchema::new("ROOT", BAD.iter().cloned()).unwrap_err();
assert_eq!(err.stable_code(), "E071");
assert!(matches!(
GroupSchema::ordered("", []),
Err(EdifactError::InvalidGroupSchema { .. })
));
}
}