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
#[cfg(feature = "events")]
use std::sync::Arc;

#[derive(Debug)]
pub enum DecthingsClientError {
    #[cfg(not(target_os = "espidf"))]
    /// The HTTP request to Decthings failed unexpectedly, for example due to a network error.
    /// If Decthings returned an error, DecthingsRpcError::Rpc should have been returned instead.
    Http(reqwest::Error),

    #[cfg(target_os = "espidf")]
    /// The HTTP request to Decthings failed unexpectedly, for example due to a network error.
    /// If Decthings returned an error, DecthingsRpcError::Rpc should have been returned instead.
    Http(esp_idf_svc::sys::EspError),

    #[cfg(feature = "events")]
    /// Failed to connect websocket to Decthings.
    WebSocketConnect(Arc<tokio_tungstenite::tungstenite::Error>),

    #[cfg(feature = "events")]
    /// Failed to write websocket data to Decthings.
    WebSocketWrite(Arc<tokio_tungstenite::tungstenite::Error>),

    #[cfg(feature = "events")]
    /// Failed to read websocket data from Decthings.
    WebSocketRead(Arc<tokio_tungstenite::tungstenite::Error>),

    /// JSON parse failed for the data received from Decthings.
    ParseResponseFailed(serde_json::Error),

    /// The data received by Decthings was invalid.
    InvalidMessage,
}

#[cfg(target_os = "espidf")]
impl From<esp_idf_svc::sys::EspError> for DecthingsClientError {
    fn from(value: esp_idf_svc::sys::EspError) -> Self {
        DecthingsClientError::Http(value)
    }
}

#[cfg(target_os = "espidf")]
impl From<esp_idf_svc::io::EspIOError> for DecthingsClientError {
    fn from(value: esp_idf_svc::io::EspIOError) -> Self {
        DecthingsClientError::Http(value.0)
    }
}

impl std::fmt::Display for DecthingsClientError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for DecthingsClientError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Http(e) => Some(e),
            #[cfg(feature = "events")]
            Self::WebSocketConnect(e) => Some(e),
            #[cfg(feature = "events")]
            Self::WebSocketRead(e) => Some(e),
            #[cfg(feature = "events")]
            Self::WebSocketWrite(e) => Some(e),
            Self::ParseResponseFailed(e) => Some(e),
            Self::InvalidMessage => None,
        }
    }
}

#[cfg(feature = "events")]
impl From<super::websocket::WebSocketClientError> for DecthingsClientError {
    fn from(x: super::websocket::WebSocketClientError) -> Self {
        match x {
            super::websocket::WebSocketClientError::Connect(e) => {
                DecthingsClientError::WebSocketConnect(e)
            }
            super::websocket::WebSocketClientError::Write(e) => {
                DecthingsClientError::WebSocketWrite(e)
            }
            super::websocket::WebSocketClientError::Read(e) => {
                DecthingsClientError::WebSocketRead(e)
            }
            super::websocket::WebSocketClientError::InvalidMessage => {
                DecthingsClientError::InvalidMessage
            }
        }
    }
}

#[derive(Debug)]
pub enum DecthingsRpcError<E> {
    /// The request failed, for example network or JSON error.
    Request(DecthingsClientError),
    /// The request was successful, but an error was returned by Decthings.
    Rpc(E),
}

impl<E: std::fmt::Debug> std::fmt::Display for DecthingsRpcError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl<E> From<serde_json::Error> for DecthingsRpcError<E> {
    fn from(x: serde_json::Error) -> Self {
        Self::Request(DecthingsClientError::ParseResponseFailed(x))
    }
}

impl<E> From<DecthingsClientError> for DecthingsRpcError<E> {
    fn from(x: DecthingsClientError) -> Self {
        Self::Request(x)
    }
}

impl<E: std::fmt::Debug> std::error::Error for DecthingsRpcError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Request(e) => Some(e),
            Self::Rpc(_) => None,
        }
    }
}