Skip to main content

gvf_core/
error.rs

1/*
2    appellation: error <module>
3    authors: @FL03
4*/
5//! this module defines the [`Error`] type for the `gvf` framework, enumerating various
6//! errors types that commonly occur within the framework.
7#[cfg(feature = "alloc")]
8use alloc::{boxed::Box, string::String};
9
10/// a type alias for [`Result`](core::result::Result) that uses the custom [`Error`] type
11pub type Result<T = ()> = core::result::Result<T, Error>;
12
13/// The [`Error`] type for the `gvf` framework, enumerating various error types that commonly
14/// occur within the framework.
15#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum Error {
18    #[cfg(feature = "anyhow")]
19    #[error(transparent)]
20    AnyError(#[from] anyhow::Error),
21    #[cfg(feature = "alloc")]
22    #[error(transparent)]
23    BoxError(#[from] Box<dyn core::error::Error + Send + Sync>),
24    #[error(transparent)]
25    FmtError(#[from] core::fmt::Error),
26    #[cfg(feature = "json")]
27    #[error(transparent)]
28    JsonError(#[from] serde_json::Error),
29    #[cfg(feature = "std")]
30    #[error(transparent)]
31    IOError(#[from] std::io::Error),
32    #[cfg(feature = "alloc")]
33    #[error("Unknown error: {0}")]
34    UnknownError(String),
35}
36
37#[cfg(feature = "alloc")]
38impl From<String> for Error {
39    fn from(value: String) -> Self {
40        Error::UnknownError(value)
41    }
42}
43
44#[cfg(feature = "alloc")]
45impl From<&str> for Error {
46    fn from(value: &str) -> Self {
47        Error::UnknownError(String::from(value))
48    }
49}