File: static_env.rs

package info (click to toggle)
rust-config 0.15.9-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 584 kB
  • sloc: makefile: 2
file content (24 lines) | stat: -rw-r--r-- 715 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::sync::OnceLock;

use config::Config;

fn config() -> &'static Config {
    static CONFIG: OnceLock<Config> = OnceLock::new();
    CONFIG.get_or_init(|| {
        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"));
}