#![cfg(feature = "derive")]
use edifact::{
ComponentRef, EdifactDeserialize, EdifactError, EdifactSerialize, ElementRef,
OwnedComponentRef, OwnedElementRef, OwnedSegmentDef, SegmentDefinition, SegmentLayout, Status,
};
const C082_COMPONENTS: &[ComponentRef] = &[
ComponentRef::new(1, "3039", Status::Mandatory),
ComponentRef::new(2, "1131", Status::Conditional),
ComponentRef::new(3, "3055", Status::Conditional),
];
const NAD_ELEMENTS: &[ElementRef] = &[
ElementRef::new(1, "3035", Status::Mandatory, 1),
ElementRef::composite(2, "C082", Status::Conditional, 1, C082_COMPONENTS),
];
pub const NAD: SegmentDefinition = SegmentDefinition::new("NAD", "Name and address", NAD_ELEMENTS);
const C507_COMPONENTS: &[ComponentRef] = &[
ComponentRef::new(1, "2005", Status::Mandatory),
ComponentRef::new(2, "2380", Status::Conditional),
ComponentRef::new(3, "2379", Status::Conditional),
];
const DTM_ELEMENTS: &[ElementRef] = &[ElementRef::composite(
1,
"C507",
Status::Mandatory,
1,
C507_COMPONENTS,
)];
pub const DTM: SegmentDefinition = SegmentDefinition::new("DTM", "Date/time/period", DTM_ELEMENTS);
const _: () = assert!(NAD.element_slot("3035") == 0);
const _: () = assert!(NAD.element_slot("3055") == 1);
const _: () = assert!(NAD.component_slot("3055") == 2);
const _: () = assert!(NAD.component_slot("C082") == 0);
const _: () = assert!(NAD.code_positions("9999") == 0);
#[derive(Debug, PartialEq, EdifactDeserialize, EdifactSerialize)]
#[edifact(segment = "NAD", qualifier = "MS", layout = NAD)]
struct SenderParty {
#[edifact(element = "3039")]
party_id: String,
#[edifact(element = "3055")]
agency: Option<String>,
}
#[derive(Debug, PartialEq, EdifactDeserialize, EdifactSerialize)]
#[edifact(segment = "DTM", layout = DTM)]
struct DocumentDate {
#[edifact(element = "2005")]
qualifier: String,
#[edifact(element = "2380")]
value: String,
#[edifact(element = "2379")]
format: Option<String>,
}
#[derive(Debug, PartialEq, EdifactDeserialize)]
#[edifact(segment = "NAD", layout = NAD)]
struct RequiredShapes {
#[edifact(element = "3035", required)]
qualifier: Option<String>,
#[edifact(element = "3055", required)]
agency: Option<String>,
}
fn parse(input: &[u8]) -> Vec<edifact::Segment<'_>> {
edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.expect("fixture must parse")
}
#[test]
fn value_by_code_reads_elements_and_composite_components() {
let segments = parse(b"NAD+MS+9900112233445::293'");
let nad = &segments[0];
assert_eq!(nad.value_by_code(&NAD, "3035").unwrap(), Some("MS"));
assert_eq!(
nad.value_by_code(&NAD, "3039").unwrap(),
Some("9900112233445")
);
assert_eq!(nad.value_by_code(&NAD, "1131").unwrap(), Some(""));
assert_eq!(nad.value_by_code(&NAD, "3055").unwrap(), Some("293"));
assert_eq!(
nad.value_by_code(&NAD, "C082").unwrap(),
Some("9900112233445")
);
}
#[test]
fn unknown_data_element_is_an_error_not_a_wrong_read() {
let segments = parse(b"NAD+MS+9900112233445::293'");
let nad = &segments[0];
let err = nad.value_by_code(&NAD, "2380").unwrap_err();
assert!(
matches!(&err, EdifactError::UnknownDataElement { tag, data_element }
if tag == "NAD" && data_element == "2380"),
"expected UnknownDataElement, got {err:?}"
);
assert_eq!(err.stable_code(), "E033");
}
#[test]
fn applying_a_layout_to_the_wrong_segment_is_rejected() {
let segments = parse(b"NAD+MS+9900112233445::293'");
let err = segments[0].value_by_code(&DTM, "2005").unwrap_err();
assert!(
matches!(&err, EdifactError::SegmentLayoutMismatch { expected, actual }
if expected == "DTM" && actual == "NAD"),
"expected SegmentLayoutMismatch, got {err:?}"
);
}
#[test]
fn ambiguous_data_element_is_rejected() {
const REPEATED: &[ElementRef] = &[
ElementRef::new(1, "1153", Status::Mandatory, 1),
ElementRef::new(2, "1153", Status::Conditional, 1),
];
const RFF: SegmentDefinition = SegmentDefinition::new("RFF", "Reference", REPEATED);
let err = RFF.resolve_code("1153").unwrap_err();
assert!(
matches!(&err, EdifactError::AmbiguousDataElement { data_element, .. } if data_element == "1153"),
"expected AmbiguousDataElement, got {err:?}"
);
assert_eq!(err.stable_code(), "E034");
}
#[test]
fn span_by_code_points_at_the_addressed_value() {
let input = b"NAD+MS+9900112233445::293'";
let segments = parse(input);
let span = segments[0]
.span_by_code(&NAD, "3055")
.unwrap()
.expect("DE 3055 is present");
assert_eq!(&input[span.start..span.end], b"293");
}
#[test]
fn owned_and_borrowed_segments_resolve_identically() {
let input = b"NAD+MS+9900112233445::293'";
let owned: Vec<edifact::OwnedSegment> = edifact::from_reader(std::io::Cursor::new(input))
.collect::<Result<Vec<_>, _>>()
.expect("fixture must parse");
assert_eq!(owned[0].value_by_code(&NAD, "3055").unwrap(), Some("293"));
assert_eq!(
owned[0].value_by_code(&NAD, "3039").unwrap(),
Some("9900112233445")
);
assert_eq!(
owned[0].span_by_code(&NAD, "3055").unwrap(),
owned[0].span_by_code(&NAD, "3055").unwrap(),
);
}
#[test]
fn runtime_loaded_definitions_resolve_the_same_codes() {
let def = OwnedSegmentDef::new_unchecked(
"NAD".to_owned(),
"Name and address".to_owned(),
vec![
OwnedElementRef::new_unchecked(1, "3035".to_owned(), Status::Mandatory, 1),
OwnedElementRef::new_unchecked(2, "C082".to_owned(), Status::Conditional, 1)
.with_components(vec![
OwnedComponentRef::new_unchecked(1, "3039".to_owned(), Status::Mandatory),
OwnedComponentRef::new_unchecked(2, "1131".to_owned(), Status::Conditional),
OwnedComponentRef::new_unchecked(3, "3055".to_owned(), Status::Conditional),
]),
],
);
let segments = parse(b"NAD+MS+9900112233445::293'");
assert_eq!(
segments[0].value_by_code(&def, "3055").unwrap(),
Some("293")
);
assert_eq!(
def.resolve_code("3039").unwrap(),
NAD.resolve_code("3039").unwrap()
);
assert!(segments[0].value_by_code(&def, "2380").is_err());
}
#[test]
fn code_addressed_derive_deserializes_from_the_right_slots() {
let segments = parse(b"NAD+MS+9900112233445::293'");
let party = SenderParty::edifact_deserialize(&segments).expect("deserialize");
assert_eq!(party.party_id, "9900112233445");
assert_eq!(party.agency.as_deref(), Some("293"));
}
#[test]
fn code_addressed_derive_round_trips() {
let segments = parse(b"NAD+MS+9900112233445::293'DTM+137:20260101:102'");
let party = SenderParty::edifact_deserialize(&segments).expect("deserialize NAD");
assert_eq!(
edifact::to_edifact_string(&party).expect("serialize NAD"),
"NAD+MS+9900112233445::293'"
);
let date = DocumentDate::edifact_deserialize(&segments).expect("deserialize DTM");
assert_eq!(date.qualifier, "137");
assert_eq!(date.value, "20260101");
assert_eq!(date.format.as_deref(), Some("102"));
assert_eq!(
edifact::to_edifact_string(&date).expect("serialize DTM"),
"DTM+137:20260101:102'"
);
}
#[test]
fn code_addressed_derive_matches_the_owned_path() {
let input = b"NAD+MS+9900112233445::293'";
let owned: Vec<edifact::OwnedSegment> = edifact::from_reader(std::io::Cursor::new(input))
.collect::<Result<Vec<_>, _>>()
.expect("parse");
let borrowed = parse(input);
assert_eq!(
SenderParty::edifact_deserialize(&owned).expect("owned"),
SenderParty::edifact_deserialize(&borrowed).expect("borrowed"),
);
}
#[test]
fn required_reports_the_variant_matching_what_the_field_addresses() {
let missing_qualifier = parse(b"NAD++9900112233445::293'");
let err = RequiredShapes::edifact_deserialize(&missing_qualifier).unwrap_err();
assert!(
matches!(&err, EdifactError::MissingRequiredElement { tag, element_index }
if tag == "NAD" && *element_index == 0),
"DE 3035 names a whole element, expected MissingRequiredElement, got {err:?}"
);
assert_eq!(err.stable_code(), "E008");
let missing_agency = parse(b"NAD+MS+9900112233445'");
let err = RequiredShapes::edifact_deserialize(&missing_agency).unwrap_err();
assert!(
matches!(&err, EdifactError::MissingRequiredComponent { tag, element_index, component_index }
if tag == "NAD" && *element_index == 1 && *component_index == 2),
"DE 3055 names a component, expected MissingRequiredComponent, got {err:?}"
);
assert_eq!(err.stable_code(), "E021");
}
#[test]
fn required_variant_agrees_across_borrowed_and_owned_paths() {
let input = b"NAD++9900112233445::293'";
let owned: Vec<edifact::OwnedSegment> = edifact::from_reader(std::io::Cursor::new(input))
.collect::<Result<Vec<_>, _>>()
.expect("parse");
let borrowed = parse(input);
let from_owned = RequiredShapes::edifact_deserialize(&owned).unwrap_err();
let from_borrowed = RequiredShapes::edifact_deserialize(&borrowed).unwrap_err();
assert_eq!(from_owned.stable_code(), from_borrowed.stable_code());
assert_eq!(from_owned, from_borrowed);
}
#[test]
fn declared_components_cap_a_composite_arity() {
use edifact::{DirectoryValidator, ValidationReport, Validator};
const DEFS: &[SegmentDefinition] = &[NAD];
let validator = DirectoryValidator::from_definitions(DEFS).enforce_known_tags(false);
let too_many = parse(b"NAD+MS+9900112233445::293:EXTRA'");
let mut report = ValidationReport::default();
validator.validate_batch(
&too_many,
&mut report,
&edifact::ValidationRuleContext::empty(),
);
assert!(
report
.errors()
.iter()
.any(|i| i.error_code() == Some("E013")),
"expected InvalidComponentCount, got {}",
report.render_deterministic()
);
let fewer = parse(b"NAD+MS+9900112233445'");
let mut report = ValidationReport::default();
validator.validate_batch(
&fewer,
&mut report,
&edifact::ValidationRuleContext::empty(),
);
assert!(
!report.has_errors(),
"omitting conditional components must stay valid, got {}",
report.render_deterministic()
);
}
#[test]
fn absent_trailing_component_is_truncated() {
let party = SenderParty {
party_id: "9900112233445".to_owned(),
agency: None,
};
assert_eq!(
edifact::to_edifact_string(&party).expect("serialize"),
"NAD+MS+9900112233445'"
);
}
const C080_FAITHFUL: &[ComponentRef] = &[
ComponentRef::repeated(1, "3036", Status::Mandatory, 5),
ComponentRef::new(6, "3045", Status::Conditional),
];
const NAD_C080_ELEMENTS: &[ElementRef] = &[
ElementRef::new(1, "3035", Status::Mandatory, 1),
ElementRef::composite(2, "C080", Status::Conditional, 1, C080_FAITHFUL),
];
const NAD_C080: SegmentDefinition =
SegmentDefinition::new("NAD", "Name and address", NAD_C080_ELEMENTS);
#[test]
fn a_by_design_repeat_stays_addressable_and_points_at_the_first_occurrence() {
assert_eq!(
NAD_C080.code_positions("3036"),
1,
"one entry, one position"
);
let path = NAD_C080.resolve_code("3036").expect("addressable");
assert_eq!((path.element, path.component), (1, Some(0)));
let segments: Vec<_> = edifact::from_bytes(b"NAD+BY+ACME:GMBH:::+X'")
.collect::<Result<Vec<_>, _>>()
.expect("parse");
assert_eq!(
segments[0].value_by_code(&NAD_C080, "3036").unwrap(),
Some("ACME"),
);
assert_eq!(NAD_C080.resolve_code("3045").unwrap().component, Some(5));
}
#[test]
fn the_repeat_count_is_recorded_rather_than_lost() {
let c080 = NAD_C080.elements[1];
assert_eq!(c080.components()[0].repeat_count(), 5);
assert_eq!(c080.components()[1].repeat_count(), 1);
}
#[test]
fn a_repeated_composite_is_not_capped_at_its_entry_count() {
let segments: Vec<_> = edifact::from_bytes(b"NAD+BY+A:B:C:D:E:F'")
.collect::<Result<Vec<_>, _>>()
.expect("parse");
let validator =
edifact::DirectoryValidator::from_definitions(std::slice::from_ref(&NAD_C080_STATIC));
let report = edifact::ValidationContext::builder()
.with_validator(edifact::ValidationLayer::Structure, validator)
.build()
.validate(&segments);
assert!(
!report
.errors()
.iter()
.any(|i| i.error_code() == Some("E013")),
"six components must fit a composite declaring 5+1 slots: {:#?}",
report.errors(),
);
}
static NAD_C080_STATIC: SegmentDefinition = NAD_C080;
mod layout_audit {
use edifact::{
ComponentRef, ElementRef, LayoutFinding, SegmentDefinition, SegmentLayout, Status,
from_bytes,
};
static SHORT_C507: &[ComponentRef] = &[
ComponentRef::new(1, "2005", Status::Mandatory),
ComponentRef::new(2, "2380", Status::Conditional),
];
static SHORT_DTM_ELEMENTS: &[ElementRef] = &[ElementRef::composite(
1,
"C507",
Status::Mandatory,
1,
SHORT_C507,
)];
static SHORT_DTM: SegmentDefinition =
SegmentDefinition::new("DTM", "Date/time/period", SHORT_DTM_ELEMENTS);
static FULL_C507: &[ComponentRef] = &[
ComponentRef::new(1, "2005", Status::Mandatory),
ComponentRef::new(2, "2380", Status::Conditional),
ComponentRef::new(3, "2379", Status::Conditional),
];
static FULL_DTM_ELEMENTS: &[ElementRef] = &[ElementRef::composite(
1,
"C507",
Status::Mandatory,
1,
FULL_C507,
)];
static FULL_DTM: SegmentDefinition =
SegmentDefinition::new("DTM", "Date/time/period", FULL_DTM_ELEMENTS);
fn corpus(input: &[u8]) -> Vec<edifact::Segment<'_>> {
from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.expect("parse")
}
#[test]
fn a_layout_the_wire_disproves_is_a_contradiction() {
let segments = corpus(b"DTM+137:20260101:102'");
let audit = SHORT_DTM.audit(&segments);
assert_eq!(audit.segments_examined(), 1);
assert!(audit.has_contradictions(), "{audit}");
assert!(audit.findings().iter().any(|f| matches!(
f,
LayoutFinding::UndeclaredComponent {
element: 0,
component: 2,
..
}
)));
}
#[test]
fn a_correct_layout_against_a_full_corpus_is_silent() {
let segments = corpus(b"DTM+137:20260101:102'");
let audit = FULL_DTM.audit(&segments);
assert!(!audit.has_contradictions(), "{audit}");
assert_eq!(audit.unconfirmed().count(), 0);
}
#[test]
fn a_slot_the_corpus_never_reaches_is_reported_as_unconfirmed_not_wrong() {
let segments = corpus(b"DTM+137:20260101'");
let audit = FULL_DTM.audit(&segments);
assert!(!audit.has_contradictions(), "{audit}");
let unconfirmed: Vec<&str> = audit
.unconfirmed()
.map(|slot| slot.data_element.as_str())
.collect();
assert_eq!(unconfirmed, ["2379"]);
}
#[test]
fn a_mandatory_slot_that_is_empty_everywhere_is_a_contradiction() {
let segments = corpus(b"DTM+:20260101:102'");
let audit = FULL_DTM.audit(&segments);
assert!(audit.has_contradictions(), "{audit}");
assert!(audit.findings().iter().any(|f| matches!(
f,
LayoutFinding::MandatoryNeverPopulated { slot } if slot.data_element == "2005"
)));
}
#[test]
fn an_empty_corpus_proves_nothing_and_says_so() {
let segments = corpus(b"BGM+220'");
let audit = FULL_DTM.audit(&segments);
assert_eq!(audit.segments_examined(), 0);
assert_eq!(audit.unconfirmed().count(), 2); assert!(audit.has_contradictions());
}
#[test]
fn repeated_occurrences_all_count_as_evidence() {
let segments = corpus(b"UNA:+.?*'DTM+137:20260101*137:20260102:102'");
let audit = FULL_DTM.audit(&segments);
assert!(!audit.has_contradictions(), "{audit}");
assert_eq!(audit.unconfirmed().count(), 0);
}
#[test]
fn a_mandatory_component_of_an_absent_conditional_composite_is_not_a_violation() {
let corpus = corpus(b"UNB+UNOC:3+S+R+260101:0900+IC1'");
let audit = edifact::service::UNB.audit(&corpus);
assert_eq!(audit.segments_examined(), 1);
assert!(!audit.has_contradictions(), "{audit}");
assert!(audit.unconfirmed().any(|slot| slot.data_element == "0022"));
}
#[test]
fn a_mandatory_component_of_a_present_composite_is_still_required() {
let corpus = corpus(b"UNB+UNOC:3+S+R+260101:0900+IC1+:AA'");
let audit = edifact::service::UNB.audit(&corpus);
assert!(audit.has_contradictions(), "{audit}");
assert!(audit.findings().iter().any(|f| matches!(
f,
LayoutFinding::MandatoryNeverPopulated { slot } if slot.data_element == "0022"
)));
}
#[test]
fn a_directory_wide_audit_covers_every_tag_the_corpus_contains() {
use edifact::{audit_directory, service};
let corpus =
corpus(b"UNB+UNOC:3+S+R+260101:0900+IC1'UNH+M1+ORDERS:D:96A:UN'UNT+2+M1'UNZ+1+IC1'");
let audits = audit_directory(service::lookup, &corpus);
assert_eq!(
audits
.iter()
.map(edifact::LayoutAudit::tag)
.collect::<Vec<_>>(),
["UNB", "UNH", "UNT", "UNZ"],
);
assert!(audits.iter().all(|a| !a.has_contradictions()));
assert!(!audits.iter().any(|a| a.tag() == "UNG"));
}
#[test]
fn every_shipped_service_layout_is_confirmed_by_a_corpus_that_fills_it() {
use edifact::service;
let input: &[u8] = b"\
UNB+UNOA:4:SCD:XML:01+SENDER:14:REV:SUB+RECIPIENT:14:FWD:SUB+20260101:0900+IC4711+PW:AA+APPREF+A+1+COMMS+1'\
UNG+ORDERS+APPSND:14+APPRCV:14+20260101:0900+GRP1+UN+D:96A:EAN008+APPPW'\
UNH+MSG1+ORDERS:D:96A:UN:EAN008:CLD1:SUB+CAR+1:C+SUBSET:1:0:UN+GUIDE:1:0:UN+SCEN:1:0:UN'\
UNS+D'\
UGH+SG12'\
UGT+SG12'\
UNT+4+MSG1'\
UNE+1+GRP1'\
UNO+PKG1+AAB:REF1+OBJ:ATTRID:ATTR:UN+1024:3:1:C'\
UNP+1024+PKG1'\
UCI+IC4711+SENDER:14:REV:SUB+RECIPIENT:14:FWD:SUB+4+13+UNB+3:2:1+SEC1+2'\
UCF+GRP1+APPSND:14+APPRCV:14+4+13+UNG+3:2:1+SEC1+2'\
UCM+MSG1+ORDERS:D:96A:UN:EAN008:CLD1:SUB+4+13+UNH+3:2:1+PKG1+AAB:REF1+SEC1+2'\
UCS+7+13'\
UCD+13+3:2:1'\
UNZ+1+IC4711'";
let segments = corpus(input);
for definition in service::ALL {
let audit = definition.audit(&segments);
assert!(
audit.segments_examined() > 0,
"{} is shipped but the fixture never exercises it — add it rather \
than letting the audit skip it silently",
definition.tag,
);
assert!(
!audit.has_contradictions(),
"{} disagrees with the wire:\n{audit}",
definition.tag,
);
let unconfirmed: Vec<String> = audit
.unconfirmed()
.map(|slot| slot.data_element.clone())
.collect();
assert!(
unconfirmed.is_empty(),
"{} has positions the corpus never reached — either the fixture \
does not fill them or the layout declares something that is not \
there: {unconfirmed:?}",
definition.tag,
);
}
}
#[test]
fn one_finding_per_position_however_large_the_corpus() {
let mut input = Vec::new();
for _ in 0..360 {
input.extend_from_slice(b"DTM+137:20260101:102'");
}
let segments = corpus(&input);
let audit = SHORT_DTM.audit(&segments);
assert_eq!(audit.segments_examined(), 360);
assert_eq!(audit.contradictions().count(), 1, "{audit}");
}
}