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

use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;

pub enum ParseError {
    Eof(&'static str),
    NotFound(&'static str),
    Expected(Vec<u8>),
    ExpectedType(&'static str),
    Io(IoError),
    InvalidBodyChar(u8),
    LineTooLong(usize),
    TrailingInput(&'static str, usize),
    InternalError,
    Parse(&'static str, Box<ParseError>),
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>
    {
        match *self {
            ParseError::Eof(ref field) => write!(f, "{} while looking for \"{}\"",
                                                 self.description(), field),
            ParseError::NotFound(ref token) => write!(f, "\"{}\" {}",
                                                      token, self.description()),
            ParseError::Expected(ref bytes) => write!(f, "{}. Expected \"{:?}\"",
                                                      self.description(), bytes),
            ParseError::ExpectedType(ref t) => write!(f, "{}. Expected {}",
                                                      self.description(), t),
            ParseError::Io(ref e) => write!(f, "{}: {}",
                                            self.description(), e),
            ParseError::InvalidBodyChar(ref c) => write!(f, "{}: {} is not 7-bit ASCII",
                                                         self.description(), c),
            ParseError::LineTooLong(ref l) => write!(f, "Line {} is too long", l),
            ParseError::TrailingInput(ref field, ref c) => write!(
                f, "Trailing input at byte {} in {}", c, field),
            ParseError::Parse(ref field, ref inner) => write!(
                f, "{} {}: {}", self.description(), field, inner),
            _ => write!(f, "{}", self.description()),
        }
    }
}

impl fmt::Debug for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>
    {
        <Self as fmt::Display>::fmt(self, f)
    }
}

impl StdError for ParseError {
    fn description(&self) -> &str
    {
        match *self {
            ParseError::Eof(_) => "End of File",
            ParseError::NotFound(_) => "Not Found",
            ParseError::Expected(_) => "Expectation Failed",
            ParseError::ExpectedType(_) => "Expectation Failed",
            ParseError::Io(_) => "I/O Error",
            ParseError::InvalidBodyChar(_) => "Invalid Body Character",
            ParseError::LineTooLong(_) => "Line too long",
            ParseError::TrailingInput(_,_) => "Trailing input",
            ParseError::InternalError => "Internal error",
            ParseError::Parse(_,_) => "Unable to parse",
        }
    }

    fn cause(&self) -> Option<&dyn StdError>
    {
        match *self {
            ParseError::Io(ref e) => Some(e),
            ParseError::Parse(_, ref e) => Some(e),
            _ => None,
        }
    }
}