diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-28 18:57:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-28 18:57:50 +0100 |
commit | 1565ca6ccf4db827c2ab56417916d977c9c4959d (patch) | |
tree | 2eee2d160dcc9476448cd5a9721fe25113d7c97f | |
parent | 66a7fa9de8f33cb76a9ad6fe94cf64fcc82360da (diff) | |
parent | cfdd11ddb28f0d46d56f4dc23f3581b7bd468442 (diff) |
Merge pull request #253 from conradludgate/fix-examples
make cargo recognise examples
-rw-r--r-- | Cargo.toml | 5 | ||||
-rw-r--r-- | examples/custom_format/main.rs | 4 | ||||
-rw-r--r-- | examples/glob/Cargo.toml | 8 | ||||
-rw-r--r-- | examples/glob/main.rs | 68 | ||||
-rw-r--r-- | examples/glob/src/main.rs | 49 | ||||
-rw-r--r-- | examples/global/Cargo.toml | 8 | ||||
-rw-r--r-- | examples/global/main.rs | 26 | ||||
-rw-r--r-- | examples/global/src/main.rs | 26 | ||||
-rw-r--r-- | examples/hierarchical-env/Cargo.toml | 9 | ||||
-rw-r--r-- | examples/hierarchical-env/main.rs (renamed from examples/hierarchical-env/src/main.rs) | 0 | ||||
-rw-r--r-- | examples/hierarchical-env/settings.rs (renamed from examples/hierarchical-env/src/settings.rs) | 10 | ||||
-rw-r--r-- | examples/simple/Cargo.toml | 7 | ||||
-rw-r--r-- | examples/simple/main.rs (renamed from examples/simple/src/main.rs) | 14 | ||||
-rw-r--r-- | examples/watch/Cargo.toml | 9 | ||||
-rw-r--r-- | examples/watch/main.rs (renamed from examples/watch/src/main.rs) | 24 |
15 files changed, 130 insertions, 137 deletions
@@ -45,3 +45,8 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util warp = "=0.3.1" futures = "0.3.15" reqwest = "=0.11.3" # version is forced to allow examples compile on rust 1.46, remove "=" as soon as possible + +serde = "1.0" +glob = "0.3" +lazy_static = "1" +notify = "^4.0.0" diff --git a/examples/custom_format/main.rs b/examples/custom_format/main.rs index eb62e45..4da2e9d 100644 --- a/examples/custom_format/main.rs +++ b/examples/custom_format/main.rs @@ -21,7 +21,7 @@ impl Format for MyFormat { uri: Option<&String>, text: &str, ) -> Result<Map<String, config::Value>, Box<dyn std::error::Error + Send + Sync>> { - // Let's assume our format is somewhat crippled, but this is fine + // Let's assume our format is somewhat malformed, but this is fine // In real life anything can be used here - nom, serde or other. // // For some more real-life examples refer to format implementation within the library code @@ -40,7 +40,7 @@ impl Format for MyFormat { } } -// As crazy as it seems for config sourced from a string, legacy demands its sacrifice +// As strange as it seems for config sourced from a string, legacy demands its sacrifice // It is only required for File source, custom sources can use Format without caring for extensions static MY_FORMAT_EXT: Vec<&'static str> = vec![]; impl FileStoredFormat for MyFormat { 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..f216543 --- /dev/null +++ b/examples/glob/main.rs @@ -0,0 +1,68 @@ +use config::*; +use glob::glob; +use std::collections::HashMap; +use std::path::Path; + +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("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"))) + .unwrap(); + + // Print out our settings (as a HashMap) + println!( + "\n{:?} \n\n-----------", + settings + .try_deserialize::<HashMap<String, String>>() + .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("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")), + ]) + .unwrap(); + + // Print out our settings (as a HashMap) + println!( + "\n{:?} \n\n-----------", + settings + .try_deserialize::<HashMap<String, String>>() + .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("examples/glob/conf/*") + .unwrap() + .map(|path| File::from(path.unwrap())) + .collect::<Vec<_>>(), + ) + .unwrap(); + + // Print out our settings (as a HashMap) + println!( + "\n{:?} \n\n-----------", + settings + .try_deserialize::<HashMap<String, String>>() + .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::<HashMap<String, String>>().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::<HashMap<String, String>>().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::<Vec<_>>()) - .unwrap(); - - // Print out our settings (as a HashMap) - println!("\n{:?} \n\n-----------", - settings.try_deserialize::<HashMap<String, String>>().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..3717d42 --- /dev/null +++ b/examples/global/main.rs @@ -0,0 +1,26 @@ +#[macro_use] +extern crate lazy_static; + +extern crate config; + +use config::Config; +use std::error::Error; +use std::sync::RwLock; + +lazy_static! { + static ref SETTINGS: RwLock<Config> = RwLock::new(Config::default()); +} + +fn try_main() -> Result<(), Box<dyn Error>> { + // Set property + SETTINGS.write()?.set("property", 42)?; + + // Get property + println!("property: {}", SETTINGS.read()?.get::<i32>("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<Config> = RwLock::new(Config::default()); -} - -fn try_main() -> Result<(), Box<dyn Error>> { - // Set property - SETTINGS.write()?.set("property", 42)?; - - // Get property - println!("property: {}", SETTINGS.read()?.get::<i32>("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/src/main.rs b/examples/hierarchical-env/main.rs index 65f727d..65f727d 100644 --- a/examples/hierarchical-env/src/main.rs +++ b/examples/hierarchical-env/main.rs diff --git a/examples/hierarchical-env/src/settings.rs b/examples/hierarchical-env/settings.rs index 7ef58b8..7801380 100644 --- a/examples/hierarchical-env/src/settings.rs +++ b/examples/hierarchical-env/settings.rs @@ -1,5 +1,5 @@ +use config::{Config, ConfigError, Environment, File}; use std::env; -use config::{ConfigError, Config, File, Environment}; #[derive(Debug, Deserialize)] struct Database { @@ -41,17 +41,19 @@ impl Settings { let mut s = Config::default(); // Start off by merging in the "default" configuration file - s.merge(File::with_name("config/default"))?; + 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!("config/{}", env)).required(false))?; + 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("config/local").required(false))?; + 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 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/src/main.rs b/examples/simple/main.rs index b38dcbf..5aa8818 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/main.rs @@ -4,12 +4,18 @@ fn main() { let mut settings = config::Config::default(); settings // Add in `./Settings.toml` - .merge(config::File::with_name("Settings")).unwrap() + .merge(config::File::with_name("examples/simple/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!("{:?}", - settings.try_deserialize::<HashMap<String, String>>().unwrap()); + println!( + "{:?}", + settings + .try_deserialize::<HashMap<String, String>>() + .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/src/main.rs b/examples/watch/main.rs index 9f3d21a..a86dd42 100644 --- a/examples/watch/src/main.rs +++ b/examples/watch/main.rs @@ -1,27 +1,29 @@ use config::*; +use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher}; use std::collections::HashMap; -use std::sync::RwLock; -use notify::{RecommendedWatcher, DebouncedEvent, Watcher, RecursiveMode}; use std::sync::mpsc::channel; +use std::sync::RwLock; use std::time::Duration; lazy_static::lazy_static! { static ref SETTINGS: RwLock<Config> = RwLock::new({ let mut settings = Config::default(); - settings.merge(File::with_name("Settings.toml")).unwrap(); + settings.merge(File::with_name("examples/watch/Settings.toml")).unwrap(); settings }); } fn show() { - println!(" * Settings :: \n\x1b[31m{:?}\x1b[0m", - SETTINGS - .read() - .unwrap() - .clone() - .try_deserialize::<HashMap<String, String>>() - .unwrap()); + println!( + " * Settings :: \n\x1b[31m{:?}\x1b[0m", + SETTINGS + .read() + .unwrap() + .clone() + .try_deserialize::<HashMap<String, String>>() + .unwrap() + ); } fn watch() { @@ -35,7 +37,7 @@ fn watch() { // 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) + .watch("examples/watch/Settings.toml", RecursiveMode::NonRecursive) .unwrap(); // This is a simple loop, but you may want to use more complex logic here, |