Skip to main content

edifact_parser/
error.rs

1use edifact_primitives::SegmentPosition;
2
3/// Errors that can occur during EDIFACT parsing.
4#[derive(Debug, thiserror::Error)]
5pub enum ParseError {
6    /// The UNA service string advice header is invalid.
7    #[error("invalid UNA header at byte {offset}")]
8    InvalidUna { offset: usize },
9
10    /// A segment was not properly terminated.
11    #[error("unterminated segment at byte {offset}")]
12    UnterminatedSegment { offset: usize },
13
14    /// The input ended unexpectedly.
15    #[error("unexpected end of input")]
16    UnexpectedEof,
17
18    /// The input contains invalid UTF-8.
19    #[error("invalid UTF-8 at byte {offset}: {source}")]
20    InvalidUtf8 {
21        offset: usize,
22        #[source]
23        source: std::str::Utf8Error,
24    },
25
26    /// A segment ID could not be determined.
27    #[error("empty segment ID at byte {offset}")]
28    EmptySegmentId { offset: usize },
29
30    /// Handler returned Control::Stop.
31    #[error("parsing stopped by handler at {position}")]
32    StoppedByHandler { position: SegmentPosition },
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_parse_error_display_invalid_una() {
41        let err = ParseError::InvalidUna { offset: 0 };
42        assert_eq!(err.to_string(), "invalid UNA header at byte 0");
43    }
44
45    #[test]
46    fn test_parse_error_display_unterminated() {
47        let err = ParseError::UnterminatedSegment { offset: 42 };
48        assert_eq!(err.to_string(), "unterminated segment at byte 42");
49    }
50
51    #[test]
52    fn test_parse_error_display_unexpected_eof() {
53        let err = ParseError::UnexpectedEof;
54        assert_eq!(err.to_string(), "unexpected end of input");
55    }
56
57    #[test]
58    fn test_parse_error_display_stopped() {
59        let err = ParseError::StoppedByHandler {
60            position: SegmentPosition::new(3, 100, 1),
61        };
62        assert_eq!(
63            err.to_string(),
64            "parsing stopped by handler at segment 3 at byte 100 (message 1)"
65        );
66    }
67
68    #[test]
69    fn test_parse_error_is_send_sync() {
70        fn assert_send_sync<T: Send + Sync>() {}
71        // ParseError contains Utf8Error which is Send+Sync
72        // This ensures our error type can be used across threads
73        assert_send_sync::<ParseError>();
74    }
75}