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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! # manaconf
//!
//! Library for building a layered configuration provider.
//!
//! TODO: Better docs here

use std::error::Error;

mod helpers;
mod key;
mod value;
pub mod sources;

pub use key::{Key, KeyBuf};
pub use value::{TryFromValue, Value};

/// Trait implemented by config value sources
pub trait Source {
    /// The error type used by this `Source`
    type Error;

    /// Gets a value from the source with the given `key`
    fn get_value(&self, key: &Key) -> Result<Option<Value>, Self::Error>;
}

/// Wraps a source to map it's error to `Box<dyn Error>`
struct BoxedErrorSource<S> {
    contained: S,
}

impl<S, E> From<S> for BoxedErrorSource<S>
where
    E: Error + 'static,
    S: Source<Error = E>,
{
    fn from(source: S) -> Self {
        BoxedErrorSource { contained: source }
    }
}

impl<S, E> Source for BoxedErrorSource<S>
where
    E: Error + 'static,
    S: Source<Error = E>,
{
    type Error = Box<dyn Error>;

    fn get_value(&self, key: &Key) -> Result<Option<Value>, Self::Error> {
        self.contained
            .get_value(key)
            .map_err(|e| Box::new(e) as Box<dyn Error>)
    }
}

/// Error returned from an implementation of `ValueRead`
#[derive(Debug)]
pub enum ValueReadError {
    /// Error occurred when reading from a `Source`
    SourceReadError(Box<dyn Error>),
    /// Error occurred when attempting to convert a `Value`
    /// to a requested type
    ValueConversionError(Box<dyn Error>),
    /// Value was not present, but was expected to be so
    ValueNotPresent,
}

impl Error for ValueReadError {}
impl std::fmt::Display for ValueReadError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValueReadError::SourceReadError(e) => {
                write!(
                    fmt,
                    "Error occurred while reading from configuration source: "
                )?;
                e.fmt(fmt)
            }
            ValueReadError::ValueConversionError(e) => {
                write!(fmt, "Value failed to be converted to requested type: ")?;
                e.fmt(fmt)
            }
            ValueReadError::ValueNotPresent => write!(fmt, "Requested value did not exist"),
        }
    }
}

pub trait ValueRead: Sized {
    /// Gets a value from configuration using the given `key`
    ///
    /// Attempts to convert the value to `T` if the conversion is supported
    fn get_value<T, K>(&self, key: K) -> Result<Option<T>, ValueReadError>
    where
        K: AsRef<Key>,
        T: TryFromValue;

    /// Gets a value from configuration using the given `key` where the value
    /// is expected to exist, and thus it's an error if it doesn't.
    ///
    /// Attempts to convert the value to `T` if the conversion is supported
    fn get_expected_value<T, K>(&self, key: K) -> Result<T, ValueReadError>
    where
        K: AsRef<Key>,
        T: TryFromValue,
    {
        self.get_value(key)?.ok_or(ValueReadError::ValueNotPresent)
    }

    /// Bind values to type `T`
    fn bind<T: TryFromValueRead>(&self) -> Result<T, ValueReadError> {
        <T as TryFromValueRead>::try_from(self)
    }
}
/// Trait to implement on types that can be constructed from reading values
/// from a `ValueRead`
pub trait TryFromValueRead: Sized {
    fn try_from<R: ValueRead>(value_read: &R) -> Result<Self, ValueReadError>;
}

/// A section of configuration
///
/// This acts as a pre-applied prefix to a config key when fetching values
///
/// # Example
/// ```
/// let section = config.section("My::Config::Section");
/// // This is now the same as requesting `My::Config::Section::Value`
/// let value: String = section.get_value("Value");
/// ```
pub struct Section<'a> {
    key: KeyBuf,
    config: &'a Config,
}

impl<'a> Section<'a> {
    /// Creates a further subsection from this section, similar to a section
    /// created by calling `Config::section`, except in this case the current
    /// sections prefix is prepended to the `key` passed into `section`
    ///
    /// # Example
    /// ```
    /// let section = config.section("first");
    /// let subsection = section.section("second");
    /// // Will read value from "first::second::value"
    /// let value: String = subsection.get_value("value");
    /// ```
    pub fn section<K: AsRef<Key>>(&self, key: K) -> Section {
        self.section(key.as_ref())
    }

