use crate::sys;
use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Error {
InvalidInput,
Os(OsError),
}
impl Error {
#[inline]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "std")]
pub fn into_io(self) -> std::io::Error {
match self {
Self::InvalidInput => std::io::Error::from(std::io::ErrorKind::InvalidInput),
Self::Os(e) => e.into_io(),
}
}
}
impl From<OsError> for Error {
fn from(e: OsError) -> Self {
Self::Os(e)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
#[inline]
fn from(origin: Error) -> Self {
origin.into_io()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidInput => write!(f, "invalid input"),
Self::Os(e) => fmt::Display::fmt(e, f),
}
}
}
impl core::error::Error for Error {}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct OsError(pub(crate) sys::RawOsError);
impl OsError {
#[inline]
pub fn code(self) -> sys::RawOsError {
self.0
}
#[inline]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "std")]
pub fn into_io(self) -> std::io::Error {
std::io::Error::from_raw_os_error(self.code())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "std")]
impl From<OsError> for std::io::Error {
#[inline]
fn from(origin: OsError) -> Self {
origin.into_io()
}
}
impl fmt::Display for OsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "system error code 0x{:X}", self.code())
}
}
impl fmt::Debug for OsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("OsError")
.field(&format_args!("0x{:X}", self.code()))
.finish()
}
}
impl core::error::Error for OsError {}
pub type Result<T> = core::result::Result<T, Error>;