contained_core/
error.rs

1/*
2    appellation: error <module>
3    authors: @FL03
4*/
5//! this module defines the core error type for the crate
6
7/// a type alias for a [`Result`](core::result::Result) configured to use the custom [`Error`] type.
8pub type Result<T> = core::result::Result<T, Error>;
9
10/// The custom error type for the crate.
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    #[cfg(feature = "alloc")]
14    #[error(transparent)]
15    BoxError(#[from] alloc::boxed::Box<dyn core::error::Error + Send + Sync + 'static>),
16    #[error(transparent)]
17    FmtError(#[from] core::fmt::Error),
18    #[cfg(feature = "std")]
19    #[error(transparent)]
20    IOError(#[from] std::io::Error),
21    #[cfg(feature = "alloc")]
22    #[error("Unknown Error: {0}")]
23    Unknown(alloc::string::String),
24}
25
26#[cfg(feature = "alloc")]
27mod impl_alloc {
28    use super::Error;
29    use alloc::boxed::Box;
30    use alloc::string::String;
31
32    impl Error {
33        pub fn box_error<E>(error: E) -> Self
34        where
35            E: core::error::Error + Send + Sync + 'static,
36        {
37            Self::BoxError(Box::new(error))
38        }
39    }
40
41    impl From<&str> for Error {
42        fn from(value: &str) -> Self {
43            Self::Unknown(String::from(value))
44        }
45    }
46
47    impl From<String> for Error {
48        fn from(value: String) -> Self {
49            Self::Unknown(value)
50        }
51    }
52}