acme_core/
error.rs

1/*
2    appellation: error <module>
3    authors: @FL03
4*/
5//! this module defines the base error types for the `acme` platform
6#[cfg(feature = "alloc")]
7use alloc::{
8    boxed::Box,
9    string::{String, ToString},
10};
11
12/// a type alias for a [`Result`](core::result::Result) configured with the custom [`Error`]
13/// for the crate.
14pub type Result<T = ()> = core::result::Result<T, Error>;
15
16/// The [`Error`] type enumerates the various errors that can occur within the `acme` engine.
17#[derive(Debug, thiserror::Error)]
18pub enum Error {
19    #[cfg(feature = "alloc")]
20    #[error("Invalid component name: {0}")]
21    InvalidComponentName(String),
22    #[cfg(feature = "anyhow")]
23    #[error(transparent)]
24    AnyError(#[from] anyhow::Error),
25    #[cfg(feature = "alloc")]
26    #[error(transparent)]
27    BoxError(#[from] Box<dyn core::error::Error + Send + Sync>),
28    #[cfg(feature = "serde")]
29    #[error(transparent)]
30    DeserializeError(#[from] serde::de::value::Error),
31    #[cfg(feature = "std")]
32    #[error(transparent)]
33    IOError(#[from] std::io::Error),
34    #[cfg(feature = "serde_json")]
35    #[error(transparent)]
36    JsonError(#[from] serde_json::Error),
37    #[cfg(feature = "alloc")]
38    #[error("Unknown Error: {0}")]
39    Unknown(String),
40}
41
42#[cfg(feature = "alloc")]
43impl Error {
44    /// create a new [`BoxError`](Error::BoxError) error variant using the provided boxed error.
45    pub fn box_error<E>(error: E) -> Self
46    where
47        E: 'static + core::error::Error + Send + Sync,
48    {
49        Error::BoxError(Box::new(error))
50    }
51    /// create a new [`Unknown`](Error::Unknown) error variant using the provided value.
52    pub fn unknown<T>(value: T) -> Self
53    where
54        T: ToString,
55    {
56        Error::Unknown(value.to_string())
57    }
58}
59
60#[cfg(feature = "alloc")]
61impl From<String> for Error {
62    fn from(value: String) -> Self {
63        Error::Unknown(value)
64    }
65}
66
67#[cfg(feature = "alloc")]
68impl From<&str> for Error {
69    fn from(value: &str) -> Self {
70        Error::Unknown(String::from(value))
71    }
72}