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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//! Internal types used to represent a configuration and corresponding primitives to browse it

use std::collections::HashMap;
use std::str::FromStr;
use std::string::ToString;
use ::error::ConfigError;

/// The top-level `Config` type that represents a configuration
#[derive(PartialEq)]
#[derive(Debug)]
pub struct Config {
    root: Value
}

/// Settings list representation. Associates settings to their names.
pub type SettingsList = HashMap<String, Setting>;

/// A `Setting` representation. Settings have a name and a value.
#[derive(PartialEq)]
#[derive(Debug)]
pub struct Setting {
    /// Setting name, as read from the configuration file
    pub name: String,
    /// This setting's value. A value can be a scalar, an array, a list, or a group.
    pub value: Value
}

/// A type representing a generic value. `Setting`s store `Value`s.
#[derive(PartialEq)]
#[derive(Debug)]
pub enum Value {
    /// A scalar
    Svalue(ScalarValue),
    /// An array
    Array(ArrayValue),
    /// A list. Arrays can only store scalars of the same type, whereas lists store `Value`s of
    /// possibly different types, including other lists.
    List(ListValue),
    /// A group. Basically, a group acts as another configuration file - it stores a `SettingsList`.
    Group(SettingsList)
}

/// The scalar values representation. Scalar values bind directly to Rust primitive types.
#[derive(PartialEq)]
#[derive(Debug)]
pub enum ScalarValue {
    /// A boolean scalar
    Boolean(bool),
    /// An i32 scalar
    Integer32(i32),
    /// An i64 scalar
    Integer64(i64),
    /// An f32 scalar
    Floating32(f32),
    /// An f64 scalar
    Floating64(f64),
    /// A string scalar
    Str(String)
}

/// The type used to represent the scalars inside an array.
/// An array can only store scalar values of the same type.
pub type ArrayValue = Vec<Value>;

/// The type used to represent the generic values inside a list.
/// Lists are heterogeneous and can store any type of value, including other lists.
pub type ListValue = Vec<Value>;

impl Config {
    /// Creates a new wrapper `Config` to hold a `SettingsList`
    pub fn new(sl: SettingsList) -> Config {
        Config { root: Value::Group(sl) }
    }

    /// Looks up a value in a configuration. A path is a dot-separated list of settings
    /// describing the path of the desired value in the configuration.
    /// Returns `None` if the path is invalid. A path is invalid if it is not syntactically
    /// well-formed, if it attempts to index an array or list beyond the limit, or if it
    /// includes an unknown setting.
    /// # Examples
    /// Suppose we have loaded a configuration that consists of:
    ///
    /// ```text
    /// my_string = "hello";
    /// a_list = ([1, 2, 3], true, { x = 4; }, "good bye");
    /// ```
    ///
    /// Then, the path to retrieve `"hello"` is `my_string`.
    /// The path to retrieve `true` inside `a_list` would be `a_list.[1]`.
    /// The path to retrieve the setting `x` inside `a_list` would be `alist.[2].x`.
    ///
    /// Here's a small demonstration:
    ///
    /// ```
    /// use config::reader::from_str;
    ///
    /// let my_conf = from_str("my_string = \"hello\"; a_list = ([1, 2, 3], true, { x = 4; }, \"good_bye\");").unwrap();
    ///
    /// let my_str_value = my_conf.lookup("my_string");
    /// assert!(my_str_value.is_some());
    ///
    /// let my_boolean_value = my_conf.lookup("a_list.[1]");
    /// assert!(my_boolean_value.is_some());
    ///
    /// let my_x_setting = my_conf.lookup("a_list.[2].x");
    /// assert!(my_x_setting.is_some());
    ///
    /// ```
    ///
    pub fn lookup(&self, path: &str) -> Option<&Value> {
        let mut last_value = &self.root;
        for segment in path.split(".") {
            if segment.starts_with("[") {
                if !segment.ends_with("]") || segment.len() < 3 {
                    return None;
                }
                if let Ok(index) = (&segment[1..segment.len()-1]).parse::<usize>() {
                    if let &Value::Array(ref arr) = last_value {
                        if index >= arr.len() {
                            return None;
                        }
                        last_value = &arr[index];
                    }
                    else if let &Value::List(ref list) = last_value {
                        if index >= list.len() {
                            return None;
                        }
                        last_value = &list[index];
                    } else {
                        return None;
                    }
                } else {
                    return None;
                }
            } else {
                if let &Value::Group(ref settings_list) = last_value {
                    let next_setting = match settings_list.get(&segment[..]) {
                        Some(v) => v,
                        None => return None
                    };
                    last_value = &next_setting.value;
                } else {
                    return None;
                }
            }
        }
        Some(last_value)
    }

