Skip to main content

stak_r7rs/small/
error.rs

1use core::{
2    error,
3    fmt::{self, Display, Formatter},
4};
5use stak_vm::Exception;
6
7/// An error of primitives.
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub enum Error {
10    /// A device error.
11    Device(stak_device::PrimitiveError),
12    /// A file error.
13    File(stak_file::PrimitiveError),
14    /// A halt of a virtual machine.
15    Halt,
16    /// A time error.
17    Time(stak_time::PrimitiveError),
18    /// A virtual machine error.
19    Vm(stak_vm::Error),
20}
21
22impl Exception for Error {
23    fn is_critical(&self) -> bool {
24        match self {
25            Self::Device(error) => error.is_critical(),
26            Self::File(error) => error.is_critical(),
27            Self::Halt => true,
28            Self::Time(error) => error.is_critical(),
29            Self::Vm(error) => error.is_critical(),
30        }
31    }
32}
33
34impl error::Error for Error {}
35
36impl Display for Error {
37    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
38        match self {
39            Self::Device(error) => write!(formatter, "{error}"),
40            Self::File(error) => write!(formatter, "{error}"),
41            Self::Halt => write!(formatter, "halt"),
42            Self::Time(error) => write!(formatter, "{error}"),
43            Self::Vm(error) => write!(formatter, "{error}"),
44        }
45    }
46}
47
48impl From<stak_vm::Error> for Error {
49    fn from(error: stak_vm::Error) -> Self {
50        Self::Vm(error)
51    }
52}
53
54impl From<stak_device::PrimitiveError> for Error {
55    fn from(error: stak_device::PrimitiveError) -> Self {
56        Self::Device(error)
57    }
58}
59
60impl From<stak_file::PrimitiveError> for Error {
61    fn from(error: stak_file::PrimitiveError) -> Self {
62        Self::File(error)
63    }
64}
65
66impl From<stak_time::PrimitiveError> for Error {
67    fn from(error: stak_time::PrimitiveError) -> Self {
68        Self::Time(error)
69    }
70}