use std::convert::Infallible;
use std::sync::Arc;
#[cfg(not(feature = "async-tungstenite09"))]
use async_tungstenite::tungstenite;
#[cfg(feature = "async-tungstenite09")]
use async_tungstenite09::tungstenite;
use thiserror::Error;
#[derive(Debug, Error, Clone)]
pub enum Error {
#[error("websocket error: {0}")]
WebSocket(#[source] Arc<tungstenite::Error>),
#[error("websocket unexpected message: {0}")]
UnexpectedMessage(tungstenite::Message),
#[error("JSON error: {0}")]
Json(#[source] Arc<serde_json::Error>),
}
impl From<Infallible> for Error {
fn from(x: Infallible) -> Error {
match x {}
}
}
impl From<url::ParseError> for Error {
fn from(_: url::ParseError) -> Error {
tungstenite::Error::Url("Failed to parse URL".into()).into()
}
}
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))
}
}
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>();
}
}