-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresult.rs
More file actions
77 lines (69 loc) · 2.16 KB
/
Copy pathresult.rs
File metadata and controls
77 lines (69 loc) · 2.16 KB
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
use core::fmt;
use std::fmt::Formatter;
use crate::compiler::CompErrKind;
use crate::parser::ParseErrKind;
use crate::scanner::ScanErrKind;
use crate::vm::{RuntimeErrKind, VMState};
/// Result type used by top level program executor.
pub type ExeResult = Result<VMState, ExeErr>;
#[derive(Debug)]
pub struct ExeErr {
pub kind: ExeErrKind,
}
impl ExeErr {
pub fn new(kind: ExeErrKind) -> Self {
Self { kind }
}
/// Return exit code if this error wraps a runtime exit error.
pub fn exit_code(&self) -> Option<u8> {
if let ExeErrKind::RuntimeErr(RuntimeErrKind::Exit(code)) = self.kind {
Some(code)
} else {
None
}
}
}
#[derive(Debug)]
pub enum ExeErrKind {
Bootstrap(String),
ModuleDirNotFound(String),
ModuleNotFound(String),
CouldNotReadSourceFile(String),
ScanErr(ScanErrKind),
ParseErr(ParseErrKind),
CompErr(CompErrKind),
RuntimeErr(RuntimeErrKind),
ReplErr(String),
}
impl fmt::Display for ExeErr {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.kind)
}
}
impl fmt::Display for ExeErrKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use ExeErrKind::*;
let msg = match self {
Bootstrap(msg) => format!("Bootstrap process failed: {msg}"),
ModuleDirNotFound(path) => format!(
concat!(
"Module directory not found: {}\n",
"Please double check your module search path."
),
path
),
ModuleNotFound(name) => {
format!("Module not found: {name}")
}
CouldNotReadSourceFile(file_name) => {
format!("Could not read source file: {file_name}")
}
ScanErr(kind) => format!("Scan error: {kind:?}"),
ParseErr(kind) => format!("Parse error: {kind:?}"),
CompErr(kind) => format!("Compilation error: {kind:?}"),
RuntimeErr(kind) => format!("Runtime error: {kind:?}"),
ReplErr(msg) => format!("REPL error: {msg}"),
};
write!(f, "{msg}")
}
}