    /// A convenient wrapper around `lookup()` that unwraps the underlying primitive
    /// type of a generic `Value`.
    ///
    /// Returns `None` in the same way `lookup()` does; or if the underlying `Value`
    /// type does not match with the requested type - in this case, `bool`.
    pub fn lookup_boolean(&self, path: &str) -> Option<bool> {
        self.lookup(path).and_then(|v|
                                   match v {
                                       &Value::Svalue(ScalarValue::Boolean(b)) => Some(b),
                                       _ => None
                                   })
    }

    /// A convenient wrapper around `lookup()` that unwraps the underlying primitive
    /// type of a generic `Value`.
    ///
    /// Returns `None` in the same way `lookup()` does; or if the underlying `Value`
    /// type does not match with the requested type - in this case, `i32`.
    pub fn lookup_integer32(&self, path: &str) -> Option<i32> {
        self.lookup(path).and_then(|v|
                                   match v {
                                       &Value::Svalue(ScalarValue::Integer32(x)) => Some(x),
                                       _ => None
                                   })
    }

    /// A convenient wrapper around `lookup()` that unwraps the underlying primitive
    /// type of a generic `Value`.
    ///
    /// Returns `None` in the same way `lookup()` does; or if the underlying `Value`
    /// type does not match with the requested type - in this case, `i64`.
    pub fn lookup_integer64(&self, path: &str) -> Option<i64> {
        self.lookup(path).and_then(|v|
                                   match v {
                                       &Value::Svalue(ScalarValue::Integer64(x)) => Some(x),
                                       _ => None
                                   })
    }

    /// A convenient wrapper around `lookup()` that unwraps the underlying primitive
    /// type of a generic `Value`.
    ///
    /// Returns `None` in the same way `lookup()` does; or if the underlying `Value`
    /// type does not match with the requested type - in this case, `f32`.
    pub fn lookup_floating32(&self, path: &str) -> Option<f32> {
        self.lookup(path).and_then(|v|
                                   match v {
                                       &Value::Svalue(ScalarValue::Floating32(x)) => Some(x),
                                       _ => None
                                   })
    }

    /// A convenient wrapper around `lookup()` that unwraps the underlying primitive
    /// type of a generic `Value`.
    ///
    /// Returns `None` in the same way `lookup()` does; or if the underlying `Value`
    /// type does not match with the requested type - in this case, `f64`.
    pub fn lookup_floating64(&self, path: &str) -> Option<f64> {
        self.lookup(path).and_then(|v|
                                   match v {
                                       &Value::Svalue(ScalarValue::Floating64(x)) => Some(x),
                                       _ => None
                                   })
    }

    /// A convenient wrapper around `lookup()` that unwraps the underlying primitive
    /// type of a generic `Value`.
    ///
    /// Returns `None` in the same way `lookup()` does; or if the underlying `Value`
    /// type does not match with the requested type - in this case, `String`.
    pub fn lookup_str(&self, path: &str) -> Option<&str> {
        self.lookup(path).and_then(|v|
                                   match v {
                                       &Value::Svalue(ScalarValue::Str(ref s)) => Some(&s[..]),
                                       _ => None
                                   })
    }

