Skip to main content

pamoja_serial/
error.rs

1//! The error type for serial-line framing.
2
3/// What can go wrong framing or unframing a serial packet.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum SerialError {
6    /// The caller's output buffer is too small to hold the encoded frame or the decoded
7    /// payload. The `max_encoded_len` helpers size a buffer that always fits.
8    BufferTooSmall,
9    /// A SLIP escape byte was followed by a byte that is neither the escaped-delimiter nor
10    /// the escaped-escape marker, so the frame is corrupt.
11    InvalidEscape,
12    /// A frame ended early: a SLIP frame stopped in the middle of an escape sequence, or a
13    /// COBS code byte claimed more data than the frame actually carried.
14    TruncatedFrame,
15}
16
17impl core::fmt::Display for SerialError {
18    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        match self {
20            SerialError::BufferTooSmall => {
21                f.write_str("serial output buffer is too small for the frame")
22            }
23            SerialError::InvalidEscape => {
24                f.write_str("serial frame contains an invalid SLIP escape sequence")
25            }
26            SerialError::TruncatedFrame => f.write_str("serial frame is truncated"),
27        }
28    }
29}
30
31// `core::error::Error` rather than `std::error::Error`, so a caller on a
32// microcontroller gets the same trait a caller on a gateway does.
33impl core::error::Error for SerialError {}