brainfuck/
error.rs

1use nom;
2
3use std::{error, fmt, io, result};
4
5#[derive(Debug)]
6pub enum Error {
7    IOError(io::Error),
8    ParseError(String),
9}
10
11impl fmt::Display for Error {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        match *self {
14            Error::IOError(ref err)    => write!(f, "IO Error: {}", err),
15            Error::ParseError(ref err) => write!(f, "Parse Error: {}", err),
16        }
17    }
18}
19
20impl error::Error for Error {
21    fn description(&self) -> &str {
22        match *self {
23            Error::IOError(ref err)    => err.description(),
24            Error::ParseError(ref err) => err,
25        }
26    }
27
28    fn cause(&self) -> Option<&dyn error::Error> {
29        match *self {
30            Error::IOError(ref err) => Some(err),
31            Error::ParseError(_)    => None,
32        }
33    }
34}
35
36impl From<io::Error> for Error {
37    fn from(err: io::Error) -> Error {
38        Error::IOError(err)
39    }
40}
41
42impl<T: fmt::Debug> From<nom::Err<T, u32>> for Error {
43    fn from(err: nom::Err<T, u32>) -> Error {
44        Error::ParseError(format!("{:?}", err))
45    }
46}
47
48pub type Result<T> = result::Result<T, Error>;