#[cfg(feature = "alloc")]
use alloc::{boxed::Box, string::String};
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
AddrParseError(#[from] core::net::AddrParseError),
#[error(transparent)]
FmtError(#[from] core::fmt::Error),
#[error(transparent)]
Utf8Error(#[from] core::str::Utf8Error),
#[cfg(feature = "std")]
#[error(transparent)]
IOError(#[from] std::io::Error),
#[cfg(feature = "alloc")]
#[error(transparent)]
BoxError(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
#[cfg(feature = "alloc")]
#[error("Unknown Error: {0}")]
Unknown(String),
}
impl Error {
#[cfg(feature = "alloc")]
pub fn boxed<E>(error: E) -> Self
where
E: core::error::Error + Send + Sync + 'static,
{
Self::BoxError(Box::new(error))
}
#[cfg(feature = "alloc")]
pub fn unknown<E>(message: E) -> Self
where
E: alloc::string::ToString,
{
Self::Unknown(message.to_string())
}
}
#[cfg(feature = "alloc")]
impl From<&str> for Error {
fn from(value: &str) -> Self {
Self::unknown(value)
}
}
#[cfg(feature = "alloc")]
impl From<String> for Error {
fn from(value: String) -> Self {
Self::Unknown(value)
}
}