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
use std::{error, fmt};
#[derive(Debug)]
pub struct Error {
line: usize,
column: usize,
message: String,
}
impl Error {
#[inline]
pub fn new<T: Into<String>>((line, column): (usize, usize), message: T) -> Self {
Error {
line: line,
column: column,
message: message.into(),
}
}
}
impl error::Error for Error {
#[inline]
fn description(&self) -> &str {
&self.message
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
if self.line > 0 && self.column > 0 {
write!(
formatter,
"{} (line {}, column {})",
self.message, self.line, self.column,
)
} else if self.line > 0 {
write!(formatter, "{} (line {})", self.message, self.line)
} else {
self.message.fmt(formatter)
}
}
}