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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::{
io,
result,
str,
};
use thiserror::Error;
use crate::{
ffi,
message::Message,
reason_code::ReasonCode,
};
#[derive(Error, Debug)]
pub enum Error {
#[error("{}", error_message(*.0))]
Paho(i32),
#[error("[{0}] {1}")]
PahoDescr(i32, String),
#[error("{}", error_message(*.0))]
Publish(i32, Message),
#[error("{0}")]
ReasonCode(ReasonCode),
#[error("I/O failed: {0}")]
Io(#[from] io::Error),
#[error("String UTF-8 Error")]
Utf8(#[from] str::Utf8Error),
#[error("String NUL Error")]
Nul(#[from] std::ffi::NulError),
#[error("Timeout")]
Timeout,
#[error("{0}")]
General(&'static str),
#[error("{0}")]
GeneralString(String),
}
impl From<i32> for Error {
fn from(rc: i32) -> Error {
Error::Paho(rc)
}
}
impl From<&'static str> for Error {
fn from(descr: &'static str) -> Error {
Error::General(descr)
}
}
impl From<String> for Error {
fn from(descr: String) -> Error {
Error::GeneralString(descr)
}
}
pub type Result<T> = result::Result<T, Error>;
pub fn error_message(rc: i32) -> &'static str {
match rc {
ffi::MQTTASYNC_FAILURE => "General failure",
ffi::MQTTASYNC_PERSISTENCE_ERROR => "Persistence error",
ffi::MQTTASYNC_DISCONNECTED => "Client disconnected",
ffi::MQTTASYNC_MAX_MESSAGES_INFLIGHT => "Maximum inflight messages",
ffi::MQTTASYNC_BAD_UTF8_STRING => "Bad UTF8 string",
ffi::MQTTASYNC_NULL_PARAMETER => "NULL Parameter",
ffi::MQTTASYNC_TOPICNAME_TRUNCATED => "Topic name truncated",
ffi::MQTTASYNC_BAD_STRUCTURE => "Bad structure",
ffi::MQTTASYNC_BAD_QOS => "Bad QoS",
ffi::MQTTASYNC_NO_MORE_MSGIDS => "No more message ID's",
ffi::MQTTASYNC_OPERATION_INCOMPLETE => "Operation incomplete",
ffi::MQTTASYNC_MAX_BUFFERED_MESSAGES => "Max buffered messages",
ffi::MQTTASYNC_SSL_NOT_SUPPORTED => "SSL not supported by Paho C library",
ffi::MQTTASYNC_BAD_PROTOCOL => "Bad protocol",
ffi::MQTTASYNC_BAD_MQTT_OPTION => "Bad option",
ffi::MQTTASYNC_WRONG_MQTT_VERSION => "Wrong MQTT version",
ffi::MQTTASYNC_0_LEN_WILL_TOPIC => "Zero length Will Topic",
_ => "Unknown Error",
}
}
pub const Failure: Error = Error::Paho(ffi::MQTTASYNC_FAILURE);
pub const PersistenceError: Error = Error::Paho(ffi::MQTTASYNC_PERSISTENCE_ERROR);
pub const Disconnected: Error = Error::Paho(ffi::MQTTASYNC_DISCONNECTED);
pub const MaxMessagesInflight: Error = Error::Paho(ffi::MQTTASYNC_MAX_MESSAGES_INFLIGHT);
pub const BadUtfString: Error = Error::Paho(ffi::MQTTASYNC_BAD_UTF8_STRING);
pub const NullParameter: Error = Error::Paho(ffi::MQTTASYNC_NULL_PARAMETER);
pub const TopicNameTruncated: Error = Error::Paho(ffi::MQTTASYNC_TOPICNAME_TRUNCATED);
pub const BadStructure: Error = Error::Paho(ffi::MQTTASYNC_BAD_STRUCTURE);
pub const BadQos: Error = Error::Paho(ffi::MQTTASYNC_BAD_QOS);
pub const NoMoreMsgids: Error = Error::Paho(ffi::MQTTASYNC_NO_MORE_MSGIDS);
pub const OperationIncomplete: Error = Error::Paho(ffi::MQTTASYNC_OPERATION_INCOMPLETE);
pub const MaxBufferedMessages: Error = Error::Paho(ffi::MQTTASYNC_MAX_BUFFERED_MESSAGES);
pub const SslNotSupported: Error = Error::Paho(ffi::MQTTASYNC_SSL_NOT_SUPPORTED);
pub const BadProtocol: Error = Error::Paho(ffi::MQTTASYNC_BAD_PROTOCOL);
pub const BadMqttOption: Error = Error::Paho(ffi::MQTTASYNC_BAD_MQTT_OPTION);
pub const WrongMqttVersion: Error = Error::Paho(ffi::MQTTASYNC_WRONG_MQTT_VERSION);
pub const ZeroLenWillTopic: Error = Error::Paho(ffi::MQTTASYNC_0_LEN_WILL_TOPIC);