File tree Expand file tree Collapse file tree 2 files changed +11
-30
lines changed
Expand file tree Collapse file tree 2 files changed +11
-30
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -5,21 +5,26 @@ use std::sync::OnceLock;
55use config:: Config ;
66
77fn 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
1818fn 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 } )
You can’t perform that action at this time.
0 commit comments