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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#![cfg_attr(feature = "serde-serialization", feature(custom_derive, plugin))]
#![cfg_attr(feature = "serde-serialization", plugin(serde_macros))]

//! # Configuring your dependency
//! `toml-config` can be configured to use `rustc_serialize` or `serde`
//!
//! ### Using `toml-config` with `rustc_serialize`
//! By default `toml-config` uses `rustc_serialize`, so just add the dependency in Cargo.toml normally:
//!
//! ```toml
//! [dependencies]
//! toml-config = "0.2"
//! ```
//! ### Using `toml-config` with `serde`
//! To use toml-config with `serde`, opt out from the default features and enable the `serde-serialization`
//! feature:
//!
//! ```toml
//! [dependencies.toml-config]
//! version = "0.2"
//! default-features = false
//! features = ["serde-serialization"]
//! ```

#[cfg(feature = "rustc-serialize")] extern crate rustc_serialize;
#[cfg(feature = "serde-serialization")] extern crate serde;

#[macro_use] extern crate log;

extern crate toml;

#[cfg(feature = "rustc-serialize")] use rustc_serialize::{Encodable, Decodable};
#[cfg(feature = "serde-serialization")] use serde::{Serialize, Deserialize};


use std::io::Read;
use std::fs::File;
use std::path::Path;

/// Implements helper functions for loading TOML files into a structure
///
/// # Examples
/// To load a file into a Config struct, use `ConfigFactory`.
///
/// Either `rustc_serialize` or `serde` can be used for serialization.
///
/// ## Example using rustc_serialize
/// ```no_run
/// # #[cfg(feature = "rustc-serialize")]
/// extern crate rustc_serialize;
/// extern crate toml_config;
///
/// # #[cfg(feature = "rustc-serialize")]
/// # fn main() {
/// use rustc_serialize::{Encodable, Decodable};
/// use std::path::Path;
/// use toml_config::ConfigFactory;
///
/// #[derive(RustcEncodable, RustcDecodable)]
/// struct Config  {
///     nested: NestedConfig
/// }
///
/// // Defaults will be used for missing/invalid configurations in the TOML config file
/// impl Default for Config {
///     fn default() -> Config {
///         Config {
///             nested: NestedConfig::default()
///         }
///     }
/// }
///
/// #[derive(RustcEncodable, RustcDecodable)]
/// struct NestedConfig  {
///     value: String,
///     values: Vec<u16>
/// }
///
/// impl Default for NestedConfig {
///     fn default() -> NestedConfig {
///         NestedConfig {
///             value: "default".to_owned(),
///             values: vec![0, 0, 0]
///         }
///     }
/// }
///
/// /* config.toml:
///  * [nested]
///  * value = "test"
///  * values = [1, 2, 3]
///  */
///
/// let config: Config = ConfigFactory::load(Path::new("config.toml"));
/// assert_eq!(config.nested.value, "test");
/// assert_eq!(config.nested.values, vec![1, 2, 3]);
/// # }
/// # #[cfg(feature = "serde-serialization")] fn main() { }
/// ```
/// ## Example using serde
/// ```no_run
/// # #![cfg_attr(feature = "serde-serialization", feature(custom_derive, plugin))]
/// # #![cfg_attr(feature = "serde-serialization", plugin(serde_macros))]
/// # #[cfg(feature = "serde-serialization")]
/// extern crate serde;
/// extern crate toml_config;
///
/// # #[cfg(feature = "serde-serialization")]
/// # fn main() {
/// use serde::{Serialize, Deserialize};
/// use std::path::Path;
/// use toml_config::ConfigFactory;
///
/// #[derive(Serialize, Deserialize)]
/// struct Config  {
///     nested: NestedConfig
/// }
///
/// // Defaults will be used for missing/invalid configurations in the TOML config file
/// impl Default for Config {
///     fn default() -> Config {
///         Config {
///             nested: NestedConfig::default()
///         }
///     }
/// }
///
/// #[derive(Serialize, Deserialize)]
/// struct NestedConfig  {
///     value: String,
///     values: Vec<u16>
/// }
///
/// impl Default for NestedConfig {
///     fn default() -> NestedConfig {
///         NestedConfig {
///             value: "default".to_owned(),
///             values: vec![0, 0, 0]
///         }
///     }
/// }
///
/// /* config.toml:
///  * [nested]
///  * value = "test"
///  * values = [1, 2, 3]
///  */
///
/// let config: Config = ConfigFactory::load(Path::new("config.toml"));
/// assert_eq!(config.nested.value, "test");
/// assert_eq!(config.nested.values, vec![1, 2, 3]);
/// # }
/// # #[cfg(feature = "rustc-serialize")] fn main() { }
/// ```
pub struct ConfigFactory;

