1use thiserror::Error;
2use wasmer::MemoryAccessError;
3
4#[derive(Error, Debug)]
6pub enum Escape {
7 #[error("failed to access memory: {0}")]
8 Memory(#[from] MemoryAccessError),
9
10 #[error("internal error: {0}")]
11 Internal(String),
12
13 #[error("logic error: {0}")]
14 Logical(String),
15
16 #[error("out of ink")]
17 OutOfInk,
18
19 #[error("exit early: {0}")]
20 Exit(u32),
21}
22
23impl Escape {
24 pub fn internal<T>(error: impl Into<String>) -> Result<T, Escape> {
25 Err(Self::Internal(error.into()))
26 }
27
28 pub fn logical<T>(error: impl Into<String>) -> Result<T, Escape> {
29 Err(Self::Logical(error.into()))
30 }
31
32 pub fn out_of_ink<T>() -> Result<T, Escape> {
33 Err(Self::OutOfInk)
34 }
35}
36
37impl From<std::io::Error> for Escape {
38 fn from(err: std::io::Error) -> Self {
39 Self::Internal(err.to_string())
40 }
41}
42
43pub type MaybeEscape = Result<(), Escape>;