sbf_tools/error.rs
1//! SBF error types
2
3use thiserror::Error;
4
5/// Errors that can occur during SBF parsing
6#[derive(Error, Debug)]
7#[non_exhaustive]
8pub enum SbfError {
9 /// Invalid sync bytes (expected 0x24 0x40)
10 #[error("Invalid sync bytes")]
11 InvalidSync,
12
13 /// CRC checksum mismatch
14 #[error("CRC mismatch: expected {expected:#06x}, got {actual:#06x}")]
15 CrcMismatch { expected: u16, actual: u16 },
16
17 /// Block is incomplete (not enough data)
18 #[error("Incomplete block: need {needed} bytes, have {have}")]
19 IncompleteBlock { needed: usize, have: usize },
20
21 /// Invalid block length (must be >= 8 and divisible by 4)
22 #[error("Invalid block length: {0}")]
23 InvalidLength(u16),
24
25 /// Block-specific parsing error
26 #[error("Parse error: {0}")]
27 ParseError(String),
28
29 /// I/O error during reading
30 #[error("I/O error: {0}")]
31 Io(#[from] std::io::Error),
32
33 /// No complete block is available yet, but the stream has not ended.
34 ///
35 /// Returned by [`SbfReader::read_block`](crate::SbfReader::read_block) when a
36 /// non-blocking source (for example a serial port in non-blocking mode) has
37 /// no data available right now. This is distinct from end of stream: EOF is
38 /// reported as `Ok(None)`, whereas `WouldBlock` means the caller should retry
39 /// later. Blocking sources (files, `Cursor`) never produce this.
40 #[error("Would block: no data available yet")]
41 WouldBlock,
42}
43
44/// Result type for SBF operations
45pub type SbfResult<T> = Result<T, SbfError>;