1use saturnus_core::Halt;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
11pub enum Error {
12 Halted(Halt),
15 Machine(saturnus_core::Error),
18 Objects(saturnus_objects::Error),
23 Message(String),
26}
27
28pub type Result<T, E = Error> = std::result::Result<T, E>;
30
31impl std::fmt::Display for Error {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 Error::Halted(h) => write!(f, "CPU halted: {h}"),
35 Error::Machine(e) => e.fmt(f),
36 Error::Objects(e) => write!(f, "{e:#}"),
37 Error::Message(m) => f.write_str(m),
38 }
39 }
40}
41
42impl std::error::Error for Error {
43 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44 match self {
45 Error::Machine(e) => Some(e),
46 Error::Halted(_) | Error::Objects(_) | Error::Message(_) => None,
48 }
49 }
50}
51
52impl From<Halt> for Error {
53 fn from(h: Halt) -> Self {
54 Self::Halted(h)
55 }
56}
57
58impl From<saturnus_core::Error> for Error {
59 fn from(e: saturnus_core::Error) -> Self {
60 Self::Machine(e)
61 }
62}
63
64impl From<saturnus_objects::Error> for Error {
65 fn from(e: saturnus_objects::Error) -> Self {
66 Self::Objects(e)
67 }
68}
69
70impl From<saturnus_objects::DecodeError> for Error {
71 fn from(e: saturnus_objects::DecodeError) -> Self {
72 Self::Objects(e.into())
73 }
74}
75
76impl From<String> for Error {
77 fn from(message: String) -> Self {
78 Self::Message(message)
79 }
80}
81
82impl From<&str> for Error {
83 fn from(message: &str) -> Self {
84 Self::Message(message.to_string())
85 }
86}