Skip to main content

nmea_kit/
error.rs

1/// Errors from frame-level parsing (checksum, delimiters, tag blocks).
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub enum FrameError {
4    /// Input is empty or whitespace-only.
5    Empty,
6    /// First character is not `$` or `!`.
7    InvalidPrefix(char),
8    /// Checksum field is not valid hexadecimal.
9    MalformedChecksum,
10    /// Checksum mismatch.
11    BadChecksum { expected: u8, computed: u8 },
12    /// Tag block opened with `\` but not properly closed.
13    MalformedTagBlock,
14    /// Sentence too short to contain talker + type (minimum 5 chars: e.g. "GPRMC").
15    TooShort,
16}
17
18impl core::fmt::Display for FrameError {
19    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20        match self {
21            Self::Empty => write!(f, "empty input"),
22            Self::InvalidPrefix(c) => write!(f, "invalid prefix '{c}', expected '$' or '!'"),
23            Self::MalformedChecksum => write!(f, "checksum is not valid hexadecimal"),
24            Self::BadChecksum { expected, computed } => {
25                write!(
26                    f,
27                    "checksum mismatch: expected {expected:02X}, computed {computed:02X}"
28                )
29            }
30            Self::MalformedTagBlock => write!(f, "malformed IEC 61162-450 tag block"),
31            Self::TooShort => write!(f, "sentence too short"),
32        }
33    }
34}
35
36impl std::error::Error for FrameError {}