1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use derive_more::{Display, From};
use either::Either;
use std::io;

pub use crate::error::*;
pub use crate::v5::codec;

/// Errors which can occur when attempting to handle mqtt client connection.
#[derive(Debug, Display, From)]
pub enum ClientError {
    /// Connect negotiation failed
    #[display(fmt = "Connect ack failed: {:?}", _0)]
    Ack(codec::ConnectAck),
    /// Protocol error
    #[display(fmt = "Protocol error: {:?}", _0)]
    Protocol(ProtocolError),
    /// Handshake timeout
    #[display(fmt = "Handshake timeout")]
    HandshakeTimeout,
    /// Peer disconnected
    #[display(fmt = "Peer disconnected")]
    Disconnected,
    /// Connect error
    #[display(fmt = "Connect error: {}", _0)]
    Connect(ntex::connect::ConnectError),
}

impl std::error::Error for ClientError {}

impl From<Either<EncodeError, io::Error>> for ClientError {
    fn from(err: Either<EncodeError, io::Error>) -> Self {
        match err {
            Either::Left(err) => ClientError::Protocol(ProtocolError::Encode(err)),
            Either::Right(err) => ClientError::Protocol(ProtocolError::Io(err)),
        }
    }
}

#[derive(Debug, Display)]
pub enum PublishQos0Error {
    /// Encoder error
    Encode(EncodeError),
    /// Can not allocate next packet id
    #[display(fmt = "Can not allocate next packet id")]
    PacketIdNotAvailable,
    /// Peer disconnected
    #[display(fmt = "Peer disconnected")]
    Disconnected,
}

#[derive(Debug, Display)]
pub enum PublishQos1Error {
    /// Negative ack from peer
    #[display(fmt = "Negative ack: {:?}", _0)]
    Fail(codec::PublishAck),
    /// Encoder error
    Encode(EncodeError),
    /// Can not allocate next packet id
    #[display(fmt = "Can not allocate next packet id")]
    PacketIdNotAvailable,
    /// Peer disconnected
    #[display(fmt = "Peer disconnected")]
    Disconnected,
}

#[derive(Debug, Display)]
pub enum SubscribeError {
    /// Encoder error
    Encode(EncodeError),
    /// Can not allocate next packet id
    #[display(fmt = "Can not allocate next packet id")]
    PacketIdNotAvailable,
    /// Peer disconnected
    #[display(fmt = "Peer disconnected")]
    Disconnected,
}

#[derive(Debug, Display)]
pub enum UnsubscribeError {
    /// Encoder error
    Encode(EncodeError),
    /// Can not allocate next packet id
    #[display(fmt = "Can not allocate next packet id")]
    PacketIdNotAvailable,
    /// Peer disconnected
    #[display(fmt = "Peer disconnected")]
    Disconnected,
}