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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#[cfg(doc)]
use super::*;
use std::borrow::Cow;
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 TypeMismatch {
message: Cow<'static, str>,
}
impl TypeMismatch {
pub const fn new() -> Self {
TypeMismatch {
message: Cow::Borrowed("dynamic type mismatch (no details available)"),
}
}
pub fn with_msg(message: String) -> Self {
TypeMismatch {
message: Cow::Owned(message),
}
}
pub const fn with_static_msg(message: &'static str) -> Self {
TypeMismatch {
message: Cow::Borrowed(message),
}
}
}
impl fmt::Display for TypeMismatch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.message)
}
}
impl Error for TypeMismatch {}
impl From<TypeMismatch> for MachineError {
fn from(datum_err: TypeMismatch) -> Self {
MachineError {
kind: MachineErrorKind::Data,
message: Some(datum_err.to_string()),
..Default::default()
}
}
}
pub struct DatumConversionFailure<D> {
pub original: D,
pub error: TypeMismatch,
}
impl<D> DatumConversionFailure<D> {
pub fn new(original: D, error: TypeMismatch) -> Self {
DatumConversionFailure { original, error }
}
pub fn with_msg(original: D, message: String) -> Self {
DatumConversionFailure::new(original, TypeMismatch::with_msg(message))
}
pub fn with_static_msg(original: D, message: &'static str) -> Self {
DatumConversionFailure::new(original, TypeMismatch::with_static_msg(message))
}
}
impl<D> fmt::Debug for DatumConversionFailure<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("DatumConversionFailure")
.field("error", &self.error)
.finish_non_exhaustive()
}
}
impl<D> From<DatumConversionFailure<D>> for TypeMismatch {
fn from(failure: DatumConversionFailure<D>) -> Self {
failure.error
}
}
impl<D> From<DatumConversionFailure<D>> for MachineError {
fn from(failure: DatumConversionFailure<D>) -> Self {
MachineError {
kind: MachineErrorKind::Data,
message: Some(TypeMismatch::from(failure).to_string()),
..Default::default()
}
}
}