    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
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244

    use std::error::Error;
    use std::fmt;
    use std::result;

    use serde::de;
    use serde::ser;

    #[derive(Debug)]
    pub enum Unexpected {
        Bool(bool),
        I64(i64),
        I128(i128),
        U64(u64),
        U128(u128),
        Float(f64),
        Str(String),
        Unit,
        Seq,
        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::I64(i) => write!(f, "integer 64 bit `{}`", i),
                Unexpected::I128(i) => write!(f, "integer 128 bit `{}`", i),
                Unexpected::U64(i) => write!(f, "unsigned integer 64 bit `{}`", i),
                Unexpected::U128(i) => write!(f, "unsigned integer 128 bit `{}`", i),
                Unexpected::Float(v) => write!(f, "floating point `{}`", v),
                Unexpected::Str(ref s) => write!(f, "string {:?}", s),
                Unexpected::Unit => write!(f, "unit value"),
                Unexpected::Seq => write!(f, "sequence"),
                Unexpected::Map => write!(f, "map"),
            }
        }
    }

    /// Represents all possible errors that can occur when working with
    /// configuration.
    pub enum ConfigError {
        /// Configuration is frozen and no further mutations can be made.
        Frozen,

        /// Configuration property was not found
        NotFound(String),

        /// Configuration path could not be parsed.
        PathParse(nom::error::ErrorKind),

        /// Configuration could not be parsed from file.
        FileParse {
            /// The URI used to access the file (if not loaded from a string).
            /// Example: `/path/to/config.json`
            uri: Option<String>,

            /// The captured error from attempting to parse the file in its desired format.
            /// This is the actual error object from the library used for the parsing.
            cause: Box<dyn Error + Send + Sync>,
        },

        /// Value could not be converted into the requested type.
        Type {
            /// The URI that references the source that the value came from.
            /// Example: `/path/to/config.json` or `Environment` or `etcd://localhost`
            // TODO: Why is this called Origin but FileParse has a uri field?
            origin: Option<String>,

            /// What we found when parsing the value
            unexpected: Unexpected,

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

            /// The key in the configuration hash of this value (if available where the
            /// error is generated).
            key: Option<String>,
        },

        /// Custom message
        Message(String),

        /// Unadorned error from a foreign origin.
        Foreign(Box<dyn Error + Send + Sync>),
    }

    impl ConfigError {
        // FIXME: pub(crate)
        #[doc(hidden)]
        pub fn invalid_type(
            origin: Option<String>,
            unexpected: Unexpected,
            expected: &'static str,
        ) -> Self {
            Self::Type {
                origin,
                unexpected,
                expected,
                key: None,
            }
        }

        // Have a proper error fire if the root of a file is ever not a Table
        // TODO: for now only json5 checked, need to finish others
        #[doc(hidden)]
        pub fn invalid_root(origin: Option<&String>, unexpected: Unexpected) -> Box<Self> {
            Box::new(Self::Type {
                origin: origin.cloned(),
                unexpected,
                expected: "a map",
                key: None,
            })
        }

        // FIXME: pub(crate)
        #[doc(hidden)]
        #[must_use]
        pub fn extend_with_key(self, key: &str) -> Self {
            match self {
                Self::Type {
                    origin,
                    unexpected,
                    expected,
                    ..
                } => Self::Type {
                    origin,
                    unexpected,
                    expected,
                    key: Some(key.into()),
                },

                _ => self,
            }
        }

        #[must_use]
        fn prepend(self, segment: &str, add_dot: bool) -> Self {
            let concat = |key: Option<String>| {
                let key = key.unwrap_or_default();
                let dot = if add_dot && key.as_bytes().get(0).unwrap_or(&b'[') != &b'[' {
                    "."
                } else {
                    ""
                };
                format!("{}{}{}", segment, dot, key)
            };
            match self {
                Self::Type {
                    origin,
                    unexpected,
                    expected,
                    key,
                } => Self::Type {
                    origin,
                    unexpected,
                    expected,
                    key: Some(concat(key)),
                },
                Self::NotFound(key) => Self::NotFound(concat(Some(key))),
                _ => self,
            }
        }

        #[must_use]
        pub(crate) fn prepend_key(self, key: &str) -> Self {
            self.prepend(key, true)
        }

        #[must_use]
        pub(crate) fn prepend_index(self, idx: usize) -> Self {
            self.prepend(&format!("[{}]", idx), false)
        }
    }

    /// Alias for a `Result` with the error type set to `ConfigError`.
    pub type Result<T> = result::Result<T, ConfigError>;

    // Forward Debug to Display for readable panic! messages
    impl fmt::Debug for ConfigError {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}", *self)
        }
    }

    impl fmt::Display for ConfigError {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match *self {
                ConfigError::Frozen => write!(f, "configuration is frozen"),

                ConfigError::PathParse(ref kind) => write!(f, "{}", kind.description()),

                ConfigError::Message(ref s) => write!(f, "{}", s),

                ConfigError::Foreign(ref cause) => write!(f, "{}", cause),

                ConfigError::NotFound(ref key) => {
                    write!(f, "configuration property {:?} not found", key)
                }

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

                    if let Some(ref key) = *key {
                        write!(f, " for key `{}`", key)?;
                    }

                    if let Some(ref origin) = *origin {
                        write!(f, " in {}", origin)?;
                    }

                    Ok(())
                }

                ConfigError::FileParse { ref cause, ref uri } => {
                    write!(f, "{}", cause)?;

                    if let Some(ref uri) = *uri {
                        write!(f, " in {}", uri)?;
                    }

                    Ok(())
                }
            }
        }
    }

    impl Error for ConfigError {}

    impl de::Error for ConfigError {
        fn custom<T: fmt::Display>(msg: T) -> Self {
            Self::Message(msg.to_string())
        }
    }

    impl ser::Error for ConfigError {
        fn custom<T: fmt::Display>(msg: T) -> Self {
            Self::Message(msg.to_string())
        }
    }