embedded_mqtt/
error.rs

1use core::{
2    convert::From,
3    fmt,
4    str::Utf8Error,
5};
6
7use crate::qos;
8
9#[derive(Copy, Clone, PartialEq, Eq, Debug)]
10pub enum DecodeError {
11    /// Invalid packet type in header
12    PacketType,
13    /// Invalid packet type flag in header
14    PacketFlag,
15    /// Malformed remaining length in header
16    RemainingLength,
17    /// Invalid buffer length
18    InvalidLength,
19    /// Invalid UTF-8 encoding
20    Utf8,
21    /// Invalid QoS value
22    InvalidQoS(qos::Error),
23    /// Invalid protocol level
24    InvalidProtocolLevel,
25    /// Invalid connect flag value
26    InvalidConnectFlag,
27    /// Invalid Connack flag
28    InvalidConnackFlag,
29    /// Invalid Connack Return Code
30    InvalidConnackReturnCode,
31    /// Invalid Suback Return Code
32    InvalidSubackReturnCode,
33}
34
35impl DecodeError {
36    fn desc(&self) -> &'static str {
37        match *self {
38            DecodeError::PacketType => "invalid packet type in header",
39            DecodeError::PacketFlag => "invalid packet type flag in header",
40            DecodeError::RemainingLength => "malformed remaining length in header",
41            DecodeError::InvalidLength => "invalid buffer length",
42            DecodeError::Utf8 => "invalid utf-8 encoding",
43            DecodeError::InvalidQoS(_) => "invalid QoS bit pattern",
44            DecodeError::InvalidProtocolLevel => "invalid protocol level",
45            DecodeError::InvalidConnectFlag => "invalid connect flag value",
46            DecodeError::InvalidConnackFlag => "invalid connack flag value",
47            DecodeError::InvalidConnackReturnCode => "invalid connack return code",
48            DecodeError::InvalidSubackReturnCode => "invalid suback return code",
49        }
50    }
51}
52
53impl fmt::Display for DecodeError {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        f.write_str(self.desc())
56    }
57}
58
59#[cfg(feature = "std")]
60impl ::std::error::Error for DecodeError {
61    fn description(&self) -> &str {
62        self.desc()
63    }
64}
65
66impl From<Utf8Error> for DecodeError {
67    fn from(_: Utf8Error) -> Self {
68        DecodeError::Utf8
69    }
70}
71
72impl From<qos::Error> for DecodeError {
73    fn from(err: qos::Error) -> Self {
74        DecodeError::InvalidQoS(err)
75    }
76}
77
78#[derive(Copy, Clone, PartialEq, Eq, Debug)]
79pub enum EncodeError {
80    /// Not enough space in buffer to encode
81    OutOfSpace,
82    /// Value too big for field
83    ValueTooBig,
84}
85
86impl EncodeError {
87    fn desc(&self) -> &'static str {
88        match *self {
89            EncodeError::OutOfSpace => "not enough space in encode buffer",
90            EncodeError::ValueTooBig => "value too big to ever be encoded"
91        }
92    }
93}
94
95impl fmt::Display for EncodeError {
96    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97        f.write_str(self.desc())
98    }
99}
100
101#[cfg(feature = "std")]
102impl ::std::error::Error for EncodeError {
103    fn description(&self) -> &str {
104        self.desc()
105    }
106}
107
108impl From<core::num::TryFromIntError> for EncodeError {
109    fn from(_err: core::num::TryFromIntError) -> EncodeError {
110        EncodeError::ValueTooBig
111    }
112}