From 14224be23dc2f253a240b85214927d97e1160669 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sun, 30 Jul 2017 13:20:36 -0700 Subject: Remove ConfigResult; close #36 --- examples/global/Cargo.toml | 7 +++++++ examples/global/src/main.rs | 26 ++++++++++++++++++++++++++ examples/hierarchical-env/src/settings.rs | 16 ++++++++-------- examples/pattern/src/main.rs | 16 +++++++++------- examples/simple/src/main.rs | 8 ++++---- examples/watch/Settings.toml | 2 +- examples/watch/src/main.rs | 10 ++++++---- 7 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 examples/global/Cargo.toml create mode 100644 examples/global/src/main.rs (limited to 'examples') diff --git a/examples/global/Cargo.toml b/examples/global/Cargo.toml new file mode 100644 index 0000000..ec24740 --- /dev/null +++ b/examples/global/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "global" +version = "0.1.0" + +[dependencies] +config = { path = "../../" } +lazy_static = "^0.2.8" diff --git a/examples/global/src/main.rs b/examples/global/src/main.rs new file mode 100644 index 0000000..4fe0864 --- /dev/null +++ b/examples/global/src/main.rs @@ -0,0 +1,26 @@ +#[macro_use] +extern crate lazy_static; + +extern crate config; + +use std::error::Error; +use std::sync::RwLock; +use config::Config; + +lazy_static! { + static ref SETTINGS: RwLock = RwLock::new(Config::default()); +} + +fn try_main() -> Result<(), Box> { + // Set property + SETTINGS.write()?.set("property", 42)?; + + // Get property + println!("property: {}", SETTINGS.read()?.get::("property")?); + + Ok(()) +} + +fn main() { + try_main().unwrap() +} diff --git a/examples/hierarchical-env/src/settings.rs b/examples/hierarchical-env/src/settings.rs index 43897b3..9e07054 100644 --- a/examples/hierarchical-env/src/settings.rs +++ b/examples/hierarchical-env/src/settings.rs @@ -1,5 +1,5 @@ use std::env; -use config::{Config, File, Environment}; +use config::{ConfigError, Config, File, Environment}; #[derive(Debug, Deserialize)] struct Database { @@ -37,34 +37,34 @@ pub struct Settings { } impl Settings { - pub fn new() -> Self { + pub fn new() -> Result { let mut s = Config::new(); // Start off by merging in the "default" configuration file - s.merge(File::with_name("config/default")).unwrap(); + s.merge(File::with_name("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("development".into()); - s.merge(File::with_name(&format!("config/{}", env)).required(false)).unwrap(); + s.merge(File::with_name(&format!("config/{}", env)).required(false))?; // Add in a local configuration file // This file shouldn't be checked in to git - s.merge(File::with_name("config/local").required(false)).unwrap(); + s.merge(File::with_name("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")).unwrap(); + s.merge(Environment::with_prefix("app"))?; // You may also programmatically change settings - s.set("database.url", "postgres://").unwrap(); + s.set("database.url", "postgres://")?; // Now that we're done, let's access our configuration println!("debug: {:?}", s.get_bool("debug")); println!("database: {:?}", s.get::("database.url")); // You can deserialize (and thus freeze) the entire configuration as - s.deserialize().unwrap() + s.deserialize() } } diff --git a/examples/pattern/src/main.rs b/examples/pattern/src/main.rs index aaf164d..f88f1ed 100644 --- a/examples/pattern/src/main.rs +++ b/examples/pattern/src/main.rs @@ -10,12 +10,12 @@ fn main() { // Option 1 // -------- // Gather all conf files from conf/ manually - let settings = Config::default() + let mut settings = Config::default(); + settings // File::with_name(..) is shorthand for File::from(Path::new(..)) - .merge(File::with_name("conf/00-default.toml")) - .merge(File::from(Path::new("conf/05-some.yml"))) - .merge(File::from(Path::new("conf/99-extra.json"))) - .unwrap(); + .merge(File::with_name("conf/00-default.toml")).unwrap() + .merge(File::from(Path::new("conf/05-some.yml"))).unwrap() + .merge(File::from(Path::new("conf/99-extra.json"))).unwrap(); // Print out our settings (as a HashMap) println!("\n{:?} \n\n-----------", @@ -24,7 +24,8 @@ fn main() { // Option 2 // -------- // Gather all conf files from conf/ manually, but put in 1 merge call. - let settings = Config::default() + let mut settings = Config::default(); + settings .merge(vec![File::with_name("conf/00-default.toml"), File::from(Path::new("conf/05-some.yml")), File::from(Path::new("conf/99-extra.json"))]) @@ -37,7 +38,8 @@ fn main() { // Option 3 // -------- // Gather all conf files from conf/ using glob and put in 1 merge call. - let settings = Config::default() + let mut settings = Config::default(); + settings .merge(glob("conf/*") .unwrap() .map(|path| File::from(path.unwrap())) diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index debad02..fb66e03 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -3,13 +3,13 @@ extern crate config; use std::collections::HashMap; fn main() { - let settings = config::Config::default() + let mut settings = config::Config::default(); + settings // Add in `./Settings.toml` - .merge(config::File::with_name("Settings")) + .merge(config::File::with_name("Settings")).unwrap() // 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")) - .unwrap(); + .merge(config::Environment::with_prefix("APP")).unwrap(); // Print out our settings (as a HashMap) println!("{:?}", diff --git a/examples/watch/Settings.toml b/examples/watch/Settings.toml index f7881bb..8443f7b 100644 --- a/examples/watch/Settings.toml +++ b/examples/watch/Settings.toml @@ -1,3 +1,3 @@ -debug = true +debug = false port = 8080 host = "0.0.0.0" diff --git a/examples/watch/src/main.rs b/examples/watch/src/main.rs index 70fb7ef..0976f74 100644 --- a/examples/watch/src/main.rs +++ b/examples/watch/src/main.rs @@ -12,10 +12,12 @@ use std::sync::mpsc::channel; use std::time::Duration; lazy_static! { - static ref SETTINGS: RwLock = RwLock::new(Config::default() - .merge(File::with_name("Settings.toml")) - .unwrap() - ); + static ref SETTINGS: RwLock = RwLock::new({ + let mut settings = Config::default(); + settings.merge(File::with_name("Settings.toml")).unwrap(); + + settings + }); } fn show() { -- cgit v1.2.3