impl ConfigFactory {
    /// Loads a TOML file and decodes it into a target structure, using default values
    /// for missing or invalid file configurations
    #[cfg(feature = "rustc-serialize")]
    pub fn load<T>(path: &Path) -> T where T: Encodable + Decodable + Default {
        match ConfigFactory::parse_toml_file(path) {
            Some(toml_table) => ConfigFactory::decode(toml_table),
            None => T::default()
        }
    }

    /// Loads a TOML file and decodes it into a target structure, using default values
    /// for missing or invalid file configurations
    #[cfg(feature = "serde-serialization")]
    pub fn load<T>(path: &Path) -> T where T: Serialize + Deserialize + Default {
        match ConfigFactory::parse_toml_file(path) {
            Some(toml_table) => ConfigFactory::decode(toml_table),
            None => T::default()
        }
    }

    /// Decodes a TOML table into a target structure, using default values
    /// for missing or invalid configurations
    #[cfg(feature = "rustc-serialize")]
    pub fn decode<T>(toml_table: toml::Table) -> T where T: Encodable + Decodable + Default {
        let default_table = toml::encode(&T::default()).as_table().unwrap().clone();
        let table_with_overrides = ConfigFactory::apply_overrides(default_table, toml_table);
        toml::decode(toml::Value::Table(table_with_overrides)).unwrap()
    }

    /// Decodes a TOML table into a target structure, using default values
    /// for missing or invalid configurations
    #[cfg(feature = "serde-serialization")]
    pub fn decode<T>(toml_table: toml::Table) -> T where T: Serialize + Deserialize + Default {
        let default_table = toml::encode(&T::default()).as_table().unwrap().clone();
        let table_with_overrides = ConfigFactory::apply_overrides(default_table, toml_table);
        toml::decode(toml::Value::Table(table_with_overrides)).unwrap()
    }

    fn parse_toml_file(path: &Path) -> Option<toml::Table> {
        let mut toml_config = String::new();

        let mut file = match File::open(path) {
            Ok(file) => file,
            Err(_)  => {
                warn!("Config file not found: {}, using defaults..", path.display());
                return None;
            }
        };

        file.read_to_string(&mut toml_config)
            .unwrap_or_else(|err| panic!("Unable to read config file: {}", err));

        let mut parser = toml::Parser::new(&toml_config);
        let toml_table = parser.parse();

        if toml_table.is_none() {
            for err in &parser.errors {
                let (line, col) = parser.to_linecol(err.lo);
                error!("Parsing of {} failed [{}:{}] - {}", path.display(), line + 1, col + 1, err.desc);
            }
            error!("Unable to parse config file: {}, using defaults..", path.display());
            return None;
        }

        toml_table
    }

