arachnid_core/
error.rs

1/*
2    Appellation: error <module>
3    Created At: 2026.01.10:15:38:55
4    Contrib: @FL03
5*/
6//! this module defines the core error type for the crate
7
8#[cfg(feature = "alloc")]
9use alloc::{boxed::Box, string::String};
10/// a type alias for a [`Result`](core::result::Result) configured to use the custom [`Error`] type.
11pub type Result<T> = core::result::Result<T, Error>;
12
13/// The custom error type for the crate.
14#[derive(Debug, thiserror::Error)]
15#[non_exhaustive]
16pub enum Error {
17    // external errors
18    #[error(transparent)]
19    AnyError(anyhow::Error),
20    // core errors
21    #[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    // standard library errors
36    #[cfg(feature = "std")]
37    #[error(transparent)]
38    IOError(#[from] std::io::Error),
39    // alloc-dependent errors
40    #[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}