mock_io/
error.rs

1//! Error types used in this crate
2use std::{error::Error as IError, fmt};
3
4use thiserror::Error;
5
6/// Error returned by functions in this crate
7#[derive(Debug)]
8pub struct Error {
9    inner: Inner,
10}
11
12impl Error {
13    /// Returns the kind of error
14    pub fn kind(&self) -> ErrorKind {
15        (&self.inner).into()
16    }
17}
18
19impl fmt::Display for Error {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        self.kind().fmt(f)
22    }
23}
24
25impl IError for Error {
26    fn source(&self) -> Option<&(dyn IError + 'static)> {
27        self.inner.source()
28    }
29}
30
31#[doc(hidden)]
32impl From<Error> for std::io::Error {
33    fn from(error: Error) -> Self {
34        std::io::Error::new(std::io::ErrorKind::Other, error)
35    }
36}
37
38#[doc(hidden)]
39impl<T: Into<Inner>> From<T> for Error {
40    fn from(inner: T) -> Self {
41        Self {
42            inner: inner.into(),
43        }
44    }
45}
46
47/// Different kinds of possible errors returned by functions in this crate
48#[derive(Debug)]
49#[non_exhaustive]
50pub enum ErrorKind {
51    /// Stream connecting error
52    StreamConnectError,
53    /// Channel receiving error
54    ChannelRecvError,
55    /// Channel sending error
56    ChannelSendError,
57    /// Other error
58    Other,
59}
60
61impl fmt::Display for ErrorKind {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self {
64            Self::StreamConnectError => write!(f, "Stream connecting error"),
65            Self::ChannelRecvError => write!(f, "Channel receiving error"),
66            Self::ChannelSendError => write!(f, "Channel sending error"),
67            Self::Other => write!(f, "Other error"),
68        }
69    }
70}
71
72#[doc(hidden)]
73#[derive(Debug, Error)]
74pub enum Inner {
75    #[cfg(feature = "sync")]
76    #[error("Sync stream connecting error")]
77    SyncConnectError,
78    #[cfg(feature = "sync")]
79    #[error("Sync channel receiving error: {0}")]
80    SyncRecvError(#[from] std::sync::mpsc::RecvError),
81    #[cfg(feature = "sync")]
82    #[error("Sync channel sending error: {0}")]
83    SyncSendError(#[from] std::sync::mpsc::SendError<Vec<u8>>),
84    #[cfg(feature = "async-futures")]
85    #[error("Async stream connecting error")]
86    AsyncConnectError(#[from] async_channel::SendError<crate::futures::MockStream>),
87    #[cfg(feature = "async-futures")]
88    #[error("Async channel receiving error: {0}")]
89    AsyncRecvError(#[from] async_channel::RecvError),
90    #[cfg(feature = "async-futures")]
91    #[error("Async channel sending error: {0}")]
92    AsyncSendError(#[from] async_channel::SendError<Vec<u8>>),
93    #[cfg(feature = "async-tokio")]
94    #[error("Tokio stream connecting error")]
95    TokioConnectError(#[from] tokio::sync::mpsc::error::SendError<crate::tokio::MockStream>),
96    #[cfg(feature = "async-tokio")]
97    #[error("Tokio channel receiving error")]
98    TokioRecvError,
99    #[cfg(feature = "async-tokio")]
100    #[error("Tokio channel sending error: {0}")]
101    TokioSendError(#[from] tokio::sync::mpsc::error::SendError<Vec<u8>>),
102    #[error("Other error")]
103    Other,
104}
105
106impl<'a> From<&'a Inner> for ErrorKind {
107    fn from(inner: &'a Inner) -> Self {
108        match inner {
109            #[cfg(feature = "sync")]
110            Inner::SyncConnectError => ErrorKind::StreamConnectError,
111            #[cfg(feature = "sync")]
112            Inner::SyncRecvError(_) => ErrorKind::ChannelRecvError,
113            #[cfg(feature = "sync")]
114            Inner::SyncSendError(_) => ErrorKind::ChannelSendError,
115            #[cfg(feature = "async-futures")]
116            Inner::AsyncConnectError(_) => ErrorKind::StreamConnectError,
117            #[cfg(feature = "async-futures")]
118            Inner::AsyncRecvError(_) => ErrorKind::ChannelRecvError,
119            #[cfg(feature = "async-futures")]
120            Inner::AsyncSendError(_) => ErrorKind::ChannelSendError,
121            #[cfg(feature = "async-tokio")]
122            Inner::TokioConnectError(_) => ErrorKind::StreamConnectError,
123            #[cfg(feature = "async-tokio")]
124            Inner::TokioRecvError => ErrorKind::ChannelRecvError,
125            #[cfg(feature = "async-tokio")]
126            Inner::TokioSendError(_) => ErrorKind::ChannelSendError,
127            Inner::Other => ErrorKind::Other,
128        }
129    }
130}
131
132#[cfg(feature = "sync")]
133impl From<std::sync::mpsc::SendError<crate::sync::MockStream>> for Inner {
134    fn from(_: std::sync::mpsc::SendError<crate::sync::MockStream>) -> Self {
135        Self::SyncConnectError
136    }
137}