stak_dynamic/
error.rs

1use any_fn::AnyFnError;
2use core::{
3    error::Error,
4    fmt::{self, Display, Formatter},
5};
6use stak_vm::Exception;
7
8/// An error.
9#[derive(Debug)]
10pub enum DynamicError {
11    /// An `AnyFn` error.
12    AnyFn(AnyFnError),
13    /// A foreign value expected.
14    ForeignValueExpected,
15    /// A value index error.
16    ValueIndex,
17    /// A virtual machine error.
18    Vm(stak_vm::Error),
19}
20
21impl From<AnyFnError> for DynamicError {
22    fn from(error: AnyFnError) -> Self {
23        Self::AnyFn(error)
24    }
25}
26
27impl From<stak_vm::Error> for DynamicError {
28    fn from(error: stak_vm::Error) -> Self {
29        Self::Vm(error)
30    }
31}
32
33impl Exception for DynamicError {
34    fn is_critical(&self) -> bool {
35        match self {
36            Self::AnyFn(_) | Self::ForeignValueExpected | Self::ValueIndex => true,
37            Self::Vm(error) => error.is_critical(),
38        }
39    }
40}
41
42impl Error for DynamicError {}
43
44impl Display for DynamicError {
45    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
46        match self {
47            Self::AnyFn(error) => write!(formatter, "{error}"),
48            Self::ForeignValueExpected => write!(formatter, "foreign value expected"),
49            Self::ValueIndex => write!(formatter, "invalid value index"),
50            Self::Vm(error) => write!(formatter, "{error}"),
51        }
52    }
53}