#![allow(clippy::module_name_repetitions)]
use core::{
fmt::{self, Formatter},
result,
};
#[cfg(feature = "std")]
use std::{
error,
ffi::NulError,
io::{self, IntoInnerError},
};
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub type ErrorKind = io::ErrorKind;
pub type Result<T> = result::Result<T, Error>;
pub struct Error {
#[cfg(feature = "std")]
inner: io::Error,
#[cfg(not(feature = "std"))]
pub(crate) inner: &'static str,
}
#[macro_export]
macro_rules! error {
($variant:ident, $message:literal) => {
$crate::__error_impl!($variant, $message)
};
}
#[cfg(feature = "std")]
#[doc(hidden)]
#[macro_export]
macro_rules! __error_impl {
($variant:ident, $message:literal) => {
$crate::Error::new($crate::ErrorKind::$variant, $message)
};
}
#[cfg(not(feature = "std"))]
#[doc(hidden)]
#[macro_export]
macro_rules! __error_impl {
($variant:ident, $message:literal) => {
$crate::Error::__const_error($message)
};
}
#[cfg(not(feature = "std"))]
impl Error {
#[doc(hidden)]
#[must_use]
pub const fn __const_error(message: &'static str) -> Error {
Error { inner: message }
}
}
#[cfg(feature = "std")]
impl Error {
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn new<E>(kind: ErrorKind, error: E) -> Error
where
E: Into<Box<dyn error::Error + Send + Sync>>,
{
io::Error::new(kind, error).into()
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn other<E>(error: E) -> Error
where
E: Into<Box<dyn error::Error + Send + Sync>>,
{
io::Error::new(ErrorKind::Other, error).into()
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn last_os_error() -> Error {
io::Error::last_os_error().into()
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn from_raw_os_error(code: i32) -> Error {
let inner = io::Error::from_raw_os_error(code);
Error { inner }
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn raw_os_error(&self) -> Option<i32> {
self.inner.raw_os_error()
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
self.inner.get_ref()
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
self.inner.get_mut()
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline]
pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
self.inner.into_inner()
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[must_use]
#[inline]
pub fn kind(&self) -> ErrorKind {
self.inner.kind()
}
}
impl fmt::Debug for Error {
#[cfg(not(feature = "std"))]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Error")
.field("message", &self.inner)
.finish()
}
#[cfg(feature = "std")]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
impl fmt::Display for Error {
#[cfg(not(feature = "std"))]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.inner)
}
#[cfg(feature = "std")]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl error::Error for Error {
#[inline]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
self.inner.source()
}
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl From<io::Error> for Error {
fn from(inner: io::Error) -> Self {
Error { inner }
}
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl From<Error> for io::Error {
#[inline]
fn from(error: Error) -> Self {
error.inner
}
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl From<ErrorKind> for Error {
#[inline]
fn from(kind: ErrorKind) -> Self {
io::Error::from(kind).into()
}
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl<W> From<IntoInnerError<W>> for Error {
#[inline]
fn from(error: IntoInnerError<W>) -> Self {
io::Error::from(error).into()
}
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl From<NulError> for Error {
#[inline]
fn from(error: NulError) -> Self {
io::Error::from(error).into()
}
}