From 21526221aeb0c35c7e4cdbb631b431147bd54ed5 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Mon, 22 Nov 2021 14:22:03 +0000 Subject: fix examples to work as cargo expects --- examples/glob/Cargo.toml | 8 ---- examples/glob/main.rs | 49 ++++++++++++++++++++++ examples/glob/src/main.rs | 49 ---------------------- examples/global/Cargo.toml | 8 ---- examples/global/main.rs | 26 ++++++++++++ examples/global/src/main.rs | 26 ------------ examples/hierarchical-env/Cargo.toml | 9 ---- examples/hierarchical-env/main.rs | 16 +++++++ examples/hierarchical-env/settings.rs | 70 +++++++++++++++++++++++++++++++ examples/hierarchical-env/src/main.rs | 16 ------- examples/hierarchical-env/src/settings.rs | 70 ------------------------------- examples/simple/Cargo.toml | 7 ---- examples/simple/main.rs | 15 +++++++ examples/simple/src/main.rs | 15 ------- examples/watch/Cargo.toml | 9 ---- examples/watch/main.rs | 67 +++++++++++++++++++++++++++++ examples/watch/src/main.rs | 67 ----------------------------- 17 files changed, 243 insertions(+), 284 deletions(-) delete mode 100644 examples/glob/Cargo.toml create mode 100644 examples/glob/main.rs delete mode 100644 examples/glob/src/main.rs delete mode 100644 examples/global/Cargo.toml create mode 100644 examples/global/main.rs delete mode 100644 examples/global/src/main.rs delete mode 100644 examples/hierarchical-env/Cargo.toml create mode 100644 examples/hierarchical-env/main.rs create mode 100644 examples/hierarchical-env/settings.rs delete mode 100644 examples/hierarchical-env/src/main.rs delete mode 100644 examples/hierarchical-env/src/settings.rs delete mode 100644 examples/simple/Cargo.toml create mode 100644 examples/simple/main.rs delete mode 100644 examples/simple/src/main.rs delete mode 100644 examples/watch/Cargo.toml create mode 100644 examples/watch/main.rs delete mode 100644 examples/watch/src/main.rs (limited to 'examples') diff --git a/examples/glob/Cargo.toml b/examples/glob/Cargo.toml deleted file mode 100644 index 2fe1b1e..0000000 --- a/examples/glob/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "glob" -version = "0.1.0" -edition = "2018" - -[dependencies] -config = { path = "../../" } -glob = "0.3" diff --git a/examples/glob/main.rs b/examples/glob/main.rs new file mode 100644 index 0000000..5e81f3c --- /dev/null +++ b/examples/glob/main.rs @@ -0,0 +1,49 @@ +use std::path::Path; +use std::collections::HashMap; +use config::*; +use glob::glob; + +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("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-----------", + settings.try_deserialize::>().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("conf/00-default.toml"), + File::from(Path::new("conf/05-some.yml")), + File::from(Path::new("conf/99-extra.json"))]) + .unwrap(); + + // Print out our settings (as a HashMap) + println!("\n{:?} \n\n-----------", + settings.try_deserialize::>().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("conf/*") + .unwrap() + .map(|path| File::from(path.unwrap())) + .collect::>()) + .unwrap(); + + // Print out our settings (as a HashMap) + println!("\n{:?} \n\n-----------", + settings.try_deserialize::>().unwrap()); +} diff --git a/examples/glob/src/main.rs b/examples/glob/src/main.rs deleted file mode 100644 index 5e81f3c..0000000 --- a/examples/glob/src/main.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::path::Path; -use std::collections::HashMap; -use config::*; -use glob::glob; - -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("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-----------", - settings.try_deserialize::>().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("conf/00-default.toml"), - File::from(Path::new("conf/05-some.yml")), - File::from(Path::new("conf/99-extra.json"))]) - .unwrap(); - - // Print out our settings (as a HashMap) - println!("\n{:?} \n\n-----------", - settings.try_deserialize::>().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("conf/*") - .unwrap() - .map(|path| File::from(path.unwrap())) - .collect::>()) - .unwrap(); - - // Print out our settings (as a HashMap) - println!("\n{:?} \n\n-----------", - settings.try_deserialize::>().unwrap()); -} diff --git a/examples/global/Cargo.toml b/examples/global/Cargo.toml deleted file mode 100644 index 8a5155f..0000000 --- a/examples/global/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "global" -version = "0.1.0" -edition = "2018" - -[dependencies] -config = { path = "../../" } -lazy_static = "1" diff --git a/examples/global/main.rs b/examples/global/main.rs new file mode 100644 index 0000000..8e0e068 --- /dev/null +++ b/examples/global/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/global/src/main.rs b/examples/global/src/main.rs deleted file mode 100644 index 8e0e068..0000000 --- a/examples/global/src/main.rs +++ /dev/null @@ -1,26 +0,0 @@ -#[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/Cargo.toml b/examples/hierarchical-env/Cargo.toml deleted file mode 100644 index 421d10d..0000000 --- a/examples/hierarchical-env/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "hierarchical-env" -version = "0.1.0" -edition = "2018" - -[dependencies] -config = { path = "../../" } -serde_derive = "^1.0.8" -serde = "^1.0.8" diff --git a/examples/hierarchical-env/main.rs b/examples/hierarchical-env/main.rs new file mode 100644 index 0000000..65f727d --- /dev/null +++ b/examples/hierarchical-env/main.rs @@ -0,0 +1,16 @@ +extern crate config; +extern crate serde; + +#[macro_use] +extern crate serde_derive; + +mod settings; + +use settings::Settings; + +fn main() { + let settings = Settings::new(); + + // Print out our settings + println!("{:?}", settings); +} diff --git a/examples/hierarchical-env/settings.rs b/examples/hierarchical-env/settings.rs new file mode 100644 index 0000000..7ef58b8 --- /dev/null +++ b/examples/hierarchical-env/settings.rs @@ -0,0 +1,70 @@ +use std::env; +use config::{ConfigError, Config, File, Environment}; + +#[derive(Debug, Deserialize)] +struct Database { + url: String, +} + +#[derive(Debug, Deserialize)] +struct Sparkpost { + key: String, + token: String, + url: String, + version: u8, +} + +#[derive(Debug, Deserialize)] +struct Twitter { + consumer_token: String, + consumer_secret: String, +} + +#[derive(Debug, Deserialize)] +struct Braintree { + merchant_id: String, + public_key: String, + private_key: String, +} + +#[derive(Debug, Deserialize)] +pub struct Settings { + debug: bool, + database: Database, + sparkpost: Sparkpost, + twitter: Twitter, + braintree: Braintree, +} + +impl Settings { + pub fn new() -> Result { + let mut s = Config::default(); + + // Start off by merging in the "default" configuration file + 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_else(|_| "development".into()); + 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))?; + + // 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://")?; + + // 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.try_deserialize() + } +} diff --git a/examples/hierarchical-env/src/main.rs b/examples/hierarchical-env/src/main.rs deleted file mode 100644 index 65f727d..0000000 --- a/examples/hierarchical-env/src/main.rs +++ /dev/null @@ -1,16 +0,0 @@ -extern crate config; -extern crate serde; - -#[macro_use] -extern crate serde_derive; - -mod settings; - -use settings::Settings; - -fn main() { - let settings = Settings::new(); - - // Print out our settings - println!("{:?}", settings); -} diff --git a/examples/hierarchical-env/src/settings.rs b/examples/hierarchical-env/src/settings.rs deleted file mode 100644 index 7ef58b8..0000000 --- a/examples/hierarchical-env/src/settings.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::env; -use config::{ConfigError, Config, File, Environment}; - -#[derive(Debug, Deserialize)] -struct Database { - url: String, -} - -#[derive(Debug, Deserialize)] -struct Sparkpost { - key: String, - token: String, - url: String, - version: u8, -} - -#[derive(Debug, Deserialize)] -struct Twitter { - consumer_token: String, - consumer_secret: String, -} - -#[derive(Debug, Deserialize)] -struct Braintree { - merchant_id: String, - public_key: String, - private_key: String, -} - -#[derive(Debug, Deserialize)] -pub struct Settings { - debug: bool, - database: Database, - sparkpost: Sparkpost, - twitter: Twitter, - braintree: Braintree, -} - -impl Settings { - pub fn new() -> Result { - let mut s = Config::default(); - - // Start off by merging in the "default" configuration file - 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_else(|_| "development".into()); - 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))?; - - // 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://")?; - - // 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.try_deserialize() - } -} diff --git a/examples/simple/Cargo.toml b/examples/simple/Cargo.toml deleted file mode 100644 index f1d8097..0000000 --- a/examples/simple/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "simple" -version = "0.1.0" -edition = "2018" - -[dependencies] -config = { path = "../../" } diff --git a/examples/simple/main.rs b/examples/simple/main.rs new file mode 100644 index 0000000..b38dcbf --- /dev/null +++ b/examples/simple/main.rs @@ -0,0 +1,15 @@ +use std::collections::HashMap; + +fn main() { + let mut settings = config::Config::default(); + settings + // Add in `./Settings.toml` + .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(); + + // Print out our settings (as a HashMap) + println!("{:?}", + settings.try_deserialize::>().unwrap()); +} diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs deleted file mode 100644 index b38dcbf..0000000 --- a/examples/simple/src/main.rs +++ /dev/null @@ -1,15 +0,0 @@ -use std::collections::HashMap; - -fn main() { - let mut settings = config::Config::default(); - settings - // Add in `./Settings.toml` - .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(); - - // Print out our settings (as a HashMap) - println!("{:?}", - settings.try_deserialize::>().unwrap()); -} diff --git a/examples/watch/Cargo.toml b/examples/watch/Cargo.toml deleted file mode 100644 index e3245cb..0000000 --- a/examples/watch/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "watch" -version = "0.1.0" -edition = "2018" - -[dependencies] -config = { path = "../../" } -lazy_static = "1" -notify = "^4.0.0" diff --git a/examples/watch/main.rs b/examples/watch/main.rs new file mode 100644 index 0000000..9f3d21a --- /dev/null +++ b/examples/watch/main.rs @@ -0,0 +1,67 @@ +use config::*; +use std::collections::HashMap; +use std::sync::RwLock; +use notify::{RecommendedWatcher, DebouncedEvent, Watcher, RecursiveMode}; +use std::sync::mpsc::channel; +use std::time::Duration; + +lazy_static::lazy_static! { + static ref SETTINGS: RwLock = RwLock::new({ + let mut settings = Config::default(); + settings.merge(File::with_name("Settings.toml")).unwrap(); + + settings + }); +} + +fn show() { + println!(" * Settings :: \n\x1b[31m{:?}\x1b[0m", + SETTINGS + .read() + .unwrap() + .clone() + .try_deserialize::>() + .unwrap()); +} + +fn watch() { + // Create a channel to receive the events. + let (tx, rx) = channel(); + + // Automatically select the best implementation for your platform. + // You can also access each implementation directly e.g. INotifyWatcher. + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2)).unwrap(); + + // Add a path to be watched. All files and directories at that path and + // below will be monitored for changes. + watcher + .watch("./Settings.toml", RecursiveMode::NonRecursive) + .unwrap(); + + // This is a simple loop, but you may want to use more complex logic here, + // for example to handle I/O. + loop { + match rx.recv() { + Ok(DebouncedEvent::Write(_)) => { + println!(" * Settings.toml written; refreshing configuration ..."); + SETTINGS.write().unwrap().refresh().unwrap(); + show(); + } + + Err(e) => println!("watch error: {:?}", e), + + _ => { + // Ignore event + } + } + } +} + +fn main() { + // This is just an example of what could be done, today + // We do want this to be built-in to config-rs at some point + // Feel free to take a crack at a PR + + show(); + watch(); +} diff --git a/examples/watch/src/main.rs b/examples/watch/src/main.rs deleted file mode 100644 index 9f3d21a..0000000 --- a/examples/watch/src/main.rs +++ /dev/null @@ -1,67 +0,0 @@ -use config::*; -use std::collections::HashMap; -use std::sync::RwLock; -use notify::{RecommendedWatcher, DebouncedEvent, Watcher, RecursiveMode}; -use std::sync::mpsc::channel; -use std::time::Duration; - -lazy_static::lazy_static! { - static ref SETTINGS: RwLock = RwLock::new({ - let mut settings = Config::default(); - settings.merge(File::with_name("Settings.toml")).unwrap(); - - settings - }); -} - -fn show() { - println!(" * Settings :: \n\x1b[31m{:?}\x1b[0m", - SETTINGS - .read() - .unwrap() - .clone() - .try_deserialize::>() - .unwrap()); -} - -fn watch() { - // Create a channel to receive the events. - let (tx, rx) = channel(); - - // Automatically select the best implementation for your platform. - // You can also access each implementation directly e.g. INotifyWatcher. - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2)).unwrap(); - - // Add a path to be watched. All files and directories at that path and - // below will be monitored for changes. - watcher - .watch("./Settings.toml", RecursiveMode::NonRecursive) - .unwrap(); - - // This is a simple loop, but you may want to use more complex logic here, - // for example to handle I/O. - loop { - match rx.recv() { - Ok(DebouncedEvent::Write(_)) => { - println!(" * Settings.toml written; refreshing configuration ..."); - SETTINGS.write().unwrap().refresh().unwrap(); - show(); - } - - Err(e) => println!("watch error: {:?}", e), - - _ => { - // Ignore event - } - } - } -} - -fn main() { - // This is just an example of what could be done, today - // We do want this to be built-in to config-rs at some point - // Feel free to take a crack at a PR - - show(); - watch(); -} -- cgit v1.2.3