use std::fmt; use std::error::{Error as StdError}; use moa_host::HostError; #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum EmulatorErrorKind { Misc, MemoryAlignment, } #[derive(Clone, Debug, thiserror::Error)] pub enum Error { Assertion(String), Breakpoint(String), Emulator(EmulatorErrorKind, String), Processor(u32), Other(String), } impl Error { pub fn new(msg: S) -> Error where S: Into, { Error::Emulator(EmulatorErrorKind::Misc, msg.into()) } pub fn emulator(kind: EmulatorErrorKind, msg: S) -> Error where S: Into, { Error::Emulator(kind, msg.into()) } pub fn processor(native: u32) -> Error { Error::Processor(native) } pub fn breakpoint(msg: S) -> Error where S: Into, { Error::Breakpoint(msg.into()) } pub fn assertion(msg: S) -> Error where S: Into, { Error::Assertion(msg.into()) } pub fn msg(&self) -> &str { match self { Error::Assertion(msg) | Error::Breakpoint(msg) | Error::Other(msg) | Error::Emulator(_, msg) => msg.as_str(), Error::Processor(_) => "native exception", } } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::Assertion(msg) | Error::Breakpoint(msg) | Error::Other(msg) | Error::Emulator(_, msg) => write!(f, "{}", msg), Error::Processor(_) => write!(f, "native exception"), } } } impl From> for Error { fn from(err: HostError) -> Self { Self::Other(format!("other")) } }