-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherror.rs
More file actions
128 lines (108 loc) · 4.48 KB
/
Copy patherror.rs
File metadata and controls
128 lines (108 loc) · 4.48 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
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
use std::error;
use std::fmt;
use std::rc::Rc;
use crate::yamrb::value::RClass;
use crate::yamrb::value::RObject;
use crate::yamrb::vm::VM;
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
General,
Internal(String),
InvalidOpCode,
RuntimeError(String),
ArgumentError(String),
RangeError(String),
TypeMismatch,
NoMethodError(String),
NameError(String),
ZeroDivisionError,
TaggedError(&'static str, String),
Break(Rc<RObject>),
BlockReturn(usize, Rc<RObject>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error nr {:?}", self)
}
}
impl error::Error for Error {}
impl Error {
pub fn internal(msg: impl Into<String>) -> Error {
Error::Internal(msg.into())
}
pub fn message(&self) -> String {
match self {
Error::General => "General error".to_string(),
Error::Internal(msg) => format!("[Internal Error] {}", msg.clone()),
Error::InvalidOpCode => "Invalid opcode".to_string(),
Error::RuntimeError(msg) => msg.clone(),
Error::ArgumentError(msg) => format!("Invalid argument: {}", msg),
Error::RangeError(msg) => format!("Out of range: {}", msg),
Error::TypeMismatch => "Type mismatch".to_string(),
Error::NoMethodError(msg) => format!("Method not found: {}", msg),
Error::NameError(msg) => format!("Cannot found name: {}", msg),
Error::ZeroDivisionError => "divided by 0".to_string(),
Error::TaggedError(tag, msg) => format!("[{}] {}", tag, msg),
Error::Break(_) => "[Break]".to_string(),
Error::BlockReturn(_, _) => "[BlockReturn]".to_string(),
}
}
pub fn is_instance_of(&self, other: Rc<RClass>) -> bool {
matches!(
(self, other.sym_id.name.as_str()),
(Error::General, "StandardError")
| (Error::Internal(_), "InternalError")
| (Error::InvalidOpCode, "StandardError")
| (Error::RuntimeError(_), "RuntimeError")
| (Error::ArgumentError(_), "ArgumentError")
| (Error::RangeError(_), "RangeError")
| (Error::TypeMismatch, "StandardError")
| (Error::NoMethodError(_), "NoMethodError")
| (Error::NameError(_), "NameError")
| (Error::ZeroDivisionError, "ZeroDivisionError")
)
}
pub fn is_a(&self, vm: &mut VM, other: Rc<RClass>) -> bool {
let mut klass: Option<Rc<RClass>> = RClass::from_error(vm, self).into();
let target_klass_name = other.sym_id.name.as_str();
while let Some(k) = klass {
if k.sym_id.name.as_str() == target_klass_name {
return true;
}
klass = k.super_class.clone();
}
false
}
}
/// Used for asynchronous or multithreaded context
#[derive(Debug, Clone, PartialEq)]
pub enum StaticError {
General(String),
}
impl fmt::Display for StaticError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error nr {:?}", self)
}
}
impl error::Error for StaticError {}
unsafe impl Sync for StaticError {}
unsafe impl Send for StaticError {}
impl From<Error> for StaticError {
fn from(err: Error) -> StaticError {
match err {
Error::General => StaticError::General("General error".to_string()),
Error::Internal(msg) => StaticError::General(format!("[Internal Error] {}", msg)),
Error::InvalidOpCode => StaticError::General("Invalid opcode".to_string()),
Error::RuntimeError(msg) => StaticError::General(msg),
Error::ArgumentError(msg) => StaticError::General(format!("Invalid argument: {}", msg)),
Error::RangeError(msg) => StaticError::General(format!("Out of range: {}", msg)),
Error::TypeMismatch => StaticError::General("Type mismatch".to_string()),
Error::NoMethodError(msg) => StaticError::General(format!("Method not found: {}", msg)),
Error::NameError(msg) => StaticError::General(format!("Cannot found name: {}", msg)),
Error::ZeroDivisionError => StaticError::General("divided by 0".to_string()),
Error::TaggedError(tag, msg) => StaticError::General(format!("[{}] {}", tag, msg)),
Error::Break(_) => StaticError::General("[Break]".to_string()),
Error::BlockReturn(_, _) => StaticError::General("[BlockReturn]".to_string()),
}
}
}