2021-09-28 23:09:38 +00:00
|
|
|
|
2024-03-02 05:17:09 +00:00
|
|
|
use std::fmt;
|
|
|
|
use moa_host::HostError;
|
|
|
|
|
2023-05-15 04:12:38 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum EmulatorErrorKind {
|
|
|
|
Misc,
|
|
|
|
MemoryAlignment,
|
|
|
|
}
|
|
|
|
|
2024-03-02 05:17:09 +00:00
|
|
|
#[derive(Clone, Debug, thiserror::Error)]
|
2023-06-11 02:29:54 +00:00
|
|
|
pub enum Error {
|
|
|
|
Assertion(String),
|
|
|
|
Breakpoint(String),
|
|
|
|
Emulator(EmulatorErrorKind, String),
|
|
|
|
Processor(u32),
|
2024-03-02 05:17:09 +00:00
|
|
|
Other(String),
|
2021-09-28 23:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
2023-05-15 04:12:38 +00:00
|
|
|
pub fn new<S>(msg: S) -> Error
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2023-06-11 02:29:54 +00:00
|
|
|
Error::Emulator(EmulatorErrorKind::Misc, msg.into())
|
2023-05-15 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn emulator<S>(kind: EmulatorErrorKind, msg: S) -> Error
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2023-06-11 02:29:54 +00:00
|
|
|
Error::Emulator(kind, msg.into())
|
2021-09-28 23:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn processor(native: u32) -> Error {
|
2023-06-11 02:29:54 +00:00
|
|
|
Error::Processor(native)
|
2021-09-28 23:09:38 +00:00
|
|
|
}
|
2021-11-03 22:33:22 +00:00
|
|
|
|
2023-05-15 04:12:38 +00:00
|
|
|
pub fn breakpoint<S>(msg: S) -> Error
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2023-06-11 02:29:54 +00:00
|
|
|
Error::Breakpoint(msg.into())
|
2021-11-03 22:33:22 +00:00
|
|
|
}
|
2022-09-10 06:17:33 +00:00
|
|
|
|
2023-05-15 04:12:38 +00:00
|
|
|
pub fn assertion<S>(msg: S) -> Error
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2023-06-11 02:29:54 +00:00
|
|
|
Error::Assertion(msg.into())
|
2022-09-10 06:17:33 +00:00
|
|
|
}
|
2023-05-23 04:12:05 +00:00
|
|
|
|
2023-06-11 02:29:54 +00:00
|
|
|
pub fn msg(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Error::Assertion(msg) |
|
|
|
|
Error::Breakpoint(msg) |
|
2024-03-02 05:17:09 +00:00
|
|
|
Error::Other(msg) |
|
2023-06-11 02:29:54 +00:00
|
|
|
Error::Emulator(_, msg) => msg.as_str(),
|
|
|
|
Error::Processor(_) => "native exception",
|
|
|
|
}
|
2023-05-23 04:12:05 +00:00
|
|
|
}
|
2021-09-28 23:09:38 +00:00
|
|
|
}
|
|
|
|
|
2024-03-02 05:17:09 +00:00
|
|
|
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<E> From<HostError<E>> for Error {
|
|
|
|
fn from(err: HostError<E>) -> Self {
|
2024-03-15 05:35:02 +00:00
|
|
|
Self::Other("other".to_string())
|
2024-03-02 05:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|