Skip to main content

atdecc/
error.rs

1//! Why a frame could not be decoded or encoded.
2
3use core::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum DecodeError {
7    /// The frame ends before the fields it must have.
8    Truncated { needed: usize, available: usize },
9    /// The subtype is not the one the decoder handles.
10    WrongSubtype(u8),
11    /// The version field is not one this crate knows.
12    UnsupportedVersion(u8),
13    /// The message type is not one the decoder handles.
14    WrongMessageType(u8),
15    /// A vendor unique message of another protocol.
16    WrongProtocol,
17    /// The control_data_length is shorter than the message's fields.
18    ShortControlData(u16),
19}
20
21impl fmt::Display for DecodeError {
22    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            DecodeError::Truncated { needed, available } => {
25                write!(
26                    formatter,
27                    "frame too short: {available} octets, {needed} needed"
28                )
29            }
30            DecodeError::WrongSubtype(subtype) => {
31                write!(formatter, "unexpected subtype {subtype:#04x}")
32            }
33            DecodeError::UnsupportedVersion(version) => {
34                write!(formatter, "unsupported version {version}")
35            }
36            DecodeError::ShortControlData(length) => {
37                write!(formatter, "control_data_length {length} is too short")
38            }
39            DecodeError::WrongMessageType(message_type) => {
40                write!(formatter, "unexpected message type {message_type}")
41            }
42            DecodeError::WrongProtocol => formatter.write_str("another vendor unique protocol"),
43        }
44    }
45}
46
47impl core::error::Error for DecodeError {}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum EncodeError {
51    /// The buffer is smaller than the encoded message.
52    BufferTooSmall { needed: usize, available: usize },
53    /// A field holds a value wider than its place in the frame.
54    OutOfRange(&'static str),
55}
56
57impl fmt::Display for EncodeError {
58    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            EncodeError::BufferTooSmall { needed, available } => {
61                write!(
62                    formatter,
63                    "buffer too small: {available} octets, {needed} needed"
64                )
65            }
66            EncodeError::OutOfRange(field) => write!(formatter, "{field} does not fit its field"),
67        }
68    }
69}
70
71impl core::error::Error for EncodeError {}