scsys_core/
error.rs

1/*
2    Appellation: error <module>
3    Created At: 2025.09.08:18:06:45
4    Contrib: @FL03
5*/
6//! ths module implements various error-handling primitives and utilities
7#[cfg(feature = "alloc")]
8use alloc::{boxed::Box, string::String};
9
10/// a type alias for a [`Result`](core::result::Result) that uses the custom [`Error`] type
11pub type Result<T = ()> = core::result::Result<T, Error>;
12
13#[derive(Debug, thiserror::Error)]
14#[non_exhaustive]
15pub enum Error {
16    #[error(transparent)]
17    StateError(#[from] scsys_state::error::Error),
18    #[error(transparent)]
19    #[cfg(feature = "time")]
20    TimeError(#[from] scsys_time::error::Error),
21    #[cfg(feature = "alloc")]
22    #[error(transparent)]
23    BoxError(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
24    #[error(transparent)]
25    FmtError(#[from] core::fmt::Error),
26    #[cfg(feature = "std")]
27    #[error(transparent)]
28    IoError(#[from] std::io::Error),
29    #[cfg(feature = "json")]
30    #[error(transparent)]
31    JsonError(#[from] serde_json::Error),
32    #[cfg(feature = "alloc")]
33    #[error("Unknown error: {0}")]
34    Unknown(String),
35}
36
37#[cfg(feature = "alloc")]
38mod impl_alloc {
39    use super::Error;
40    use alloc::{boxed::Box, string::String};
41
42    impl Error {
43        pub fn box_error<E>(error: E) -> Self
44        where
45            E: core::error::Error + Send + Sync + 'static,
46        {
47            Self::BoxError(Box::new(error))
48        }
49    }
50
51    impl From<String> for Error {
52        fn from(value: String) -> Self {
53            Self::Unknown(value)
54        }
55    }
56
57    impl From<&str> for Error {
58        fn from(value: &str) -> Self {
59            Self::Unknown(String::from(value))
60        }
61    }
62}