summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Eades <danieleades@hotmail.com>2021-12-29 11:22:10 +0000
committerDaniel Eades <danieleades@hotmail.com>2021-12-29 12:12:06 +0000
commit017adeaaa3fe84241b9696a9cd8f1928a7f2725a (patch)
tree1e30f27b7cbaa35b4c6e87ad3b2536f2f1284d9d
parent41b3de8d4acbeedc1889b97771de329ae791f354 (diff)
remove some deprecated code from examples
-rw-r--r--examples/glob/main.rs24
-rw-r--r--examples/hierarchical-env/settings.rs47
-rw-r--r--examples/simple/main.rs10
3 files changed, 41 insertions, 40 deletions
diff --git a/examples/glob/main.rs b/examples/glob/main.rs
index ce2de45..d5225a0 100644
--- a/examples/glob/main.rs
+++ b/examples/glob/main.rs
@@ -7,14 +7,12 @@ fn main() {
// Option 1
// --------
// Gather all conf files from conf/ manually
- let mut settings = Config::default();
- settings
+ let settings = Config::builder()
// 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")))
+ .add_source(File::with_name("examples/glob/conf/00-default.toml"))
+ .add_source(File::from(Path::new("examples/glob/conf/05-some.yml")))
+ .add_source(File::from(Path::new("examples/glob/conf/99-extra.json")))
+ .build()
.unwrap();
// Print out our settings (as a HashMap)
@@ -28,13 +26,13 @@ fn main() {
// Option 2
// --------
// Gather all conf files from conf/ manually, but put in 1 merge call.
- let mut settings = Config::default();
- settings
- .merge(vec![
+ let settings = Config::builder()
+ .add_source(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")),
])
+ .build()
.unwrap();
// Print out our settings (as a HashMap)
@@ -48,14 +46,14 @@ fn main() {
// Option 3
// --------
// Gather all conf files from conf/ using glob and put in 1 merge call.
- let mut settings = Config::default();
- settings
- .merge(
+ let settings = Config::builder()
+ .add_source(
glob("examples/glob/conf/*")
.unwrap()
.map(|path| File::from(path.unwrap()))
.collect::<Vec<_>>(),
)
+ .build()
.unwrap();
// Print out our settings (as a HashMap)
diff --git a/examples/hierarchical-env/settings.rs b/examples/hierarchical-env/settings.rs
index 6286e5c..65b5f87 100644
--- a/examples/hierarchical-env/settings.rs
+++ b/examples/hierarchical-env/settings.rs
@@ -3,11 +3,13 @@ use serde_derive::Deserialize;
use std::env;
#[derive(Debug, Deserialize)]
+#[allow(unused)]
struct Database {
url: String,
}
#[derive(Debug, Deserialize)]
+#[allow(unused)]
struct Sparkpost {
key: String,
token: String,
@@ -16,12 +18,14 @@ struct Sparkpost {
}
#[derive(Debug, Deserialize)]
+#[allow(unused)]
struct Twitter {
consumer_token: String,
consumer_secret: String,
}
#[derive(Debug, Deserialize)]
+#[allow(unused)]
struct Braintree {
merchant_id: String,
public_key: String,
@@ -29,6 +33,7 @@ struct Braintree {
}
#[derive(Debug, Deserialize)]
+#[allow(unused)]
pub struct Settings {
debug: bool,
database: Database,
@@ -39,29 +44,27 @@ pub struct Settings {
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
- let mut s = Config::default();
+ let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
- // Start off by merging in the "default" configuration file
- s.merge(File::with_name("examples/hierarchical-env/config/default"))?;
-
- // Add in the current environment file
- // Default to 'development' env
- // Note that this file is _optional_
- let env = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
- s.merge(
- File::with_name(&format!("examples/hierarchical-env/config/{}", env)).required(false),
- )?;
-
- // Add in a local configuration file
- // This file shouldn't be checked in to git
- s.merge(File::with_name("examples/hierarchical-env/config/local").required(false))?;
-
- // Add in settings from the environment (with a prefix of APP)
- // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
- s.merge(Environment::with_prefix("app"))?;
-
- // You may also programmatically change settings
- s.set("database.url", "postgres://")?;
+ let s = Config::builder()
+ // Start off by merging in the "default" configuration file
+ .add_source(File::with_name("examples/hierarchical-env/config/default"))
+ // Add in the current environment file
+ // Default to 'development' env
+ // Note that this file is _optional_
+ .add_source(
+ File::with_name(&format!("examples/hierarchical-env/config/{}", run_mode))
+ .required(false),
+ )
+ // Add in a local configuration file
+ // This file shouldn't be checked in to git
+ .add_source(File::with_name("examples/hierarchical-env/config/local").required(false))
+ // Add in settings from the environment (with a prefix of APP)
+ // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
+ .add_source(Environment::with_prefix("app"))
+ // You may also programmatically change settings
+ .set_override("database.url", "postgres://")?
+ .build()?;
// Now that we're done, let's access our configuration
println!("debug: {:?}", s.get_bool("debug"));
diff --git a/examples/simple/main.rs b/examples/simple/main.rs
index 5aa8818..55ae8ac 100644
--- a/examples/simple/main.rs
+++ b/examples/simple/main.rs
@@ -1,14 +1,14 @@
+use config::Config;
use std::collections::HashMap;
fn main() {
- let mut settings = config::Config::default();
- settings
+ let settings = Config::builder()
// Add in `./Settings.toml`
- .merge(config::File::with_name("examples/simple/Settings"))
- .unwrap()
+ .add_source(config::File::with_name("examples/simple/Settings"))
// Add in settings from the environment (with a prefix of APP)
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
- .merge(config::Environment::with_prefix("APP"))
+ .add_source(config::Environment::with_prefix("APP"))
+ .build()
.unwrap();
// Print out our settings (as a HashMap)