Skip to main content

nmea_kit/
error.rs

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