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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2015 Axel Rasmussen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Error is a structure which denotes all of the possible kinds of errors bdrck
/// can produce, including errors from any of its underlying dependencies.
#[derive(Fail, Debug)]
pub enum Error {
    /// An error encountered while trying to interact with environment
    /// variables.
    #[fail(display = "{}", _0)]
    EnvVar(#[cause] ::std::env::VarError),
    /// An error encountered in trying to decode a hex string to the bytes it
    /// represents.
    #[fail(display = "{}", _0)]
    HexDecode(#[cause] ::data_encoding::DecodeError),
    /// An error originating in HTTP client code.
    #[fail(display = "{}", _0)]
    Http(#[cause] ::reqwest::Error),
    /// An internal unrecoverable error, usually due to some underlying library.
    #[fail(display = "{}", _0)]
    Internal(::failure::Error),
    /// Errors akin to EINVAL - essentially, an argument passed into a function
    /// was invalid in some way..
    #[fail(display = "{}", _0)]
    InvalidArgument(::failure::Error),
    /// An I/O error, generally encountered when interacting with the
    /// filesystem.
    #[fail(display = "{}", _0)]
    Io(#[cause] ::std::io::Error),
    /// An error encountered while serializing or deserializing JSON.
    #[fail(display = "{}", _0)]
    Json(#[cause] ::serde_json::Error),
    /// An error encountered when decoding a serialized message.
    #[fail(display = "{}", _0)]
    MsgDecode(#[cause] ::msgpack::decode::Error),
    /// An error encountered when encoding a struct to a serialized message.
    #[fail(display = "{}", _0)]
    MsgEncode(#[cause] ::msgpack::encode::Error),
    /// Errors akin to ENOENT - something like e.g. "file not found", although
    /// this is not necessarily *always* about files.
    #[fail(display = "{}", _0)]
    NotFound(::failure::Error),
    /// An error where some data returned by an underlying library call
    /// contained a NUL byte ('\0'), in a context where such a thing is invalid.
    #[fail(display = "{}", _0)]
    Nul(#[cause] ::std::ffi::NulError),
    /// An error encountered when trying to parse an integer from a string.
    #[fail(display = "{}", _0)]
    ParseInt(#[cause] ::std::num::ParseIntError),
    /// An error encountered when trying to parse an IP address from a string.
    #[fail(display = "{}", _0)]
    ParseIpAddr(#[cause] ::std::net::AddrParseError),
    /// A precondition error, which basically amounts to a function being called
    /// when one or more of its preconditions were not satisfied.
    #[fail(display = "{}", _0)]
    Precondition(::failure::Error),
    /// An error encountered in either parsing or applying a regular expression.
    #[fail(display = "{}", _0)]
    Regex(#[cause] ::regex::Error),
    /// An error encountered when attempting to set the global Logger
    /// implementation.
    #[fail(display = "{}", _0)]
    SetLogger(#[cause] ::log::SetLoggerError),
    /// An error of an unknown type occurred. Generally this comes from some
    /// dependency or underlying library, in a case where it's difficult to tell
    /// exactly what kind of problem occurred.
    #[fail(display = "{}", _0)]
    Unknown(::failure::Error),
}

impl From<::std::env::VarError> for Error {
    fn from(e: ::std::env::VarError) -> Self {
        Error::EnvVar(e)
    }
}

impl From<::reqwest::Error> for Error {
    fn from(e: ::reqwest::Error) -> Self {
        Error::Http(e)
    }
}

impl From<::std::io::Error> for Error {
    fn from(e: ::std::io::Error) -> Self {
        Error::Io(e)
    }
}

impl From<::serde_json::Error> for Error {
    fn from(e: ::serde_json::Error) -> Self {
        Error::Json(e)
    }
}

impl From<::msgpack::decode::Error> for Error {
    fn from(e: ::msgpack::decode::Error) -> Self {
        Error::MsgDecode(e)
    }
}

impl From<::msgpack::encode::Error> for Error {
    fn from(e: ::msgpack::encode::Error) -> Self {
        Error::MsgEncode(e)
    }
}

impl From<::std::ffi::NulError> for Error {
    fn from(e: ::std::ffi::NulError) -> Self {
        Error::Nul(e)
    }
}

impl From<::std::num::ParseIntError> for Error {
    fn from(e: ::std::num::ParseIntError) -> Self {
        Error::ParseInt(e)
    }
}

impl From<::std::net::AddrParseError> for Error {
    fn from(e: ::std::net::AddrParseError) -> Self {
        Error::ParseIpAddr(e)
    }
}

impl From<::regex::Error> for Error {
    fn from(e: ::regex::Error) -> Self {
        Error::Regex(e)
    }
}

impl From<::log::SetLoggerError> for Error {
    fn from(e: ::log::SetLoggerError) -> Self {
        Error::SetLogger(e)
    }
}

/// A Result type which uses bdrck's internal Error type.
pub type Result<T> = ::std::result::Result<T, Error>;