    /// A convenient wrapper around `lookup_boolean()` that unwraps the underlying primitive
    /// type of a boolean `Value`.
    ///
    /// If either of `lookup_boolean()` or `lookup` return `None`,
    /// then the user-provided default value is returned.
    pub fn lookup_boolean_or(&self, path: &str, default: bool) -> bool {
        self.lookup_boolean(path).unwrap_or(default)
    }

    /// A convenient wrapper around `lookup_integer32()` that unwraps the underlying primitive
    /// type of an integer32 `Value`.
    ///
    /// If either of `lookup_integer32()` or `lookup` return `None`,
    /// then the user-provided default value is returned.
    pub fn lookup_integer32_or(&self, path: &str, default: i32) -> i32 {
        self.lookup_integer32(path).unwrap_or(default)
    }

    /// A convenient wrapper around `lookup_integer64()` that unwraps the underlying primitive
    /// type of an integer64 `Value`.
    ///
    /// If either of `lookup_integer64()` or `lookup` return `None`,
    /// then the user-provided default value is returned.
    pub fn lookup_integer64_or(&self, path: &str, default: i64) -> i64 {
        self.lookup_integer64(path).unwrap_or(default)
    }

    /// A convenient wrapper around `lookup_floating32()` that unwraps the underlying primitive
    /// type of an floating32 `Value`.
    ///
    /// If either of `lookup_floating32()` or `lookup` return `None`,
    /// then the user-provided default value is returned.
    pub fn lookup_floating32_or(&self, path: &str, default: f32) -> f32 {
        self.lookup_floating32(path).unwrap_or(default)
    }

    /// A convenient wrapper around `lookup_floating64()` that unwraps the underlying primitive
    /// type of an floating64 `Value`. If either of `lookup_floating64()` or `lookup` return `None`,
    /// then the user-provided default value is returned.
    pub fn lookup_floating64_or(&self, path: &str, default: f64) -> f64 {
        self.lookup_floating64(path).unwrap_or(default)
    }

    /// A convenient wrapper around `lookup_str()` that unwraps the underlying primitive
    /// type of a string `Value`.
    ///
    /// If either of `lookup_str()` or `lookup` return `None`,
    /// then the user-provided default value is returned.
    pub fn lookup_str_or<'a>(&'a self, path: &str, default: &'a str) -> &'a str {
        self.lookup_str(path).unwrap_or(default)
    }
}

// Implement `FromStr` for `Config` so it can be constructed using `parse()` method
// on the string slice.
impl FromStr for Config {
    type Err = ConfigError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use ::reader;

        reader::from_str(s)
    }
}

impl Setting {
    /// Creates a new setting with a given name and value
    /// # Examples 
    /// Let's say we want to create a setting to store an `i32`.
    /// We start by creating a `ScalarValue`:
    ///
    /// ```
    /// use config::types::ScalarValue;
    /// # use config::types::Value;
    /// # use config::types::Setting;
    ///
    /// let setting_scalarvalue = ScalarValue::Integer32(1);
    /// # let setting_value = Value::Svalue(setting_scalarvalue);
    /// # let setting_name = "my_setting".to_string();
    /// # let my_setting = Setting::new(setting_name, setting_value);
    /// ```
    ///
    /// Then, we wrap it into a `Value`, because settings store generic values:
    ///
    /// ```
    /// # use config::types::ScalarValue;
    /// use config::types::Value;
    /// # use config::types::Setting;
    ///
    /// # let setting_scalarvalue = ScalarValue::Integer32(1);
    /// let setting_value = Value::Svalue(setting_scalarvalue);
    /// # let setting_name = "my_setting".to_string();
    /// # let my_setting = Setting::new(setting_name, setting_value);
    /// ```
    ///
    /// And then we choose a name for our setting and create it:
    ///
    /// ```
    /// # use config::types::ScalarValue;
    /// # use config::types::Value;
    /// use config::types::Setting;
    ///
    /// # let setting_scalarvalue = ScalarValue::Integer32(1);
    /// # let setting_value = Value::Svalue(setting_scalarvalue);
    /// let setting_name = "my_setting".to_string();
    /// let my_setting = Setting::new(setting_name, setting_value);
    /// ```
    ///
    /// Here's the complete example:
    ///
    /// ```
    /// use config::types::ScalarValue;
    /// use config::types::Value;
    /// use config::types::Setting;
    ///
    /// let setting_scalarvalue = ScalarValue::Integer32(1);
    /// let setting_value = Value::Svalue(setting_scalarvalue);
    /// let setting_name = "my_setting".to_string();
    /// let my_setting = Setting::new(setting_name, setting_value);
    /// ```
    ///
    pub fn new(sname: String, val: Value) -> Setting {
        Setting { name: sname, value: val }
    }
}

