use std::io;
use std::fmt::{self, Formatter, Display};
use std::error::Error;
use std::str::Utf8Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServerConnectError
{
UnacceptableProtocolVersion,
IdentifierRejected,
ServerUnavailable,
BadUserNameOrPassword,
NotAuthorized,
Unknown(u8),
ProtocolError
}
#[derive(Debug)]
pub enum TimeoutKind
{
DnsLookup,
TcpConnect,
MqttConnect
}
#[derive(Debug)]
pub enum ConnectError
{
InvalidHostname,
#[cfg(feature = "tls")]
TlsError(rustls::Error),
IoError(io::Error),
LookupHostError(io::Error),
HostnameNotFound,
Timeout(TimeoutKind),
OneshotRecvError,
ServerError(ServerConnectError)
}
#[derive(Debug)]
pub enum PublishError
{
TransceiverTaskTerminated
}
#[derive(Debug)]
pub enum TryPublishError
{
QueueFull,
TransceiverTaskTerminated
}
#[derive(Debug)]
pub enum SubscribeError
{
RefusedByBroker,
TransceiverTaskTerminated
}
#[derive(Debug)]
pub enum PacketDecodeError
{
ReachedEndUnexpectedly,
Utf8Error(Utf8Error),
MalformedPacket,
ReachedMaxSize,
InvalidPacketId(u8)
}
impl Display for PacketDecodeError
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
{
match self {
PacketDecodeError::ReachedEndUnexpectedly => write!(f, "reached end of packet unexpectedly"),
PacketDecodeError::Utf8Error(_) => write!(f, "invalid utf-8 found in packet"),
PacketDecodeError::MalformedPacket => write!(f, "packet contained invalid values"),
PacketDecodeError::ReachedMaxSize => write!(f, "maximum packet size reached"),
PacketDecodeError::InvalidPacketId(pid) => write!(f, "packet with id 0x{:02x} is invalid", pid),
}
}
}
impl Error for PacketDecodeError
{
fn source(&self) -> Option<&(dyn Error + 'static)>
{
match self {
PacketDecodeError::ReachedEndUnexpectedly => None,
PacketDecodeError::Utf8Error(src) => Some(src),
PacketDecodeError::MalformedPacket => None,
PacketDecodeError::ReachedMaxSize => None,
PacketDecodeError::InvalidPacketId(_) => None
}
}
}
impl Into<io::Error> for PacketDecodeError
{
fn into(self) -> io::Error
{
io::Error::new(io::ErrorKind::Other, Box::new(self))
}
}