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("The specified index ({index}) is out of bounds for a collection of {len} elements.")]
15    IndexOutOfBounds { index: usize, len: usize },
16    #[cfg(feature = "alloc")]
17    #[error("[Type Error] {0}")]
18    TypeError(String),
19    #[cfg(feature = "alloc")]
20    #[error(transparent)]
21    BoxError(Box<dyn core::error::Error + Send + Sync + 'static>),
22    #[error(transparent)]
23    FmtError(core::fmt::Error),
24    #[cfg(feature = "std")]
25    #[error(transparent)]
26    IOError(std::io::Error),
27    #[cfg(feature = "alloc")]
28    #[error("An unknown error occurred: {0}")]
29    Unknown(String),
30    #[error(transparent)]
31    StateError(#[from] rstm_state::StateError),
32}
33
34impl Error {
35    /// returns an [`IndedOutOfBounds`](Error::IndexOutOfBounds) variant
36    pub const fn index_out_of_bounds(index: usize, len: usize) -> Self {
37        Self::IndexOutOfBounds { index, len }
38    }
39}
40
41#[cfg(feature = "alloc")]
42impl From<&str> for Error {
43    fn from(err: &str) -> Self {
44        Error::Unknown(String::from(err))
45    }
46}
47
48#[cfg(feature = "alloc")]
49impl From<String> for Error {
50    fn from(err: String) -> Self {
51        Error::Unknown(err)
52    }
53}