eryon_actors/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: @FL03
4*/
5#[cfg(feature = "alloc")]
6use alloc::{
7    boxed::Box,
8    string::{String, ToString},
9};
10
11/// a type alias for a [`Result`](core::result::Result) equipped with the [`ActorError`] type
12pub(crate) type Result<T = ()> = core::result::Result<T, ActorError>;
13
14#[derive(Debug, thiserror::Error)]
15pub enum ActorError {
16    #[cfg(feature = "alloc")]
17    #[error("Driver Error: {0}")]
18    DriverError(String),
19    #[error("Generative Error: {0}")]
20    GenerativeError(String),
21    #[error("Invalid Critical Point")]
22    InvalidCriticalPoint,
23    #[error("Actor is in an infinite loop")]
24    InfiniteLoop,
25    #[cfg(feature = "alloc")]
26    #[error("Invalid Shape: {0}")]
27    InvalidShape(String),
28    #[cfg(feature = "alloc")]
29    #[error("Invalid Parameter: {0}")]
30    InvalidParameter(String),
31    #[error("Actor is in an invalid state")]
32    InvalidState,
33    #[cfg(feature = "alloc")]
34    #[error("Not Found: {0}")]
35    NotFound(String),
36    #[cfg(feature = "alloc")]
37    #[error("No path found between {src} and {tgt}")]
38    NoPathFound { src: String, tgt: String },
39    #[error("No rule found")]
40    NoRuleFound,
41    #[error("{0} is not initialized")]
42    NotInitialized(String),
43    #[error("Transformation error")]
44    TransformationError,
45    #[error("Actor is in an unknown state")]
46    UnknownState,
47    #[error("Unknown Symbol")]
48    UnknownSymbol,
49    #[error(transparent)]
50    CoreError(#[from] eryon::Error),
51    #[error(transparent)]
52    MemoryError(#[from] eryon_mem::MemoryError),
53    #[error(transparent)]
54    SurfaceError(#[from] eryon_surface::SurfaceError),
55    #[error(transparent)]
56    CncError(#[from] cnc::Error),
57    #[error(transparent)]
58    NeuralError(#[from] cnc::nn::NeuralError),
59}
60
61impl ActorError {
62    /// returns a new [`NoRuleFound`](ActorError::NoRuleFound) variant
63    pub const fn no_rule_found() -> Self {
64        ActorError::NoRuleFound
65    }
66}
67
68#[cfg(feature = "alloc")]
69impl ActorError {
70    /// returns a new [`NotFound`](ActorError::NotFound) variant
71    pub fn not_found(what: impl ToString) -> Self {
72        ActorError::NotFound(what.to_string())
73    }
74    /// returns a new [`NotInitialized`](ActorError::NotInitialized) variant
75    pub fn not_initialized(what: impl ToString) -> Self {
76        ActorError::NotInitialized(what.to_string())
77    }
78    /// returns a new [`InvalidParameter`](ActorError::InvalidParameter) variant
79    pub fn invalid_parameter(what: impl ToString) -> Self {
80        ActorError::InvalidParameter(what.to_string())
81    }
82    /// returns a new [`NoPathFound`](ActorError::NoPathFound) variant
83    pub fn no_path_found(src: impl ToString, tgt: impl ToString) -> Self {
84        ActorError::NoPathFound {
85            src: src.to_string(),
86            tgt: tgt.to_string(),
87        }
88    }
89}
90
91impl From<ActorError> for eryon::Error {
92    fn from(err: ActorError) -> Self {
93        match err {
94            ActorError::CoreError(e) => e,
95            _ => eryon::Error::BoxError(Box::new(err)),
96        }
97    }
98}
99#[cfg(feature = "alloc")]
100impl From<&str> for ActorError {
101    fn from(err: &str) -> Self {
102        eryon::Error::from(err).into()
103    }
104}
105#[cfg(feature = "alloc")]
106impl From<String> for ActorError {
107    fn from(err: String) -> Self {
108        eryon::Error::from(err).into()
109    }
110}