use sealed::Sealed;
use std::{
error::Error,
fmt,
io::{
self,
ErrorKind::{
AddrInUse, AddrNotAvailable, AlreadyExists, BrokenPipe, ConnectionAborted,
ConnectionRefused, ConnectionReset, Interrupted, InvalidData, InvalidInput,
NotConnected, NotFound, OutOfMemory, PermissionDenied, TimedOut, UnexpectedEof,
Unsupported, WouldBlock, WriteZero,
},
},
};
#[doc(inline)]
pub use context::{context, with};
mod context;
mod sealed {
pub trait Sealed: Into<std::io::Error> {}
}
macro_rules! ctor {
($($name:ident -> $kind:expr),* $(,)?) => {
$(
#[doc = concat!(
"Create an [`io::Error`] with kind [`",
stringify!($kind),
"`], wrapping the passed in `error`."
)]
fn $name(error: impl Into<Box<dyn Error + Send + Sync>>) -> io::Error {
io::Error::new($kind, error)
}
)*
};
}
pub trait IoErrorExt: Sealed {
ctor! {
addr_in_use -> AddrInUse,
addr_not_available -> AddrNotAvailable,
already_exists -> AlreadyExists,
broken_pipe -> BrokenPipe,
connection_aborted -> ConnectionAborted,
connection_refused -> ConnectionRefused,
connection_reset -> ConnectionReset,
interrupted -> Interrupted,
invalid_data -> InvalidData,
invalid_input -> InvalidInput,
not_connected -> NotConnected,
not_found -> NotFound,
out_of_memory -> OutOfMemory,
permission_denied -> PermissionDenied,
timed_out -> TimedOut,
unexpected_eof -> UnexpectedEof,
unsupported -> Unsupported,
would_block -> WouldBlock,
write_zero -> WriteZero,
}
fn context(self, msg: impl fmt::Display) -> io::Error {
context(self.into(), msg)
}
fn io_context(self, msg: impl fmt::Display) -> io::Error {
self.context(msg)
}
}
impl Sealed for io::Error {}
impl IoErrorExt for io::Error {}