1#[cfg(not(feature = "std"))]
2use alloc::string::String;
3use core::fmt;
4
5#[derive(Debug)]
7pub enum Error {
8 MalformedPacket(&'static str),
9 InvalidPacketType(u8),
10 InvalidQoS(u8),
11 InvalidReasonCode(u8),
12 ConnectionRefused(u8),
13 NotConnected,
14 PacketTooLarge,
15 StringTooLong(usize),
16 Timeout,
17 UnexpectedPacket(&'static str),
18 Serialize(String),
19 Deserialize(String),
20 ConnectionClosed,
21 #[cfg(feature = "std")]
22 Io(std::io::Error),
23}
24
25impl fmt::Display for Error {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 match self {
28 Error::MalformedPacket(msg) => write!(f, "malformed packet: {msg}"),
29 Error::InvalidPacketType(t) => write!(f, "invalid packet type: {t}"),
30 Error::InvalidQoS(q) => write!(f, "invalid QoS: {q}"),
31 Error::InvalidReasonCode(c) => write!(f, "invalid reason code: 0x{c:02x}"),
32 Error::ConnectionRefused(c) => write!(f, "connection refused: 0x{c:02x}"),
33 Error::NotConnected => write!(f, "not connected"),
34 Error::PacketTooLarge => write!(f, "packet too large"),
35 Error::StringTooLong(len) => write!(f, "string too long: {len} bytes"),
36 Error::ConnectionClosed => write!(f, "connection closed"),
37 Error::Timeout => write!(f, "timeout"),
38 Error::UnexpectedPacket(msg) => write!(f, "unexpected packet: {msg}"),
39 Error::Serialize(msg) => write!(f, "serialize: {msg}"),
40 Error::Deserialize(msg) => write!(f, "deserialize: {msg}"),
41 #[cfg(feature = "std")]
42 Error::Io(e) => write!(f, "io: {e}"),
43 }
44 }
45}
46
47#[cfg(feature = "std")]
48impl std::error::Error for Error {}
49
50#[cfg(feature = "std")]
51impl From<std::io::Error> for Error {
52 fn from(e: std::io::Error) -> Self {
53 Error::Io(e)
54 }
55}
56
57pub type Result<T> = core::result::Result<T, Error>;