mig-assembly 0.51.0

MIG-guided EDIFACT tree assembly — parse RawSegments into typed MIG trees
Documentation
use thiserror::Error;

#[derive(Error, Debug)]
pub enum AssemblyError {
    #[error(
        "Unexpected segment '{segment_id}' at position {position}, expected one of: {expected:?}"
    )]
    UnexpectedSegment {
        segment_id: String,
        position: usize,
        expected: Vec<String>,
    },

    #[error("Missing mandatory segment '{segment_id}' for PID {pid}")]
    MissingMandatory { segment_id: String, pid: String },

    #[error("Unknown PID: {0}")]
    UnknownPid(String),

    #[error("PID detection failed: could not determine PID from segments")]
    PidDetectionFailed,

    #[error("Segment cursor out of bounds at position {0}")]
    CursorOutOfBounds(usize),

    #[error("Parse error: {0}")]
    ParseError(String),

    #[error("Expected segment '{expected}' not found")]
    SegmentNotFound { expected: String },

    /// A group instance has content but not the segment that opens the group
    /// in the MIG, so rendering it would produce EDIFACT no receiver can
    /// assemble (e.g. an SG10 with `CAV` but no `CCI`).
    #[error(
        "Group {group_path} ({source_path}) has segments {present_segments:?} but no entry segment '{entry_segment}'"
    )]
    MissingGroupEntrySegment {
        group_path: String,
        source_path: String,
        entry_segment: String,
        present_segments: Vec<String>,
    },

    /// A segment would carry no qualifier in the mandatory coded data element
    /// that opens it (`FTX++++text` for an FTX whose DE4451 the BO4E input did
    /// not fill, or filled with a value the mapping does not know). A receiver
    /// cannot tell what such a segment is, and the MIG syntax check rejects it.
    #[error(
        "Segment {tag} would carry no qualifier in DE{element_id} (one of {allowed:?}): the BO4E field that writes it is missing or holds a value the mapping does not know"
    )]
    InvalidSegmentQualifier {
        tag: String,
        /// The data element (e.g. `"4451"`).
        element_id: String,
        /// What the segment would carry (empty when nothing was written).
        value: String,
        /// The codes the MIG lists there.
        allowed: Vec<String>,
    },
}

impl From<mig_types::cursor::SegmentNotFound> for AssemblyError {
    fn from(e: mig_types::cursor::SegmentNotFound) -> Self {
        AssemblyError::SegmentNotFound {
            expected: e.expected,
        }
    }
}