rstm_core/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5#[cfg(feature = "alloc")]
6use alloc::{boxed::Box, string::String};
7
8/// A type alias for a [Result] with our custom error type: [`Error`](crate::Error)
9pub type Result<T = ()> = core::result::Result<T, crate::Error>;
10
11/// The [`Error`] implementation describes the various errors that can occur within the library
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14    #[error("[Index Error] Out of Bounds: {index} is out of bounds for a length of {len}")]
15    IndexOutOfBounds { index: usize, len: usize },
16    #[cfg(feature = "alloc")]
17    #[error("[Execution Error] {0}")]
18    ExecutionError(String),
19    #[cfg(feature = "alloc")]
20    #[error("[Runtime Error] {0}")]
21    RuntimeError(String),
22    #[error("[State Error] {0}")]
23    StateError(#[from] rstm_state::StateError),
24    #[cfg(feature = "alloc")]
25    #[error("[Transformation Error]: {0}")]
26    TransformationError(String),
27    #[cfg(feature = "alloc")]
28    #[error("[Type Error] {0}")]
29    TypeError(String),
30    #[cfg(feature = "alloc")]
31    #[error(transparent)]
32    BoxError(Box<dyn core::error::Error + Send + Sync + 'static>),
33    #[error(transparent)]
34    FmtError(core::fmt::Error),
35    #[cfg(feature = "std")]
36    #[error(transparent)]
37    IoError(std::io::Error),
38    #[cfg(feature = "alloc")]
39    #[error("[Unknown Error] {0}")]
40    Unknown(String),
41}
42
43impl Error {
44    /// returns an [`IndedOutOfBounds`](Error::IndexOutOfBounds) variant
45    pub const fn index_out_of_bounds(index: usize, len: usize) -> Self {
46        Self::IndexOutOfBounds { index, len }
47    }
48}
49
50#[cfg(feature = "alloc")]
51impl From<&str> for Error {
52    fn from(err: &str) -> Self {
53        Error::Unknown(String::from(err))
54    }
55}
56
57#[cfg(feature = "alloc")]
58impl From<String> for Error {
59    fn from(err: String) -> Self {
60        Error::Unknown(err)
61    }
62}