diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2022-11-28 17:41:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 17:41:31 +0100 |
commit | cea4debca004cee5227be5345db570a6c6b4a944 (patch) | |
tree | e2952f626b2da2fdb3b8f98a947f57107be73e6d | |
parent | f4c1193ea1f93c7984d23a1366efe41d66fa732d (diff) | |
parent | fec6a4ade33301395c16dcb13ac1b34d0e411e3c (diff) |
Merge pull request #392 from matthiasbeyer/add-static-env-example
Add simple example using lazy_static
-rw-r--r-- | examples/static_env.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/examples/static_env.rs b/examples/static_env.rs new file mode 100644 index 0000000..ba3222f --- /dev/null +++ b/examples/static_env.rs @@ -0,0 +1,20 @@ +use config::Config; + +lazy_static::lazy_static! { + #[derive(Debug)] + pub static ref CONFIG: Config = Config::builder() + .add_source(config::Environment::with_prefix("APP_NAME").separator("_")) + .build() + .unwrap(); +} + +/// Get a configuration value from the static configuration object +pub fn get<'a, T: serde::Deserialize<'a>>(key: &str) -> T { + // You shouldn't probably do it like that and actually handle that error that might happen + // here, but for the sake of simplicity, we do it like this here + CONFIG.get::<T>(key).unwrap() +} + +fn main() { + println!("{:?}", get::<String>("foo")); +} |