Skip to content

Commit c7b1b10

Browse files
committed
docs(examples): Consoldate env examples
1 parent 5aa9638 commit c7b1b10

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

examples/env_list.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/static_env.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ use std::sync::OnceLock;
55
use config::Config;
66

77
fn main() {
8-
println!("{:?}", get::<String>("foo"));
8+
println!("APP_STRING={:?}", get::<String>("string"));
9+
println!("APP_INT={:?}", get::<i32>("int"));
10+
println!("APP_STRLIST={:?}", get::<Vec<i32>>("strlist"));
911
}
1012

1113
/// Get a configuration value from the environment
12-
pub fn get<'a, T: serde::Deserialize<'a>>(path: &str) -> T {
13-
// You shouldn't probably do it like that and actually handle that error that might happen
14-
// here, but for the sake of simplicity, we do it like this here
15-
config().get::<T>(path).unwrap()
14+
pub fn get<'a, T: serde::Deserialize<'a>>(path: &str) -> Option<T> {
15+
config().get::<T>(path).ok()
1616
}
1717

1818
fn config() -> &'static Config {
1919
static CONFIG: OnceLock<Config> = OnceLock::new();
2020
CONFIG.get_or_init(|| {
2121
Config::builder()
22-
.add_source(config::Environment::with_prefix("APP"))
22+
.add_source(
23+
config::Environment::with_prefix("APP")
24+
.try_parsing(true)
25+
.separator("_")
26+
.list_separator(","),
27+
)
2328
.build()
2429
.unwrap()
2530
})

0 commit comments

Comments
 (0)