    fn apply_overrides(defaults: toml::Table, overrides: toml::Table) -> toml::Table {
        let mut merged = defaults.clone();
        for (k, v) in overrides.into_iter() {
            match defaults.get(&k) {
                Some(default_value) =>
                    if v.type_str() == default_value.type_str() {
                        match v {
                            toml::Value::Table(overrides_table) => {
                                let defaults_table = default_value.as_table().unwrap().clone();
                                let merged_table = ConfigFactory::apply_overrides(defaults_table, overrides_table);
                                merged.insert(k, toml::Value::Table(merged_table))
                            },
                            any => merged.insert(k, any)
                        };
                    } else {
                        warn!("Wrong type for config: {}. Expected: {}, found: {}, using default value..",
                            k, default_value.type_str(), v.type_str());
                    },
                None => {
                    merged.insert(k, v);
                }
            }
        }
        merged
    }
}

#[cfg(test)]
mod tests {
    use super::ConfigFactory;

    extern crate tempfile;
    use std::io::Write;

    macro_rules! tempfile {
        ($content:expr) => {
            {
                let mut f = tempfile::NamedTempFile::new().unwrap();
                f.write_all($content.as_bytes()).unwrap();
                f.flush().unwrap();
                f
            }
        }
    }

    #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
    #[cfg_attr(feature = "serde-serialization", derive(Serialize, Deserialize))]
    pub struct Config  {
        pub nested: NestedConfig,
        pub optional: Option<String>
    }

    impl Default for Config {
        fn default() -> Config {
            Config {
                nested: NestedConfig::default(),
                optional: None
            }
        }
    }

    #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
    #[cfg_attr(feature = "serde-serialization", derive(Serialize, Deserialize))]
    pub struct NestedConfig  {
        pub value: String,
        pub values: Vec<u16>
    }

    impl Default for NestedConfig {
        fn default() -> NestedConfig {
            NestedConfig {
                value: "default".to_owned(),
                values: vec![0, 0, 0]
            }
        }
    }

    #[test]
    fn it_should_parse_and_decode_a_valid_toml_file() {
        let toml_file = tempfile!(r#"
            optional = "override"
            [nested]
            value = "test"
            values = [1, 2, 3]
        "#);
        let config: Config = ConfigFactory::load(toml_file.path());
        assert_eq!(config.nested.value, "test");
        assert_eq!(config.nested.values, vec![1, 2, 3]);
        assert_eq!(config.optional, Some("override".to_owned()));
    }

    #[test]
    fn it_should_use_the_default_for_missing_config() {
        let toml_file = tempfile!("");
        let config: Config = ConfigFactory::load(toml_file.path());
        assert_eq!(config.nested.value, "default");
        assert_eq!(config.nested.values, vec![0, 0, 0]);
    }

    #[test]
    fn it_should_use_the_default_values_for_missing_config_values() {
        let toml_file = tempfile!(r#"
            [nested]
            value = "test"
        "#);
        let config: Config = ConfigFactory::load(toml_file.path());
        assert_eq!(config.nested.value, "test");
        assert_eq!(config.nested.values, vec![0, 0, 0]);
    }

    #[test]
    fn it_should_return_the_default_config_value_for_misconfigured_properties() {
        let toml_file = tempfile!(r#"
            [nested]
            value = "test"
            values = "wrong-type"
        "#);
        let config: Config = ConfigFactory::load(toml_file.path());
        assert_eq!(config.nested.value, "test");
        assert_eq!(config.nested.values, vec![0, 0, 0]);
    }

    #[test]
    fn it_should_ignore_unexpected_config() {
        let toml_file = tempfile!(r#"
            [nested]
            value = "test"
            values = [1, 2, 3]
            unexpected = "ignore-me"
        "#);
        let config: Config = ConfigFactory::load(toml_file.path());
        assert_eq!(config.nested.value, "test");
        assert_eq!(config.nested.values, vec![1, 2, 3]);
    }

    #[test]
    fn it_should_return_the_default_config_when_parsing_fails() {
        let toml_file = tempfile!(r#"
            [nested]
            value = "test
            values = [1, 2, 3]
        "#);
        let config: Config = ConfigFactory::load(toml_file.path());
        assert_eq!(config.nested.value, "default");
        assert_eq!(config.nested.values, vec![0, 0, 0]);
    }
}