#![allow(clippy::cast_possible_truncation)]
use edifact_rs::{ValidationIssue, ValidationSeverity};
#[inline]
#[allow(dead_code)]
pub(super) fn ahb_check_mandatory(
segments: &[edifact_rs::Segment<'_>],
tag: &str,
rule_id: &str,
msg: &str,
pid: &str,
issues: &mut Vec<ValidationIssue>,
) {
if !segments.iter().any(|s| s.tag == tag) {
issues.push(
ValidationIssue::new(ValidationSeverity::Error, msg.to_owned())
.with_rule_id(rule_id)
.with_segment(tag.to_owned())
.with_context_entry("pid", pid),
);
}
}
#[inline]
#[allow(dead_code)]
pub(super) fn ahb_check_soll(
segments: &[edifact_rs::Segment<'_>],
tag: &str,
rule_id: &str,
msg: &str,
pid: &str,
issues: &mut Vec<ValidationIssue>,
) {
if !segments.iter().any(|s| s.tag == tag) {
issues.push(
ValidationIssue::new(ValidationSeverity::Warning, msg.to_owned())
.with_rule_id(rule_id)
.with_segment(tag.to_owned())
.with_context_entry("pid", pid),
);
}
}
#[inline]
#[allow(dead_code)]
pub(super) fn ahb_check_not_used(
segments: &[edifact_rs::Segment<'_>],
tag: &str,
rule_id: &str,
msg: &str,
pid: &str,
issues: &mut Vec<ValidationIssue>,
) {
if segments.iter().any(|s| s.tag == tag) {
issues.push(
ValidationIssue::new(ValidationSeverity::Error, msg.to_owned())
.with_rule_id(rule_id)
.with_segment(tag.to_owned())
.with_context_entry("pid", pid),
);
}
}
#[inline]
#[allow(dead_code)]
pub(super) fn ahb_check_qualifier(
segments: &[edifact_rs::Segment<'_>],
tag: &str,
rule_id: &str,
msg: &str,
is_allowed: impl Fn(&str) -> bool,
pid: &str,
issues: &mut Vec<ValidationIssue>,
) {
for (occ, seg) in segments.iter().filter(|s| s.tag == tag).enumerate() {
if let Some(q) = seg.element_str(0) {
if !is_allowed(q) {
issues.push(
ValidationIssue::new(ValidationSeverity::Error, msg.to_owned())
.with_rule_id(rule_id)
.with_segment(tag.to_owned())
.with_segment_occurrence(occ as u16)
.with_element_index(0)
.with_component_index(0)
.with_context_entry("pid", pid),
);
}
}
}
}
#[inline]
#[allow(clippy::too_many_arguments)]
#[allow(dead_code)]
pub(super) fn ahb_check_field_value(
segments: &[edifact_rs::Segment<'_>],
tag: &str,
element_index: usize,
rule_id: &str,
msg: &str,
is_allowed: impl Fn(&str) -> bool,
pid: &str,
issues: &mut Vec<ValidationIssue>,
) {
for (occ, seg) in segments.iter().filter(|s| s.tag == tag).enumerate() {
if let Some(v) = seg.element_str(element_index) {
if !is_allowed(v) {
issues.push(
ValidationIssue::new(ValidationSeverity::Error, msg.to_owned())
.with_rule_id(rule_id)
.with_segment(tag.to_owned())
.with_segment_occurrence(occ as u16)
.with_element_index(element_index as u8)
.with_component_index(0)
.with_context_entry("pid", pid),
);
}
}
}
}
#[inline]
#[allow(dead_code)]
pub(super) fn ahb_check_required_qualifier(
segments: &[edifact_rs::Segment<'_>],
tag: &str,
rule_id: &str,
msg: &str,
is_required: impl Fn(&str) -> bool,
pid: &str,
issues: &mut Vec<ValidationIssue>,
) {
let found = segments
.iter()
.filter(|s| s.tag == tag)
.any(|s| s.element_str(0).is_some_and(&is_required));
if !found {
issues.push(
ValidationIssue::new(ValidationSeverity::Error, msg.to_owned())
.with_rule_id(rule_id)
.with_segment(tag.to_owned())
.with_context_entry("pid", pid),
);
}
}
#[inline]
#[allow(dead_code)]
pub(super) fn ahb_check_conditional(
segments: &[edifact_rs::Segment<'_>],
condition_fn: impl Fn(&[edifact_rs::Segment<'_>]) -> bool,
check_fn: impl Fn(&[edifact_rs::Segment<'_>], &mut Vec<ValidationIssue>),
issues: &mut Vec<ValidationIssue>,
) {
if condition_fn(segments) {
check_fn(segments, issues);
}
}