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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::fmt;
use crate::types::Status;
pub type Error = Box<dyn std::error::Error + Send + Sync>;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug)]
pub struct HostCallError<'a> {
function: &'a str,
status: Status,
}
impl<'a> HostCallError<'a> {
pub(crate) fn new(function: &'a str, status: Status) -> Self {
HostCallError { function, status }
}
pub fn module(&self) -> &'a str {
"env"
}
pub fn function(&self) -> &'a str {
self.function
}
pub fn status(&self) -> Status {
self.status
}
}
impl<'a> fmt::Display for HostCallError<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"call to the host ABI function \"{}.{}\" has failed with status code {}",
self.module(),
self.function,
self.status as u32,
)
}
}
impl<'a> std::error::Error for HostCallError<'a> {}
#[derive(Debug)]
pub struct HostResponseError<'a> {
function: &'a str,
error: Error,
}
impl<'a> HostResponseError<'a> {
pub(crate) fn new(function: &'a str, error: Error) -> Self {
HostResponseError { function, error }
}
pub fn module(&self) -> &'a str {
"env"
}
pub fn function(&self) -> &'a str {
self.function
}
}
impl<'a> fmt::Display for HostResponseError<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"failed to parse response from the host ABI function \"{}.{}\": {}",
self.module(),
self.function,
self.error,
)
}
}
impl<'a> std::error::Error for HostResponseError<'a> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&*self.error)
}
}