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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use core::result;
use std::error::Error as StdError;
use std::fmt;
use std::io;

use num_bigint::BigInt;
use serde::de;
use serde::ser;

#[derive(Debug)]
pub enum Unexpected {
    Bool(bool),
    Integer(BigInt),
    Float(f64),
    Str(String),
    Unit,
    Array,
    Map,
}

impl fmt::Display for Unexpected {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        match *self {
            Unexpected::Bool(b) => write!(f, "boolean `{}`", b),
            Unexpected::Integer(ref i) => write!(f, "integer `{}`", i),
            Unexpected::Float(v) => write!(f, "floating point `{}`", v),
            Unexpected::Str(ref s) => write!(f, "string {:?}", s),
            Unexpected::Unit => write!(f, "unit value"),
            Unexpected::Array => write!(f, "array"),
            Unexpected::Map => write!(f, "map"),
        }
    }
}

pub struct Error {
    inner: Box<ErrorImpl>,
}

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

impl From<io::Error> for Error {
    fn from(source: io::Error) -> Self {
        Error {
            inner: Box::new(ErrorImpl::Io(source)),
        }
    }
}

impl From<std::convert::Infallible> for Error {
    fn from(_: std::convert::Infallible) -> Self {
        unreachable!()
    }
}

impl Error {
    #[doc(hidden)]
    #[cold]
    pub(crate) fn io(source: io::Error) -> Self {
        Error {
            inner: Box::new(ErrorImpl::Io(source)),
        }
    }

    #[doc(hidden)]
    #[cold]
    pub(crate) fn too_large<T: Into<BigInt>>(index: T) -> Self {
        Error {
            inner: Box::new(ErrorImpl::Range(index.into())),
        }
    }

    #[doc(hidden)]
    #[cold]
    pub(crate) fn invalid_type(unexpected: Unexpected, expected: &'static str) -> Self {
        Error {
            inner: Box::new(ErrorImpl::Type {
                unexpected,
                expected,
            }),
        }
    }

    #[doc(hidden)]
    #[cold]
    pub(crate) fn path_parse<R: pest::RuleType + Send + Sync + 'static>(
        source: pest::error::Error<R>,
        path: &str,
    ) -> Self {
        Error {
            inner: Box::new(ErrorImpl::PathParse {
                path: Box::from(path),
                source: Box::new(source),
            }),
        }
    }

    #[doc(hidden)]
    #[cold]
    pub(crate) fn format_parse<E>(origin: &str, source: E) -> Self
    where
        E: StdError + Sync + Send + 'static,
    {
        Error {
            inner: Box::new(ErrorImpl::FormatParse {
                origin: origin.into(),
                source: Box::new(source),
            }),
        }
    }

    #[doc(hidden)]
    #[cold]
    pub(crate) fn serde<T: AsRef<str>>(message: T) -> Self {
        Error {
            inner: Box::new(ErrorImpl::Serde(message.as_ref().into())),
        }
    }
}

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

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", *self)
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match *self.inner {
            ErrorImpl::PathParse { ref source, .. } => Some(source.as_ref()),

            ErrorImpl::FormatParse { ref source, .. } => Some(source.as_ref()),

            _ => None,
        }
    }
}

impl de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error {
            inner: Box::new(ErrorImpl::Serde(msg.to_string().into())),
        }
    }
}

impl ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error {
            inner: Box::new(ErrorImpl::Serde(msg.to_string().into())),
        }
    }
}

/// Represents all possible errors that can occur when working with
/// configuration.
enum ErrorImpl {
    /// Value path could not be parsed (Origin Path).
    PathParse {
        path: Box<str>,
        source: Box<dyn StdError + Send + Sync>,
    },

    /// Some IO error occurred while file operation.
    Io(io::Error),

    /// Value could not be converted into the requested type.
    Type {
        /// What we found when parsing the value
        unexpected: Unexpected,

        /// What was expected when parsing the value
        expected: &'static str,
    },

    /// Value could not be parsed by target format.
    FormatParse {
        origin: Box<str>,
        source: Box<dyn StdError + Send + Sync>,
    },

    /// Serde error
    Serde(Box<str>),

    /// Path index over range
    Range(BigInt),
}

impl fmt::Display for ErrorImpl {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ErrorImpl::PathParse {
                ref path,
                ref source,
            } => write!(f, "{}\n for {}", source, path),

            ErrorImpl::Range(ref i) => write!(f, "invalid range {}", i),

            ErrorImpl::Type {
                ref unexpected,
                expected,
            } => {
                write!(f, "invalid type: {}, expected {}", unexpected, expected)?;
                Ok(())
            }

            ErrorImpl::Io(ref err) => write!(f, "{}", err),

            ErrorImpl::Serde(ref s) => write!(f, "{}", s),

            ErrorImpl::FormatParse {
                ref source,
                ref origin,
            } => {
                write!(f, "{}\n in {}", source, origin)?;
                Ok(())
            }
        }
    }
}