use serde_json::Error as JsonError; use std::{ error::Error as StdError, fmt::{self, Display, Formatter}, io::Error as IoError, result::Result as StdResult, sync::mpsc::RecvTimeoutError as ChannelTimeout, }; use tokio::sync::mpsc::error::SendError; use crate::models::Message; #[derive(Debug)] pub enum Error { IoError(IoError), JsonError(JsonError), Timeout(ChannelTimeout), Conversion, SubscriptionFailed, ConnectionClosed, ConnectionClosedWhileSending(Message), Busy, } impl Display for Error { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { Self::Conversion => f.write_str("Failed to convert values"), Self::SubscriptionFailed => f.write_str("Failed to subscribe to event"), Self::ConnectionClosed => f.write_str("Connection closed"), Self::ConnectionClosedWhileSending(msg) => { write!(f, "Connection closed while sending {:?}", msg) } Self::Busy => f.write_str("A resource was busy"), Self::IoError(err) => write!(f, "{}", err), Self::JsonError(err) => write!(f, "{}", err), Self::Timeout(err) => write!(f, "{}", err), } } } impl StdError for Error {} impl From for Error { fn from(err: IoError) -> Self { Self::IoError(err) } } impl From for Error { fn from(err: JsonError) -> Self { Self::JsonError(err) } } impl From for Error { fn from(err: ChannelTimeout) -> Self { Self::Timeout(err) } } impl From> for Error { fn from(err: SendError) -> Self { Self::ConnectionClosedWhileSending(err.0) } } pub type Result = StdResult;