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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#[cfg(doc)]
use super::*;
use std::error::Error;
use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum MachineErrorKind {
Memory,
Syntax,
ExecLimit,
Runtime,
Data,
Unknown,
}
impl Default for MachineErrorKind {
fn default() -> Self {
MachineErrorKind::Unknown
}
}
#[derive(Clone, Default)]
pub struct MachineError {
pub kind: MachineErrorKind,
pub message: Option<String>,
pub machine_backtrace: Option<String>,
pub message_incl_chunk_name: bool,
pub chunk_name: Option<String>,
pub message_incl_pos: bool,
pub line: Option<usize>,
pub column: Option<usize>,
pub position: Option<usize>,
}
impl Error for MachineError {}
impl fmt::Display for MachineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
MachineErrorKind::Memory => write!(f, "VM memory exhausted")?,
MachineErrorKind::Syntax => write!(f, "syntax error in code for VM")?,
MachineErrorKind::ExecLimit => write!(f, "VM execution limit exhausted")?,
MachineErrorKind::Runtime => write!(f, "runtime error in VM")?,
MachineErrorKind::Data => write!(f, "unexpected data from VM")?,
_ => write!(f, "VM error")?,
}
let mut chunk_name_written = false;
if !self.message_incl_chunk_name {
if let Some(ref chunk_name) = self.chunk_name {
write!(f, " in chunk \"{}\"", chunk_name)?;
chunk_name_written = true;
}
}
if !self.message_incl_pos {
if let Some(line) = self.line {
write!(
f,
"{} line {}",
if chunk_name_written { ", " } else { " in" },
line
)?;
if let Some(column) = self.column {
write!(f, ", column {}", column)?;
}
} else if let Some(position) = self.position {
write!(f, ", at byte position {}", position)?;
}
}
if let Some(msg) = &self.message {
write!(f, ": {}", msg)?;
}
Ok(())
}
}
impl fmt::Debug for MachineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MachineError")
.field("kind", &self.kind)
.field("message", &self.message)
.field("message_incl_pos", &self.message_incl_pos)
.field("chunk_name", &self.chunk_name)
.field("line", &self.line)
.field("column", &self.column)
.field("position", &self.position)
.finish_non_exhaustive()
}
}
#[derive(Debug)]
pub struct DatumViewError {
pub details: &'static str,
}
impl DatumViewError {
pub fn new(details: &'static str) -> Self {
DatumViewError { details }
}
}
impl fmt::Display for DatumViewError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.details)
}
}
impl Error for DatumViewError {}
impl From<DatumViewError> for MachineError {
fn from(datum_err: DatumViewError) -> Self {
MachineError {
kind: MachineErrorKind::Data,
message: Some(datum_err.to_string()),
..Default::default()
}
}
}
pub struct DatumConversionError<D> {
pub original: D,
pub details: &'static str,
}
impl<D> DatumConversionError<D> {
pub fn new(original: D, details: &'static str) -> Self {
DatumConversionError { original, details }
}
}
impl<D> fmt::Display for DatumConversionError<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, ": {}", self.details)
}
}
impl<D> fmt::Debug for DatumConversionError<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("DatumConversionError")
.field("details", &self.details)
.finish_non_exhaustive()
}
}
impl<D> Error for DatumConversionError<D> {}
impl<D> From<DatumConversionError<D>> for MachineError {
fn from(datum_err: DatumConversionError<D>) -> Self {
MachineError {
kind: MachineErrorKind::Data,
message: Some(datum_err.to_string()),
..Default::default()
}
}
}