From fec6a4ade33301395c16dcb13ac1b34d0e411e3c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 7 Nov 2022 16:39:47 +0100 Subject: Add simple example using lazy_static Signed-off-by: Matthias Beyer --- examples/static_env.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 examples/static_env.rs 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::(key).unwrap() +} + +fn main() { + println!("{:?}", get::("foo")); +} -- cgit v1.2.3