1use std::fmt;
2use std::io;
3use std::error;
4
5pub type Result<T> = ::std::result::Result<T, Error>;
6
7#[derive(Debug)]
8pub enum Error {
9 Parse(ParseError),
10 Io(io::Error),
11}
12
13#[derive(Debug)]
14pub struct ParseError {
15 pub description: String,
16}
17
18impl error::Error for Error {
19 fn cause(&self) -> Option<&dyn error::Error> {
20 match *self {
21 Error::Parse(ref err) => Some(err),
22 Error::Io(ref err) => Some(err),
23 }
24 }
25}
26
27impl fmt::Display for Error {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 match *self {
30 Error::Parse(ref err) => err.fmt(f),
31 Error::Io(ref err) => err.fmt(f),
32 }
33 }
34}
35
36impl From<io::Error> for Error {
37 fn from(err: io::Error) -> Error {
38 Error::Io(err)
39 }
40}
41
42impl From<ParseError> for Error {
43 fn from(err: ParseError) -> Error {
44 Error::Parse(err)
45 }
46}
47
48pub type ParseResult<T> = ::std::result::Result<T, ParseError>;
49
50impl ParseError {
51 pub fn new(description: String) -> ParseError {
52 ParseError { description }
53 }
54}
55
56impl error::Error for ParseError {
57 fn description(&self) -> &str {
58 &self.description
59 }
60
61 fn cause(&self) -> Option<&dyn error::Error> {
62 None
63 }
64}
65
66impl fmt::Display for ParseError {
67 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68 write!(f, "{}", self.description)
69 }
70}