Diagnostics

Render EDIFACT errors and validation findings with miette: stable codes, help text, and source spans underlined.

On this page
  1. What each type provides
  2. Errors from main
  3. Underlining findings in the input
  4. Deterministic text for snapshots
  5. Text from the input is escaped
  6. Example
  7. Next steps

The opt-in diagnostics feature implements miette::Diagnostic for the error and validation types. Parsing is unchanged.

cargo add edifact --features diagnostics
cargo add miette --features fancy   # to render; edifact does not re-export it

What each type provides

TypecodehelpLabels / related
EdifactErrorstable code (E001…)remediation hint, where one existsnone: the offset or span is in the message and the fields
ValidationIssueerror code, else rule idthe issue's suggestionone label at its span: kind and group path (missing at SG4[0]/SG8[1]/RFF)
ValidationReportVALIDATIONerror, warning and info countsevery issue via related(): errors, then warnings, then infos

Severity follows the validation pipeline: Error and Critical render as errors, Warning as warnings, Info as advice.

Errors from main

EdifactError converts into miette::Report with ?:

fn main() -> miette::Result<()> {
    let input = b"UNH+1+ORDERS:D:11A:UN'BGM+220+PO-4711+9'UNT+3+1'";
    let _segs: Vec<_> = edifact::from_bytes(input).collect::<Result<_, _>>()?;
    Ok(())
}

Or wrap an error explicitly:

fn report(input: &[u8]) {
    if let Err(err) = edifact::from_bytes(input).collect::<Result<Vec<_>, _>>() {
        eprintln!("{:?}", miette::Report::new(err));
    }
}

Underlining findings in the input

Attach the source to a ValidationReport; each issue with a span is underlined:

use edifact::{ProfileRulePack, ValidationContext, from_bytes};

let input = "UNH+1+ORDERS:D:96A:UN'BGM+220'DTM+137:20240101:102'UNT+4+1'";
let segs: Vec<_> = from_bytes(input.as_bytes()).collect::<Result<_, _>>()?;
let report = ValidationContext::builder()
    .with_profile_pack(ProfileRulePack::new("AHB").forbid_segment("DTM", "AHB-DTM-X"))
    .build()
    .validate(&segs);

eprintln!("{:?}", miette::Report::new(report).with_source_code(input));
# Ok::<(), edifact::EdifactError>(())
Error: AHB-DTM-X

  × [ERROR] segment DTM must not appear
   ╭────
 1 │ UNH+1+ORDERS:D:96A:UN'BGM+220'DTM+137:20240101:102'UNT+4+1'
   ·                               ──────────┬──────────
   ·                                         ╰── not_permitted at DTM
   ╰────

Deterministic text for snapshots

ValidationReport::render_deterministic() (also its Display) needs no feature and gives the same text for the same report:

use edifact::{ProfileRulePack, ValidationContext, from_bytes};

let segs: Vec<_> = from_bytes(b"BGM+220+PO-4711+9'DTM+137:20240101:102'").collect::<Result<_, _>>()?;
let report = ValidationContext::builder()
    .with_profile_pack(ProfileRulePack::new("AHB").forbid_segment("DTM", "AHB-DTM-X"))
    .build()
    .validate(&segs);
assert!(report.render_deterministic().contains("[rule=AHB-DTM-X]"));
# Ok::<(), edifact::EdifactError>(())

Each line is the message followed by the tokens that are set, in this order: [CODE], [segment=…], [rule=…], [element=…], [component=…], [occurrence=…], [position=…], [path=…], [layer=…], [kind=…], [pack=…], [span=…], [hint=…]. Within a severity bucket, lines sort by span start, tag, rule id, element and component index, code and message, then segment position, occurrence, layer, kind, pack and group path.

Text from the input is escaped

Input-derived values — a reference, a qualifier, a tag — appear in error text, issue text, render_deterministic() and the miette help and labels with control characters escaped (\r, \n, \t, \0, otherwise \u{1b} style), as are U+2028/U+2029 and the bidirectional formatting characters (U+061C, U+200E/F, U+202A–E, U+2066–9). One diagnostic is one line: a sender cannot forge log lines, reorder text or drive a terminal. The fields keep the raw values.

use edifact::{EdifactError, Span};

let err = EdifactError::EmptyMessage { message_ref: "1\r\nFAKE".into(), span: Span::new(0, 9) };
assert_eq!(err.to_string(), r"message 1\r\nFAKE has no segments between UNH and UNT");

Example

cargo run -p edifact --example cookbook_diagnostics --features diagnostics

cookbook_diagnostics.rs raises a code-value error from a custom validator and renders it with miette::Report.

Next steps