1#[cfg(feature = "alloc")]
9use alloc::{boxed::Box, string::String};
10pub type Result<T> = core::result::Result<T, Error>;
12
13#[derive(Debug, thiserror::Error)]
15#[non_exhaustive]
16pub enum Error {
17 #[error(transparent)]
19 AnyError(anyhow::Error),
20 #[error(transparent)]
22 AddrParseError(#[from] core::net::AddrParseError),
23 #[error(transparent)]
24 BorrowError(#[from] core::cell::BorrowError),
25 #[error(transparent)]
26 BorrowMutError(#[from] core::cell::BorrowMutError),
27 #[error("The impossible has occurred...")]
28 Infallible(#[from] core::convert::Infallible),
29 #[error(transparent)]
30 FmtError(#[from] core::fmt::Error),
31 #[error(transparent)]
32 TryFromSliceError(#[from] core::array::TryFromSliceError),
33 #[error(transparent)]
34 Utf8Error(#[from] core::str::Utf8Error),
35 #[cfg(feature = "std")]
37 #[error(transparent)]
38 IOError(#[from] std::io::Error),
39 #[cfg(feature = "alloc")]
41 #[error(transparent)]
42 BoxError(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
43 #[cfg(feature = "alloc")]
44 #[error("Unknown Error: {0}")]
45 Unknown(String),
46}
47
48#[cfg(feature = "alloc")]
49mod impl_alloc {
50 use super::Error;
51 use alloc::boxed::Box;
52 use alloc::string::{String, ToString};
53
54 impl Error {
55 pub fn box_error<E>(error: E) -> Self
56 where
57 E: core::error::Error + Send + Sync + 'static,
58 {
59 Self::BoxError(Box::new(error))
60 }
61
62 pub fn unknown<E: ToString>(error: E) -> Self {
63 Self::Unknown(error.to_string())
64 }
65 }
66
67 impl From<&str> for Error {
68 fn from(value: &str) -> Self {
69 Self::Unknown(String::from(value))
70 }
71 }
72
73 impl From<String> for Error {
74 fn from(value: String) -> Self {
75 Self::Unknown(value)
76 }
77 }
78}