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    /// Address (talker + type) contains non-ASCII bytes (NMEA addresses are ASCII).
18    NonAsciiAddress,
19}
20
21impl core::fmt::Display for FrameError {
22    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23        match self {
24            Self::Empty => write!(f, "empty input"),
25            Self::InvalidPrefix(c) => write!(f, "invalid prefix '{c}', expected '$' or '!'"),
26            Self::MalformedChecksum => write!(f, "checksum is not valid hexadecimal"),
27            Self::BadChecksum { expected, computed } => {
28                write!(
29                    f,
30                    "checksum mismatch: expected {expected:02X}, computed {computed:02X}"
31                )
32            }
33            Self::MalformedTagBlock => write!(f, "malformed IEC 61162-450 tag block"),
34            Self::TooShort => write!(f, "sentence too short"),
35            Self::NonAsciiAddress => write!(f, "address field contains non-ASCII bytes"),
36        }
37    }
38}
39
40impl std::error::Error for FrameError {}