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
#[derive(Debug, Default)]
pub struct Trap {
    _private: (),
}

impl core::fmt::Display for Trap {
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
        // TODO: We should print out the exact reason for the trap.
        fmt.write_str("execution trapped")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Trap {}

#[derive(Debug)]
pub enum ExecutionError<T> {
    Trap(Trap),
    Error(T),
    OutOfGas,
}

impl<T> From<T> for ExecutionError<T> {
    fn from(error: T) -> Self {
        ExecutionError::Error(error)
    }
}

impl<T> core::fmt::Display for ExecutionError<T>
where
    T: core::fmt::Display,
{
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            ExecutionError::Trap(trap) => trap.fmt(fmt),
            ExecutionError::Error(error) => error.fmt(fmt),
            ExecutionError::OutOfGas => fmt.write_str("out of gas"),
        }
    }
}

#[cfg(feature = "std")]
impl<T> std::error::Error for ExecutionError<T> where T: core::fmt::Debug + core::fmt::Display {}