1 use config::{Config, File};
2 use glob::glob;
3 use std::collections::HashMap;
4 use std::path::Path;
5 
main()6 fn main() {
7     // Option 1
8     // --------
9     // Gather all conf files from conf/ manually
10     let settings = Config::builder()
11         // File::with_name(..) is shorthand for File::from(Path::new(..))
12         .add_source(File::with_name("examples/glob/conf/00-default.toml"))
13         .add_source(File::from(Path::new("examples/glob/conf/05-some.yml")))
14         .add_source(File::from(Path::new("examples/glob/conf/99-extra.json")))
15         .build()
16         .unwrap();
17 
18     // Print out our settings (as a HashMap)
19     println!(
20         "\n{:?} \n\n-----------",
21         settings
22             .try_deserialize::<HashMap<String, String>>()
23             .unwrap()
24     );
25 
26     // Option 2
27     // --------
28     // Gather all conf files from conf/ manually, but put in 1 merge call.
29     let settings = Config::builder()
30         .add_source(vec![
31             File::with_name("examples/glob/conf/00-default.toml"),
32             File::from(Path::new("examples/glob/conf/05-some.yml")),
33             File::from(Path::new("examples/glob/conf/99-extra.json")),
34         ])
35         .build()
36         .unwrap();
37 
38     // Print out our settings (as a HashMap)
39     println!(
40         "\n{:?} \n\n-----------",
41         settings
42             .try_deserialize::<HashMap<String, String>>()
43             .unwrap()
44     );
45 
46     // Option 3
47     // --------
48     // Gather all conf files from conf/ using glob and put in 1 merge call.
49     let settings = Config::builder()
50         .add_source(
51             glob("examples/glob/conf/*")
52                 .unwrap()
53                 .map(|path| File::from(path.unwrap()))
54                 .collect::<Vec<_>>(),
55         )
56         .build()
57         .unwrap();
58 
59     // Print out our settings (as a HashMap)
60     println!(
61         "\n{:?} \n\n-----------",
62         settings
63             .try_deserialize::<HashMap<String, String>>()
64             .unwrap()
65     );
66 }
67