Skip to content

Commit 48f49c6

Browse files
committed
remove some deprecated code from examples
1 parent 4575e9b commit 48f49c6

File tree

5 files changed

+41
-42
lines changed

5 files changed

+41
-42
lines changed

examples/glob/main.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ fn main() {
77
// Option 1
88
// --------
99
// Gather all conf files from conf/ manually
10-
let mut settings = Config::default();
11-
settings
10+
let settings = Config::builder()
1211
// File::with_name(..) is shorthand for File::from(Path::new(..))
13-
.merge(File::with_name("examples/glob/conf/00-default.toml"))
14-
.unwrap()
15-
.merge(File::from(Path::new("examples/glob/conf/05-some.yml")))
16-
.unwrap()
17-
.merge(File::from(Path::new("examples/glob/conf/99-extra.json")))
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()
1816
.unwrap();
1917

2018
// Print out our settings (as a HashMap)
@@ -28,13 +26,13 @@ fn main() {
2826
// Option 2
2927
// --------
3028
// Gather all conf files from conf/ manually, but put in 1 merge call.
31-
let mut settings = Config::default();
32-
settings
33-
.merge(vec![
29+
let settings = Config::builder()
30+
.add_source(vec![
3431
File::with_name("examples/glob/conf/00-default.toml"),
3532
File::from(Path::new("examples/glob/conf/05-some.yml")),
3633
File::from(Path::new("examples/glob/conf/99-extra.json")),
3734
])
35+
.build()
3836
.unwrap();
3937

4038
// Print out our settings (as a HashMap)
@@ -48,14 +46,14 @@ fn main() {
4846
// Option 3
4947
// --------
5048
// Gather all conf files from conf/ using glob and put in 1 merge call.
51-
let mut settings = Config::default();
52-
settings
53-
.merge(
49+
let settings = Config::builder()
50+
.add_source(
5451
glob("examples/glob/conf/*")
5552
.unwrap()
5653
.map(|path| File::from(path.unwrap()))
5754
.collect::<Vec<_>>(),
5855
)
56+
.build()
5957
.unwrap();
6058

6159
// Print out our settings (as a HashMap)

examples/hierarchical-env/settings.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ use config::{Config, ConfigError, Environment, File};
22
use std::env;
33

44
#[derive(Debug, Deserialize)]
5+
#[allow(unused)]
56
struct Database {
67
url: String,
78
}
89

910
#[derive(Debug, Deserialize)]
11+
#[allow(unused)]
1012
struct Sparkpost {
1113
key: String,
1214
token: String,
@@ -15,19 +17,22 @@ struct Sparkpost {
1517
}
1618

1719
#[derive(Debug, Deserialize)]
20+
#[allow(unused)]
1821
struct Twitter {
1922
consumer_token: String,
2023
consumer_secret: String,
2124
}
2225

2326
#[derive(Debug, Deserialize)]
27+
#[allow(unused)]
2428
struct Braintree {
2529
merchant_id: String,
2630
public_key: String,
2731
private_key: String,
2832
}
2933

3034
#[derive(Debug, Deserialize)]
35+
#[allow(unused)]
3136
pub struct Settings {
3237
debug: bool,
3338
database: Database,
@@ -38,29 +43,27 @@ pub struct Settings {
3843

3944
impl Settings {
4045
pub fn new() -> Result<Self, ConfigError> {
41-
let mut s = Config::default();
46+
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
4247

43-
// Start off by merging in the "default" configuration file
44-
s.merge(File::with_name("examples/hierarchical-env/config/default"))?;
45-
46-
// Add in the current environment file
47-
// Default to 'development' env
48-
// Note that this file is _optional_
49-
let env = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
50-
s.merge(
51-
File::with_name(&format!("examples/hierarchical-env/config/{}", env)).required(false),
52-
)?;
53-
54-
// Add in a local configuration file
55-
// This file shouldn't be checked in to git
56-
s.merge(File::with_name("examples/hierarchical-env/config/local").required(false))?;
57-
58-
// Add in settings from the environment (with a prefix of APP)
59-
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
60-
s.merge(Environment::with_prefix("app"))?;
61-
62-
// You may also programmatically change settings
63-
s.set("database.url", "postgres://")?;
48+
let s = Config::builder()
49+
// Start off by merging in the "default" configuration file
50+
.add_source(File::with_name("examples/hierarchical-env/config/default"))
51+
// Add in the current environment file
52+
// Default to 'development' env
53+
// Note that this file is _optional_
54+
.add_source(
55+
File::with_name(&format!("examples/hierarchical-env/config/{}", run_mode))
56+
.required(false),
57+
)
58+
// Add in a local configuration file
59+
// This file shouldn't be checked in to git
60+
.add_source(File::with_name("examples/hierarchical-env/config/local").required(false))
61+
// Add in settings from the environment (with a prefix of APP)
62+
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
63+
.add_source(Environment::with_prefix("app"))
64+
// You may also programmatically change settings
65+
.set_override("database.url", "postgres://")?
66+
.build()?;
6467

6568
// Now that we're done, let's access our configuration
6669
println!("debug: {:?}", s.get_bool("debug"));

examples/simple/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
use config::Config;
12
use std::collections::HashMap;
23

34
fn main() {
4-
let mut settings = config::Config::default();
5-
settings
5+
let settings = Config::builder()
66
// Add in `./Settings.toml`
7-
.merge(config::File::with_name("examples/simple/Settings"))
8-
.unwrap()
7+
.add_source(config::File::with_name("examples/simple/Settings"))
98
// Add in settings from the environment (with a prefix of APP)
109
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
11-
.merge(config::Environment::with_prefix("APP"))
10+
.add_source(config::Environment::with_prefix("APP"))
11+
.build()
1212
.unwrap();
1313

1414
// Print out our settings (as a HashMap)

src/env.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::source::Source;
66
use crate::value::{Value, ValueKind};
77

88
#[derive(Clone, Debug, Default)]
9-
#[must_use]
109
pub struct Environment {
1110
/// Optional prefix that will limit access to the environment to only keys that
1211
/// begin with the defined prefix.

src/file/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub use self::source::string::FileSourceString;
2020
///
2121
/// It supports optional automatic file format discovery.
2222
#[derive(Clone, Debug)]
23-
#[must_use]
2423
pub struct File<T, F> {
2524
source: T,
2625

0 commit comments

Comments
 (0)