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
150
151
152
153
154
155
156
157
158
159
160
161
//! Defines the `Error` and `Result` types used by this crate.

use crate::{parsers::ParseError, Encoding};
use std::error::Error as StdError;
use std::fmt::Display;
use std::io;
use thiserror::Error;

/// A type alias for `Result<T, Error>`.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// The error returned by all fallible operations within this crate.
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum Error {
    /// Represents a generic error message.
    #[error("{0}")]
    Message(String),

    /// Represents errors of operations that are not supported by a certain encoding.
    #[error("operation is not supported for encoding `{0}`")]
    UnsupportedEncoding(Encoding),

    /// Error emitted by parsers from this crate.
    #[error(transparent)]
    ParseError(#[from] ParseError),

    /// Represents generic IO errors.
    #[error(transparent)]
    Io(#[from] io::Error),

    /// Represents an invalid glob pattern.
    #[error("invalid glob pattern `{pattern}`")]
    GlobPatternError {
        /// The pattern that caused the error.
        pattern: String,
        /// The underlying error.
        source: glob::PatternError,
    },

    /// Represents an error fetching a remote data source.
    #[error("unable to fetch remote data source")]
    RequestError(#[from] ureq::Error),

    /// Represents errors emitted by serializers and deserializers.
    #[error(transparent)]
    Serde(Box<dyn StdError + Send + Sync>),
}

impl Error {
    pub(crate) fn new<T>(msg: T) -> Error
    where
        T: Display,
    {
        Error::Message(msg.to_string())
    }

    pub(crate) fn serde<E>(err: E) -> Error
    where
        E: Into<Box<dyn StdError + Send + Sync>>,
    {
        Error::Serde(err.into())
    }

    pub(crate) fn io<E>(err: E) -> Error
    where
        E: Into<io::Error>,
    {
        Error::Io(err.into())
    }

    pub(crate) fn glob_pattern<T>(pattern: T, source: glob::PatternError) -> Error
    where
        T: Display,
    {
        Error::GlobPatternError {
            pattern: pattern.to_string(),
            source,
        }
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        if err.is_io() {
            Error::io(err)
        } else {
            Error::serde(err)
        }
    }
}

impl From<serde_yaml::Error> for Error {
    fn from(err: serde_yaml::Error) -> Self {
        if let Some(source) = err.source() {
            if let Some(io_err) = source.downcast_ref::<io::Error>() {
                return Error::io(io_err.kind());
            }
        }

        Error::serde(err)
    }
}

impl From<json5::Error> for Error {
    fn from(err: json5::Error) -> Self {
        Error::serde(err)
    }
}

impl From<toml::ser::Error> for Error {
    fn from(err: toml::ser::Error) -> Self {
        Error::serde(err)
    }
}

impl From<toml::de::Error> for Error {
    fn from(err: toml::de::Error) -> Self {
        Error::serde(err)
    }
}

impl From<csv::Error> for Error {
    fn from(err: csv::Error) -> Self {
        if err.is_io_error() {
            match err.into_kind() {
                csv::ErrorKind::Io(io_err) => Error::io(io_err),
                _ => unreachable!(),
            }
        } else {
            Error::serde(err)
        }
    }
}

impl From<serde_qs::Error> for Error {
    fn from(err: serde_qs::Error) -> Self {
        match err {
            serde_qs::Error::Io(io_err) => Error::io(io_err),
            other => Error::serde(other),
        }
    }
}

impl From<serde_xml_rs::Error> for Error {
    fn from(err: serde_xml_rs::Error) -> Self {
        match err {
            serde_xml_rs::Error::Io { source } => Error::io(source),
            other => Error::serde(other),
        }
    }
}

impl From<hcl::Error> for Error {
    fn from(err: hcl::Error) -> Self {
        match err {
            hcl::Error::Io(io_err) => Error::io(io_err),
            other => Error::serde(other),
        }
    }
}