1use std::error::Error;
14use std::fmt;
15use std::io;
16use std::num::{ParseFloatError, ParseIntError};
17
18#[derive(Debug)]
24pub struct ScanError {
25 pub at: ScanErrorAt,
29
30 pub kind: ScanErrorKind,
34
35 _priv: (),
39}
40
41impl ScanError {
42 pub fn new(at: usize, kind: ScanErrorKind) -> Self {
46 ScanError {
47 at: ScanErrorAt { bytes: at },
48 kind: kind,
49 _priv: (),
50 }
51 }
52
53 pub fn expected_end() -> Self {
57 Self::new(0, ScanErrorKind::ExpectedEnd)
58 }
59
60 pub fn float(err: ParseFloatError) -> Self {
64 Self::new(0, ScanErrorKind::Float(err))
65 }
66
67 pub fn int(err: ParseIntError) -> Self {
71 Self::new(0, ScanErrorKind::Int(err))
72 }
73
74 pub fn io(err: io::Error) -> Self {
78 Self::new(0, ScanErrorKind::Io(err))
79 }
80
81 pub fn literal_mismatch() -> Self {
85 Self::new(0, ScanErrorKind::LiteralMismatch)
86 }
87
88 pub fn syntax(desc: &'static str) -> Self {
92 Self::new(0, ScanErrorKind::Syntax(desc))
93 }
94
95 pub fn syntax_no_message() -> Self {
99 Self::new(0, ScanErrorKind::SyntaxNoMessage)
100 }
101
102 pub fn other<E: Into<Box<Error>>>(err: E) -> Self {
106 Self::new(0, ScanErrorKind::from_other(err))
107 }
108
109 pub fn furthest_along(self, other: Self) -> Self {
113 if self.at.offset() >= other.at.offset() {
114 self
115 } else {
116 other
117 }
118 }
119
120 pub fn add_offset(self, bytes: usize) -> Self {
126 ScanError::new(self.at.offset() + bytes, self.kind)
127 }
128}
129
130impl<'a> fmt::Display for ScanError {
131 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
132 try!("scan error: ".fmt(fmt));
133 try!(self.kind.fmt(fmt));
134 try!(", at offset: ".fmt(fmt));
135 try!(self.at.offset().fmt(fmt));
136 Ok(())
137 }
138}
139
140impl Error for ScanError {
141 fn cause(&self) -> Option<&Error> {
142 self.kind.cause()
143 }
144
145 fn description(&self) -> &str {
146 self.kind.description()
147 }
148}
149
150#[derive(Debug)]
159pub struct ScanErrorAt {
160 bytes: usize,
162}
163
164impl ScanErrorAt {
165 pub fn offset(&self) -> usize {
169 self.bytes
170 }
171}
172
173#[derive(Debug)]
177pub enum ScanErrorKind {
178 LiteralMismatch,
180
181 Syntax(&'static str),
183
184 SyntaxNoMessage,
190
191 ExpectedEnd,
193
194 Float(ParseFloatError),
196
197 Int(ParseIntError),
199
200 Io(io::Error),
202
203 Other(Box<Error>),
205
206 #[doc(hidden)]
208 __DoNotMatch,
209}
210
211impl ScanErrorKind {
212 pub fn from_other<E: Into<Box<Error>>>(err: E) -> Self {
216 ScanErrorKind::Other(err.into())
217 }
218}
219
220impl fmt::Display for ScanErrorKind {
221 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
222 use self::ScanErrorKind::*;
223 match *self {
224 LiteralMismatch => "did not match literal".fmt(fmt),
225 Syntax(desc) => {
226 try!("syntax error: ".fmt(fmt));
227 try!(desc.fmt(fmt));
228 Ok(())
229 },
230 SyntaxNoMessage => "unknown syntax error".fmt(fmt),
231 ExpectedEnd => "expected end of input".fmt(fmt),
232 Float(ref err) => err.fmt(fmt),
233 Int(ref err) => err.fmt(fmt),
234 Io(ref err) => err.fmt(fmt),
235 Other(ref err) => err.fmt(fmt),
236 __DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
237 }
238 }
239}
240
241impl Error for ScanErrorKind {
242 fn cause(&self) -> Option<&Error> {
243 use self::ScanErrorKind::*;
244 match *self {
245 LiteralMismatch
246 | Syntax(_)
247 | SyntaxNoMessage
248 | ExpectedEnd
249 => None,
250 Float(ref err) => err.cause(),
251 Int(ref err) => err.cause(),
252 Io(ref err) => err.cause(),
253 Other(ref err) => err.cause(),
254 __DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
255 }
256 }
257
258 fn description(&self) -> &str {
259 use self::ScanErrorKind::*;
260 match *self {
261 LiteralMismatch => "did not match literal",
262 Syntax(_) => "syntax error",
263 SyntaxNoMessage => "unknown syntax error",
264 ExpectedEnd => "expected end of input",
265 Float(ref err) => err.description(),
266 Int(ref err) => err.description(),
267 Io(ref err) => err.description(),
268 Other(ref err) => err.description(),
269 __DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
270 }
271 }
272}