summaryrefslogtreecommitdiffstats
path: root/examples/glob/main.rs
blob: ce2de45138ba6c4828c7bc889c6baf27b40238cb (plain)
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
use config::{Config, File};
use glob::glob;
use std::collections::HashMap;
use std::path::Path;

fn main() {
    // Option 1
    // --------
    // Gather all conf files from conf/ manually
    let mut settings = Config::default();
    settings
        // File::with_name(..) is shorthand for File::from(Path::new(..))
        .merge(File::with_name("examples/glob/conf/00-default.toml"))
        .unwrap()
        .merge(File::from(Path::new("examples/glob/conf/05-some.yml")))
        .unwrap()
        .merge(File::from(Path::new("examples/glob/conf/99-extra.json")))
        .unwrap();

    // Print out our settings (as a HashMap)
    println!(
        "\n{:?} \n\n-----------",
        settings
            .try_deserialize::<HashMap<String, String>>()
            .unwrap()
    );

    // Option 2
    // --------
    // Gather all conf files from conf/ manually, but put in 1 merge call.
    let mut settings = Config::default();
    settings
        .merge(vec![
            File::with_name("examples/glob/conf/00-default.toml"),
            File::from(Path::new("examples/glob/conf/05-some.yml")),
            File::from(Path::new("examples/glob/conf/99-extra.json")),
        ])
        .unwrap();

    // Print out our settings (as a HashMap)
    println!(
        "\n{:?} \n\n-----------",
        settings
            .try_deserialize::<HashMap<String, String>>()
            .unwrap()
    );

    // Option 3
    // --------
    // Gather all conf files from conf/ using glob and put in 1 merge call.
    let mut settings = Config::default();
    settings
        .merge(
            glob("examples/glob/conf/*")
                .unwrap()
                .map(|path| File::from(path.unwrap()))
                .collect::<Vec<_>>(),
        )
        .unwrap();

    // Print out our settings (as a HashMap)
    println!(
        "\n{:?} \n\n-----------",
        settings
            .try_deserialize::<HashMap<String, String>>()
            .unwrap()
    );
}