Skip to main content

pamoja_mesh/
error.rs

1//! The error type for mesh framing.
2
3/// What can go wrong building or reading a mesh frame.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum MeshError {
6    /// A frame is shorter than the header and checksum a frame must at least contain.
7    FrameTooShort,
8    /// A frame is larger than [`Frame::MAX_LEN`](crate::Frame::MAX_LEN).
9    FrameTooLong,
10    /// A payload is larger than [`Frame::MAX_PAYLOAD`](crate::Frame::MAX_PAYLOAD), so it
11    /// will not fit a single frame.
12    PayloadTooLong,
13    /// A received frame declares a protocol version this build does not understand.
14    UnsupportedVersion(u8),
15    /// A received frame's checksum does not match its contents, so the frame is corrupt.
16    CrcMismatch {
17        /// The checksum computed over the frame's contents.
18        expected: u16,
19        /// The checksum the frame carried.
20        found: u16,
21    },
22}
23
24impl core::fmt::Display for MeshError {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            MeshError::FrameTooShort => {
28                f.write_str("mesh frame is shorter than its header and checksum")
29            }
30            MeshError::FrameTooLong => {
31                f.write_str("mesh frame is larger than the maximum frame size")
32            }
33            MeshError::PayloadTooLong => {
34                f.write_str("mesh payload is larger than a single frame can carry")
35            }
36            MeshError::UnsupportedVersion(version) => {
37                write!(f, "unsupported mesh protocol version {version}")
38            }
39            MeshError::CrcMismatch { expected, found } => {
40                write!(
41                    f,
42                    "mesh CRC mismatch: expected {expected:#06x}, found {found:#06x}"
43                )
44            }
45        }
46    }
47}
48
49// `core::error::Error` rather than `std::error::Error`, so a caller on a
50// microcontroller gets the same trait a caller on a gateway does.
51impl core::error::Error for MeshError {}