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
use std::convert::Infallible;
use std::sync::Arc;

use async_tungstenite::tungstenite;
use thiserror::Error;

/// Possible errors from WebSocket client.
#[derive(Debug, Error, Clone)]
pub enum Error {
    /// Errors from underlying [tungstenite](https://docs.rs/tungstenite) library.
    #[error("websocket error: {0}")]
    WebSocket(#[source] Arc<tungstenite::Error>),
    /// Received unexpected message from server.
    #[error("websocket unexpected message: {0}")]
    UnexpectedMessage(tungstenite::Message),
    /// Invalid URL.
    #[error("Invalid URL: {0}")]
    Url(#[from] url::ParseError),
    /// JSON encode/decode error.
    #[error("JSON error: {0}")]
    Json(#[source] Arc<serde_json::Error>),
}

impl From<Infallible> for Error {
    fn from(x: Infallible) -> Error {
        match x {}
    }
}

impl From<tungstenite::Error> for Error {
    fn from(err: tungstenite::Error) -> Error {
        Error::WebSocket(Arc::new(err))
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Error {
        Error::Json(Arc::new(err))
    }
}

/// Specialized Result type for operations on [`WebSocketClient`][`crate::WebSocketClient`].
pub type Result<T> = std::result::Result<T, Error>;

#[cfg(test)]
mod tests {
    use super::Error;

    #[test]
    fn test_send() {
        fn assert_send<T: Send>() {}
        assert_send::<Error>();
    }

    #[test]
    fn test_sync() {
        fn assert_send<T: Sync>() {}
        assert_send::<Error>();
    }
}