1use core::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum DecodeError {
7 Truncated { needed: usize, available: usize },
9 WrongSubtype(u8),
11 UnsupportedVersion(u8),
13 WrongMessageType(u8),
15 WrongProtocol,
17 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 BufferTooSmall { needed: usize, available: usize },
53 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 {}