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
use std::string;
use std::io;
use std::num;
use std::error;
use std::fmt;
#[derive(Debug)]
pub enum SexpError {
Parse(ParseError),
Other(String),
Io(io::Error),
FromUtf8(string::FromUtf8Error),
Float(num::ParseFloatError),
Int(num::ParseIntError),
}
impl fmt::Display for SexpError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
SexpError::Parse(ref pe) => write!(f, "symbolic expression parse error: {:?}", pe),
SexpError::Other(ref s) => write!(f, "symbolic expression other error: {}", s),
SexpError::Io(ref e) => e.fmt(f),
SexpError::FromUtf8(ref e) => e.fmt(f),
SexpError::Float(ref e) => e.fmt(f),
SexpError::Int(ref e) => e.fmt(f),
}
}
}
impl error::Error for SexpError {
fn description(&self) -> &str {
"symbolic expressions error"
}
fn cause(&self) -> Option<&error::Error> {
match *self {
SexpError::Parse(_) | SexpError::Other(_) => None,
SexpError::Io(ref e) => Some(e),
SexpError::FromUtf8(ref e) => Some(e),
SexpError::Float(ref e) => Some(e),
SexpError::Int(ref e) => Some(e),
}
}
}
#[derive(Debug)]
pub struct ParseError {
msg: String,
line: usize,
col: usize,
}
impl From<io::Error> for SexpError {
fn from(e: io::Error) -> SexpError {
SexpError::Io(e)
}
}
impl From<String> for SexpError {
fn from(e: String) -> SexpError {
SexpError::Other(e)
}
}
impl<'a> From<&'a str> for SexpError {
fn from(e: &'a str) -> SexpError {
SexpError::Other(e.into())
}
}
impl From<string::FromUtf8Error> for SexpError {
fn from(e: string::FromUtf8Error) -> SexpError {
SexpError::FromUtf8(e)
}
}
impl From<num::ParseFloatError> for SexpError {
fn from(e: num::ParseFloatError) -> SexpError {
SexpError::Float(e)
}
}
impl From<num::ParseIntError> for SexpError {
fn from(e: num::ParseIntError) -> SexpError {
SexpError::Int(e)
}
}
pub fn parse_error<T>(line: usize, col: usize, msg: String) -> Result<T, SexpError> {
let pe = ParseError {
msg: msg,
line: line,
col: col,
};
Err(SexpError::Parse(pe))
}