Skip to main content

mig_assembly/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum AssemblyError {
5    #[error(
6        "Unexpected segment '{segment_id}' at position {position}, expected one of: {expected:?}"
7    )]
8    UnexpectedSegment {
9        segment_id: String,
10        position: usize,
11        expected: Vec<String>,
12    },
13
14    #[error("Missing mandatory segment '{segment_id}' for PID {pid}")]
15    MissingMandatory { segment_id: String, pid: String },
16
17    #[error("Unknown PID: {0}")]
18    UnknownPid(String),
19
20    #[error("PID detection failed: could not determine PID from segments")]
21    PidDetectionFailed,
22
23    #[error("Segment cursor out of bounds at position {0}")]
24    CursorOutOfBounds(usize),
25
26    #[error("Parse error: {0}")]
27    ParseError(String),
28
29    #[error("Expected segment '{expected}' not found")]
30    SegmentNotFound { expected: String },
31
32    /// A group instance has content but not the segment that opens the group
33    /// in the MIG, so rendering it would produce EDIFACT no receiver can
34    /// assemble (e.g. an SG10 with `CAV` but no `CCI`).
35    #[error(
36        "Group {group_path} ({source_path}) has segments {present_segments:?} but no entry segment '{entry_segment}'"
37    )]
38    MissingGroupEntrySegment {
39        group_path: String,
40        source_path: String,
41        entry_segment: String,
42        present_segments: Vec<String>,
43    },
44}
45
46impl From<mig_types::cursor::SegmentNotFound> for AssemblyError {
47    fn from(e: mig_types::cursor::SegmentNotFound) -> Self {
48        AssemblyError::SegmentNotFound {
49            expected: e.expected,
50        }
51    }
52}