Skip to main content

cymule_runtime/
error.rs

1use std::fmt::{Display, Formatter};
2
3/// Runtime result type.
4pub type RuntimeResult<T> = std::result::Result<T, RuntimeError>;
5
6/// Stable embedded-runtime errors.
7#[derive(Debug)]
8pub enum RuntimeError {
9    /// Trusted semantic kernel rejected an operation.
10    Core(cymule_core::CoreError),
11    /// Plugin protocol or behavior was invalid.
12    Plugin(String),
13    /// IR execution reached a durable wait unsupported by one-shot execution.
14    Suspended(String),
15    /// Local process I/O failed.
16    Io(String),
17    /// JSON encoding failed.
18    Encoding(String),
19}
20
21impl Display for RuntimeError {
22    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::Core(error) => Display::fmt(error, formatter),
25            Self::Plugin(message) => write!(formatter, "plugin_error: {message}"),
26            Self::Suspended(message) => write!(formatter, "run_suspended: {message}"),
27            Self::Io(message) => write!(formatter, "io_error: {message}"),
28            Self::Encoding(message) => write!(formatter, "encoding_error: {message}"),
29        }
30    }
31}
32
33impl std::error::Error for RuntimeError {}
34
35impl From<cymule_core::CoreError> for RuntimeError {
36    fn from(error: cymule_core::CoreError) -> Self {
37        Self::Core(error)
38    }
39}
40
41impl From<std::io::Error> for RuntimeError {
42    fn from(error: std::io::Error) -> Self {
43        Self::Io(error.to_string())
44    }
45}
46
47impl From<serde_json::Error> for RuntimeError {
48    fn from(error: serde_json::Error) -> Self {
49        Self::Encoding(error.to_string())
50    }
51}