1use std::fmt;
2
3#[derive(Debug)]
4pub enum MqttError {
5 Connection(String),
6 Publish(String),
7 Subscribe(String),
8 Topic(String),
9 Protocol(String),
10}
11
12impl fmt::Display for MqttError {
13 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14 match self {
15 MqttError::Connection(msg) => write!(f, "MQTT connection error: {}", msg),
16 MqttError::Publish(msg) => write!(f, "MQTT publish error: {}", msg),
17 MqttError::Subscribe(msg) => write!(f, "MQTT subscribe error: {}", msg),
18 MqttError::Topic(msg) => write!(f, "MQTT topic error: {}", msg),
19 MqttError::Protocol(msg) => write!(f, "MQTT protocol error: {}", msg),
20 }
21 }
22}
23
24impl std::error::Error for MqttError {}
25
26impl From<std::io::Error> for MqttError {
27 fn from(err: std::io::Error) -> Self {
28 MqttError::Connection(err.to_string())
29 }
30}
31
32impl From<serde_json::Error> for MqttError {
33 fn from(err: serde_json::Error) -> Self {
34 MqttError::Publish(err.to_string())
35 }
36}