scotch_host/
error.rs

1use std::{
2    error::Error,
3    fmt::{self, Display},
4};
5
6use bincode::error::{DecodeError, EncodeError};
7use wasmer::{ExportError, MemoryAccessError, RuntimeError};
8
9/// Error for everything that can go wrong.
10#[derive(Debug)]
11pub enum ScotchHostError {
12    EncodingFailed(EncodeError),
13    DecodingFailed(DecodeError),
14    MemoryAccessFailed(MemoryAccessError),
15    AllocFailed(RuntimeError),
16    FreeFailed(RuntimeError),
17    MemoryMissing(ExportError),
18    AllocMissing(ExportError),
19    FreeMissing(ExportError),
20}
21
22impl Display for ScotchHostError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "{self:?}")
25    }
26}
27
28impl Error for ScotchHostError {}
29
30macro_rules! impl_from {
31    ($target:ident, $($var:ident : $type:ty),*$(,)?) => {
32        $(
33            impl From<$type> for $target {
34                #[inline]
35                fn from(v: $type) -> Self {
36                    Self::$var(v)
37                }
38            }
39        )*
40    }
41}
42
43impl_from!(
44    ScotchHostError,
45    EncodingFailed: EncodeError,
46    DecodingFailed: DecodeError,
47    MemoryAccessFailed: MemoryAccessError,
48);