1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::fmt;
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<S>(msg: S) -> Error
    where
        S: Into<String>,
    {
        Error::Emulator(EmulatorErrorKind::Misc, msg.into())
    }

    pub fn emulator<S>(kind: EmulatorErrorKind, msg: S) -> Error
    where
        S: Into<String>,
    {
        Error::Emulator(kind, msg.into())
    }

    pub fn processor(native: u32) -> Error {
        Error::Processor(native)
    }

    pub fn breakpoint<S>(msg: S) -> Error
    where
        S: Into<String>,
    {
        Error::Breakpoint(msg.into())
    }

    pub fn assertion<S>(msg: S) -> Error
    where
        S: Into<String>,
    {
        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<E> From<HostError<E>> for Error {
    fn from(_err: HostError<E>) -> Self {
        Self::Other("other".to_string())
    }
}

impl From<fmt::Error> for Error {
    fn from(err: fmt::Error) -> Self {
        Self::Other(format!("{:?}", err))
    }
}