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
use std::{
fmt,
io::Error as IoError,
};
#[derive(Debug)]
pub enum Error {
MismatchedBrackets {
opening: usize,
closing: usize,
},
FileReadError(
IoError
),
IoError(
IoError
),
MaxInstructionsExceeded(
usize
),
}
impl From<IoError> for Error {
fn from(err: IoError) -> Self {
Self::IoError(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(
match self {
Self::MismatchedBrackets { opening, closing } =>
format!("Mismatched brackets; there were {opening} '[' found but only {closing} ']' found"),
Self::FileReadError(err) =>
format!("Failed to read the provided file:\n{err}"),
Self::IoError(err) =>
format!("An I/O error occured:\n{err}"),
Self::MaxInstructionsExceeded(cap) =>
format!("The amount of instructions executed has reached the set limit of `{cap}`"),
}
.as_str()
)
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;