// Implement to_string() method for scalar value
impl ToString for ScalarValue {
    fn to_string(&self) -> String {
        match self {
            &ScalarValue::Boolean(ref value) => value.to_string(),
            &ScalarValue::Integer32(ref value) => value.to_string(),
            &ScalarValue::Integer64(ref value) => value.to_string(),
            &ScalarValue::Floating32(ref value) => value.to_string(),
            &ScalarValue::Floating64(ref value) => value.to_string(),
            &ScalarValue::Str(ref value) => value.clone(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::Config;
    use types::{Value, ScalarValue, SettingsList, Setting};

    #[test]
    fn simple_lookup_generic_bool() {

        let mut my_settings = SettingsList::new();
        my_settings.insert("windows".to_string(),
                           Setting::new("windows".to_string(),
                                        Value::Svalue(ScalarValue::Boolean(false))));
        my_settings.insert("linux".to_string(),
                           Setting::new("linux".to_string(),
                                        Value::Svalue(ScalarValue::Boolean(true))));
        my_settings.insert("UNIX".to_string(),
                           Setting::new("UNIX".to_string(),
                                        Value::Svalue(ScalarValue::Boolean(false))));

        let my_conf = Config::new(my_settings);

        let windows_lookup = my_conf.lookup("windows");
        assert!(windows_lookup.is_some());
        assert_eq!(windows_lookup.unwrap(), &Value::Svalue(ScalarValue::Boolean(false)));

        let linux_lookup = my_conf.lookup("linux");
        assert!(linux_lookup.is_some());
        assert_eq!(linux_lookup.unwrap(), &Value::Svalue(ScalarValue::Boolean(true)));

        let unix_lookup = my_conf.lookup("UNIX");
        assert!(unix_lookup.is_some());
        assert_eq!(unix_lookup.unwrap(), &Value::Svalue(ScalarValue::Boolean(false)));

    }

    #[test]
    fn simple_lookup_bool() {
        let mut my_settings = SettingsList::new();
        my_settings.insert("windows".to_string(),
                           Setting::new("windows".to_string(),
                                        Value::Svalue(ScalarValue::Boolean(false))));
        my_settings.insert("linux".to_string(),
                           Setting::new("linux".to_string(),
                                        Value::Svalue(ScalarValue::Boolean(true))));
        my_settings.insert("UNIX".to_string(),
                           Setting::new("UNIX".to_string(),
                                        Value::Svalue(ScalarValue::Boolean(false))));

        let my_conf = Config::new(my_settings);

        let windows_lookup = my_conf.lookup_boolean("windows");
        assert!(windows_lookup.is_some());
        assert_eq!(windows_lookup.unwrap(), false);

        let linux_lookup = my_conf.lookup_boolean("linux");
        assert!(linux_lookup.is_some());
        assert_eq!(linux_lookup.unwrap(), true);

        let unix_lookup = my_conf.lookup_boolean("UNIX");
        assert!(unix_lookup.is_some());
        assert_eq!(unix_lookup.unwrap(), false);
    }

    #[test]
    fn simple_lookup_generic_integer32() {

        let mut my_settings = SettingsList::new();
        my_settings.insert("miles".to_string(),
                           Setting::new("miles".to_string(),
                                        Value::Svalue(ScalarValue::Integer32(3))));
        my_settings.insert("mpg".to_string(),
                           Setting::new("mpg".to_string(),
                                        Value::Svalue(ScalarValue::Integer32(27))));

        let my_conf = Config::new(my_settings);

        let miles_lookup = my_conf.lookup("miles");
        assert!(miles_lookup.is_some());
        assert_eq!(miles_lookup.unwrap(), &Value::Svalue(ScalarValue::Integer32(3)));

        let mpg_lookup = my_conf.lookup("mpg");
        assert!(mpg_lookup.is_some());
        assert_eq!(mpg_lookup.unwrap(), &Value::Svalue(ScalarValue::Integer32(27)));
    }

    #[test]
    fn simple_lookup_integer32() {

        let mut my_settings = SettingsList::new();
        my_settings.insert("miles".to_string(),
                           Setting::new("miles".to_string(),
                                        Value::Svalue(ScalarValue::Integer32(3))));
        my_settings.insert("mpg".to_string(),
                           Setting::new("mpg".to_string(),
                                        Value::Svalue(ScalarValue::Integer32(27))));

        let my_conf = Config::new(my_settings);

        let miles_lookup = my_conf.lookup_integer32("miles");
        assert!(miles_lookup.is_some());
        assert_eq!(miles_lookup.unwrap(), 3);

        let mpg_lookup = my_conf.lookup_integer32("mpg");
        assert!(mpg_lookup.is_some());
        assert_eq!(mpg_lookup.unwrap(), 27);
    }

    #[test]
    fn simple_lookup_default() {

        let mut my_settings = SettingsList::new();
        my_settings.insert("miles".to_string(),
                           Setting::new("miles".to_string(),
                                        Value::Svalue(ScalarValue::Integer32(3))));
        my_settings.insert("mpg".to_string(),
                           Setting::new("mpg".to_string(),
                                        Value::Svalue(ScalarValue::Integer32(27))));

        let my_conf = Config::new(my_settings);

        let miles = my_conf.lookup_integer32_or("miles", 4);
        assert_eq!(miles, 3);

        let invalid_lookup = my_conf.lookup_integer32_or("blablabla", 22);
        assert_eq!(invalid_lookup, 22);
    }

    #[test]
    fn lookup_nested_empty_list() {
        // ((()));
        let mut my_settings = SettingsList::new();
        my_settings.insert("list".to_string(),
                           Setting::new("list".to_string(),
                                        Value::List(vec![
                                            Value::List(vec![
                                                Value::List(Vec::new())])])));

        let my_conf = Config::new(my_settings);

        let first = my_conf.lookup("list.[0]");
        assert!(first.is_some());
        assert_eq!(first.unwrap(), &Value::List(vec![Value::List(Vec::new())]));

        let second = my_conf.lookup("list.[0].[0]");
        assert!(second.is_some());
        assert_eq!(second.unwrap(), &Value::List(Vec::new()));
    }

    #[test]
    fn lookup_scalar_list() {

        let mut my_settings = SettingsList::new();
        my_settings.insert("my_list".to_string(),
                        Setting::new("my_list".to_string(),
                                     Value::List(vec![
                                         Value::Svalue(
                                             ScalarValue::Str("a \"string\" with \nquo\ttes"
                                                              .to_string())),
                                         Value::Svalue(
                                             ScalarValue::Integer64(9000000000000000000i64))])));

        let my_conf = Config::new(my_settings);

        let the_string = my_conf.lookup("my_list.[0]");
        assert!(the_string.is_some());
        assert_eq!(the_string.unwrap(), &Value::Svalue(ScalarValue::Str(
            "a \"string\" with \nquo\ttes".to_string())));

        let big_int = my_conf.lookup("my_list.[1]");
        assert!(big_int.is_some());
        assert_eq!(big_int.unwrap(), &Value::Svalue(ScalarValue::Integer64(9000000000000000000i64)));

    }

    #[test]
    fn lookup_array() {
        let mut my_settings = SettingsList::new();
        my_settings.insert("my_array".to_string(),
                           Setting::new("my_array".to_string(),
                                        Value::Array(vec![
                                            Value::Svalue(ScalarValue::Boolean(true)),
                                            Value::Svalue(ScalarValue::Boolean(false)),
                                            Value::Svalue(ScalarValue::Boolean(true))])));

        let my_conf = Config::new(my_settings);

        let value0 = my_conf.lookup("my_array.[0]");
        assert!(value0.is_some());
        assert_eq!(value0.unwrap(), &Value::Svalue(ScalarValue::Boolean(true)));

        let value1 = my_conf.lookup("my_array.[1]");
        assert!(value1.is_some());
        assert_eq!(value1.unwrap(), &Value::Svalue(ScalarValue::Boolean(false)));

        let value2 = my_conf.lookup("my_array.[2]");
        assert!(value2.is_some());
        assert_eq!(value2.unwrap(), &Value::Svalue(ScalarValue::Boolean(true)));
    }

    #[test]
    fn lookup_values_list() {

        /* my_superb_list = (
         *     [yes, no],
         *     21,
         *     [0.25, .5, .125],
         *     (()),
         *     (("a")),
         *     ("a"),
         *     ["\"x\""],
         *     (
         *         14,
         *         ["x"],
         *         (
         *             true,
         *             (
         *                 false,
         *                 (
         *                     4
         *                 ),
         *                 [5, 6]
         *             ),
         *             "y"
         *         )
         *    ),
         *    "goodbye!\r\n",
         *    {
         *        s = [1, 2];
         *        x = "str";
         *        y = ();
         *    }
         * )
         */

       let mut group_in_list = SettingsList::new();
        group_in_list.insert("s".to_string(),
                             Setting::new("s".to_string(),
                                          Value::Array(vec![
                                              Value::Svalue(ScalarValue::Integer32(1)),
                                              Value::Svalue(ScalarValue::Integer32(2))])));
        group_in_list.insert("x".to_string(),
                             Setting::new("x".to_string(),
                                          Value::Svalue(ScalarValue::Str("str".to_string()))));

        group_in_list.insert("y".to_string(),
                             Setting::new("y".to_string(), Value::List(Vec::new())));


        let list_elements = vec![
            Value::Array(vec![
                Value::Svalue(ScalarValue::Boolean(true)),
                Value::Svalue(ScalarValue::Boolean(false))]),
            Value::Svalue(ScalarValue::Integer32(21)),
            Value::Array(vec![
                Value::Svalue(ScalarValue::Floating32(0.25)),
                Value::Svalue(ScalarValue::Floating32(0.5)),
                Value::Svalue(ScalarValue::Floating32(0.125))]),
            Value::List(vec![Value::List(Vec::new())]),
            Value::List(vec![Value::List(vec![Value::Svalue(ScalarValue::Str("a".to_string()))])]),
            Value::List(vec![Value::Svalue(ScalarValue::Str("a".to_string()))]),
            Value::Array(vec![Value::Svalue(ScalarValue::Str("\"x\"".to_string()))]),
            Value::List(vec![Value::Svalue(ScalarValue::Integer32(14)),
                             Value::Array(vec![Value::Svalue(ScalarValue::Str("x".to_string()))]),
                             Value::List(vec![Value::Svalue(ScalarValue::Boolean(true)),
                                              Value::List(vec![
                                                  Value::Svalue(ScalarValue::Boolean(false)),
                                                  Value::List(vec![
                                                      Value::Svalue(ScalarValue::Integer32(4))]),
                                                  Value::Array(vec![
                                                      Value::Svalue(ScalarValue::Integer32(5)),
                                                      Value::Svalue(ScalarValue::Integer32(6))])]),
                                              Value::Svalue(ScalarValue::Str("y".to_string()))])]),
            Value::Svalue(ScalarValue::Str("goodbye!\r\n".to_string())),
            Value::Group(group_in_list)];

        let mut my_settings = SettingsList::new();
        my_settings.insert("my_superb_list".to_string(),
                           Setting::new("my_superb_list".to_string(), Value::List(list_elements)));

        let my_conf = Config::new(my_settings);

        let lookup_bool = my_conf.lookup("my_superb_list.[0].[1]");
        assert!(lookup_bool.is_some());
        assert_eq!(lookup_bool.unwrap(), &Value::Svalue(ScalarValue::Boolean(false)));

        let lookup_empty_lst = my_conf.lookup("my_superb_list.[3].[0]");
        assert!(lookup_empty_lst.is_some());
        assert_eq!(lookup_empty_lst.unwrap(), &Value::List(Vec::new()));

        let lookup_deep = my_conf.lookup("my_superb_list.[7].[2].[1].[2].[1]");
        assert!(lookup_deep.is_some());
        assert_eq!(lookup_deep.unwrap(), &Value::Svalue(ScalarValue::Integer32(6)));

        let lookup_str = my_conf.lookup("my_superb_list.[9].x");
        assert!(lookup_str.is_some());
        assert_eq!(lookup_str.unwrap(), &Value::Svalue(ScalarValue::Str("str".to_string())));

        let lookup_deep_int = my_conf.lookup("my_superb_list.[9].s.[1]");
        assert!(lookup_deep_int.is_some());
        assert_eq!(lookup_deep_int.unwrap(), &Value::Svalue(ScalarValue::Integer32(2)));

        let lookup_empty_lst = my_conf.lookup("my_superb_list.[9].y");
        assert!(lookup_empty_lst.is_some());
        assert_eq!(lookup_empty_lst.unwrap(), &Value::List(Vec::new()));
    }

    #[test]
    fn lookup_invalid_path() {
        let mut my_settings = SettingsList::new();
        my_settings.insert("my_array".to_string(),
                           Setting::new("my_array".to_string(),
                                        Value::Array(vec![
                                            Value::Svalue(ScalarValue::Boolean(true)),
                                            Value::Svalue(ScalarValue::Boolean(false)),
                                            Value::Svalue(ScalarValue::Boolean(true))])));

        let my_conf = Config::new(my_settings);

        let value0 = my_conf.lookup("my_array.[1456]");
        assert!(value0.is_none());

        let value1 = my_conf.lookup("my_array.[-30]");
        assert!(value1.is_none());

        let value2 = my_conf.lookup("something_that_does_not_exist.[14].lala.lele.[24]");
        assert!(value2.is_none());
    }

    #[test]
    fn lookup_invalid_type() {
        let mut my_settings = SettingsList::new();
        my_settings.insert("my_array".to_string(),
                           Setting::new("my_array".to_string(),
                                        Value::Array(vec![
                                            Value::Svalue(ScalarValue::Boolean(true)),
                                            Value::Svalue(ScalarValue::Boolean(false)),
                                            Value::Svalue(ScalarValue::Boolean(true))])));

        let my_conf = Config::new(my_settings);

        let value0 = my_conf.lookup_integer32("my_array.[0]");
        assert!(value0.is_none());

        let value1 = my_conf.lookup_str("my_array.[1]");
        assert!(value1.is_none());
    }

    #[test]
    fn scalar_value_to_string() {
        let mut value = ScalarValue::Boolean(true);
        assert_eq!(value.to_string(), "true");

        value = ScalarValue::Integer32(32i32);
        assert_eq!(value.to_string(), "32");

        value = ScalarValue::Integer64(-64i64);
        assert_eq!(value.to_string(), "-64");

        value = ScalarValue::Floating32(3f32);
        assert!(value.to_string().starts_with("3"));

        value = ScalarValue::Floating64(99f64);
        assert!(value.to_string().starts_with("99"));

        value = ScalarValue::Str("this is a string".to_string());
        assert_eq!(value.to_string(), "this is a string");
    }

    #[test]
    fn parse_config_from_str_slice() {
        let config: Config = "answer=42;".parse().unwrap();
        assert!(config.lookup_integer32("answer").is_some());
        assert_eq!(config.lookup_integer32("answer").unwrap().to_string(), "42".to_string());
    }
}