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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use failure::{Backtrace, Context, Fail};
use std::convert::From;
use std::fmt::{self, Display};
use std::result;
use xml::Xml;

pub type Result<T> = result::Result<T, Error>;

#[derive(Debug, Fail, PartialEq)]
pub enum ErrorKind {
    // -- CLI errors
    #[fail(display = "failed on HTTP GET")]
    HttpGet,

    #[fail(display = "failed to parse JSON")]
    ParseJson,

    #[fail(display = "source is not specified")]
    MissingSource,

    #[fail(display = "input format is not specified and failed to guess")]
    MissingInputFormat,

    #[fail(display = "failed to read from stdin")]
    ReadStdin,

    #[fail(display = "failed to read source")]
    ReadSource,

    #[fail(display = "failed to parse arguments")]
    InvalidArgument,

    // -- File system errors
    #[fail(display = "failed to read directory")]
    ReadDir,

    #[fail(display = "failed to read directory entry")]
    ReadDirEntry,

    #[fail(display = "failed to recursively create a directory")]
    CreateDirAll,

    #[fail(display = "failed to read a file")]
    ReadFile,

    #[fail(display = "failed to write a file")]
    WriteFile,

    #[fail(display = "there is no cache directory")]
    NoCacheDir,

    // -- Mintty errors
    #[fail(display = "invalid color representation: {}", _0)]
    InvalidColorFormat(String),

    #[fail(display = "invalid line: {}", _0)]
    InvalidLineFormat(String),

    #[fail(display = "unknown color name: {}", _0)]
    UnknownColorName(String),

    #[fail(display = "failed to parse int")]
    ParseInt,

    // -- iTerm errors
    #[fail(display = "invalid XML")]
    XMLParse,

    #[fail(display = "root dict was not found")]
    NoRootDict,

    #[fail(display = "cannot extract text from: {}", _0)]
    NotCharacterNode(Box<Xml>),

    #[fail(display = "unknown color component: {}", _0)]
    UnknownColorComponent(String),

    #[fail(display = "failed to parse float")]
    ParseFloat,

    // -- Provider errors
    #[fail(display = "unknown color scheme provider: {}", _0)]
    UnknownProvider(String),

    #[fail(display = "missing color scheme name")]
    MissingName,
}

#[derive(Debug)]
pub struct Error {
    inner: Context<ErrorKind>,
}

impl Error {
    pub fn kind(&self) -> &ErrorKind {
        &*self.inner.get_context()
    }
}

impl Fail for Error {
    fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(&self.inner, f)
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Error {
        let inner = Context::new(kind);
        Error { inner }
    }
}

impl From<Context<ErrorKind>> for Error {
    fn from(inner: Context<ErrorKind>) -> Error {
        Error { inner }
    }
}