    fn _section(&self, key: &Key) -> Section {
        Section { 
            key: self.key.extend_with_suffix(key),
            config: self.config
        }
    }
}

impl<'a> ValueRead for Section<'a> {
    fn get_value<T, K>(&self, key: K) -> Result<Option<T>, ValueReadError>
    where
        K: AsRef<Key>,
        T: TryFromValue,
    {
        let mut new_key = self.key.to_key_buf();
        new_key.push(key.as_ref());
        self.config.get_value(&new_key)
    }
}

pub struct Config {
    sources: Vec<Box<dyn Source<Error = Box<dyn Error>>>>,
}

impl Config {
    /// Get a section of a config at a given key path
    ///
    /// This allows config values to be accessed from Section,
    /// without having to specify the prefix
    pub fn section<K: AsRef<Key>>(&self, key: K) -> Section {
        Section {
            key: key.as_ref().to_owned(),
            config: self,
        }
    }

    fn _get_value(&self, key: &Key) -> Result<Option<Value>, ValueReadError> {
        if self.sources.len() == 0 {
            return Ok(None);
        }

        // Go through our sources, skipping any that return `Ok(None)`
        // and taking the first one to return `Ok(Some(_))` or `Err()`
        self.sources
            .iter()
            .map(|s| s.get_value(key))
            .skip_while(|v| match v {
                Ok(None) => true,
                _ => false,
            })
            .next()
            .unwrap_or(Ok(None))
            .map_err(|e| ValueReadError::SourceReadError(e))
    }
}

impl ValueRead for Config {
    fn get_value<T, K>(&self, key: K) -> Result<Option<T>, ValueReadError>
    where
        K: AsRef<Key>,
        T: TryFromValue,
    {
        self._get_value(key.as_ref())?
            .map(|v| T::try_from_value(v))
            .transpose()
            .map_err(|e| ValueReadError::ValueConversionError(Box::new(e)))
    }
}

type BoxedDynamicSource = Box<dyn Source<Error = Box<dyn Error>>>;

pub struct Builder {
    sources: Vec<BoxedDynamicSource>,
}

impl Builder {
    /// Creates a new `Builder` for constructing a `Config`
    pub fn new() -> Self {
        Self {
            sources: Vec::new(),
        }
    }

    /// Adds a new `Source` such that the sources provided keys are available
    /// from the root.
    ///
    /// Note: The order that sources are added, are the order that they are
    /// checked for values, so you want to add your source with the highest
    /// priority first.
    pub fn add_source<S, E>(mut self, source: S) -> Self
    where
        E: Error + 'static,
        S: Source<Error = E> + 'static,
    {
        self.sources.push(Box::new(BoxedErrorSource::from(source)));
        self
    }

    /// Adds a new `Source` such that the sources provided keys are available
    /// from the supplied `prefix`.
    ///
    /// Note: The order that sources are added, are the order that they are
    /// checked for values, so you want to add your source with the highest
    /// priority first.
    pub fn add_source_at_prefix<S, E, K>(self, source: S, prefix: K) -> Self
    where
        E: Error + 'static,
        S: Source<Error = E> + 'static,
        K: AsRef<Key>
    {
        self.add_source(WithPrefixSource { 
            prefix: prefix.as_ref().to_owned(), 
            source 
        })
    }

    /// Build the `Config` from the setup supplied by this builder
    pub fn build(self) -> Config {
        Config {
            sources: self.sources,
        }
    }
}

/// Wraps a source so that it only responds to keys with a given prefix.
///
/// Useful for mounting sources that otherwise would not have the desired
/// prefix
pub struct WithPrefixSource<S: Source> {
    prefix: KeyBuf,
    source: S,
}

impl<S: Source> Source for WithPrefixSource<S> {
    type Error = S::Error;

    fn get_value(&self, key: &Key) -> Result<Option<Value>, Self::Error> {
        if !key.start_with(key) {
            return Ok(None);
        }

        self.source.get_value(&key.strip_prefix(&self.prefix))
    }
}