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
use std::{borrow::Cow, fmt};
pub type LanguageError = crate::ll::error::LanguageError;
pub type LanguageErrorKind = crate::ll::error::LanguageErrorKind;
#[derive(Debug)]
pub enum Error {
Compile(LanguageError),
Runtime(LanguageError),
TooManyGlobals,
TooManyFunctions,
TooManyMethods,
TooManyArguments,
TooManyTraits,
TooManyParametersInTraitMethod,
TypeMismatch {
expected: Cow<'static, str>,
got: Cow<'static, str>,
},
ArgumentCount {
expected: usize,
got: usize,
},
ArgumentTypeMismatch {
index: usize,
expected: Cow<'static, str>,
got: Cow<'static, str>,
},
ReentrantMutableBorrow,
User(Box<dyn std::error::Error>),
}
impl From<LanguageError> for Error {
fn from(error: LanguageError) -> Self {
match &error {
LanguageError::Compile { .. } => Self::Compile(error),
LanguageError::Runtime { .. } => Self::Runtime(error),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Compile(error) | Self::Runtime(error) => error.fmt(f),
Self::TooManyGlobals => f.write_str("too many globals"),
Self::TooManyFunctions => f.write_str("too many functions"),
Self::TooManyMethods => f.write_str("too many methods with different signatures"),
Self::TooManyArguments => f.write_str("too many arguments passed to a function"),
Self::TooManyTraits => f.write_str("too many traits"),
Self::TooManyParametersInTraitMethod => {
f.write_str("trait method with too many parameters")
}
Self::TypeMismatch { expected, got } => {
write!(f, "type mismatch, expected {expected} but got {got}")
}
Self::ArgumentCount { expected, got } => {
write!(f, "{expected} arguments expected but got {got}")
}
Self::ArgumentTypeMismatch { index, expected, got } => {
write!(
f,
"type mismatch at argument {}, expected {expected} but got {got}",
index + 1
)
}
Self::ReentrantMutableBorrow => write!(f, "method receiver is in use already"),
Self::User(error) => write!(f, "{error}"),
}
}
}
impl std::error::Error for Error {}
pub trait MicaResultExt<T, E> {
fn mica(self) -> Result<T, Error>;
}
#[repr(transparent)]
struct UserError<T>(T);
impl<T> fmt::Debug for UserError<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl<T> fmt::Display for UserError<T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<T> std::error::Error for UserError<T> where T: fmt::Debug + fmt::Display {}
impl<T, E> MicaResultExt<T, E> for Result<T, E>
where
E: fmt::Debug + fmt::Display + 'static,
{
fn mica(self) -> Result<T, Error> {
self.map_err(|error| Error::User(Box::new(UserError(error))))
}
}
pub(crate) fn wrap_in_language_error<T, E>(r: Result<T, E>) -> Result<T, LanguageErrorKind>
where
E: std::error::Error + 'static,
{
r.map_err(|error| LanguageErrorKind::User(Box::new(error)))
}
pub trait MicaLanguageResultExt<T> {
fn to_language_error(self) -> Result<T, LanguageErrorKind>;
}
impl<T> MicaLanguageResultExt<T> for Result<T, Error> {
fn to_language_error(self) -> Result<T, LanguageErrorKind> {
wrap_in_language_error(self)
}
}