@@ -76,7 +76,7 @@ impl Config {
7676 let mut config_content = std:: fs:: read_to_string ( filename)
7777 . map_err ( |e| format ! ( "Failed to read config file: {}" , e) ) ?;
7878
79- config_content = Self :: substitute_env_vars ( config_content) ? ;
79+ config_content = Self :: substitute_env_vars ( config_content) ;
8080
8181 let config: ConfigWrapper = Figment :: new ( )
8282 . merge ( Toml :: string ( config_defaults) )
@@ -89,8 +89,10 @@ impl Config {
8989 Ok ( config. 0 )
9090 }
9191
92- fn substitute_env_vars ( content : String ) -> Result < String , String > {
93- let re = Regex :: new ( r"\$\{([A-Z_][A-Z0-9_]*)\}" ) . map_err ( |e| e. to_string ( ) ) ?;
92+ fn substitute_env_vars ( content : String ) -> String {
93+ let re = Regex :: new ( r"\$\{([A-Z_][A-Z0-9_]*)\}" )
94+ . map_err ( |e| e. to_string ( ) )
95+ . expect ( "error substituting values" ) ;
9496
9597 let result = re. replace_all ( & content, |caps : & regex:: Captures | {
9698 let var_name = & caps[ 1 ] ;
@@ -100,7 +102,7 @@ impl Config {
100102 Err ( _) => format ! ( "${{{}}}" , var_name) ,
101103 }
102104 } ) ;
103- Ok ( result. into_owned ( ) )
105+ result. into_owned ( )
104106 }
105107
106108 // custom validation of the values
@@ -424,31 +426,33 @@ mod tests {
424426 #[ test]
425427 fn test_substitute_env_vars ( ) {
426428 // Set up environment variables
427- env:: set_var ( "TEST_VAR1" , "hello" ) ;
428- env:: set_var ( "TEST_VAR2" , "world" ) ;
429- // TEST_VAR3 not setup and should not be replaced by the function
429+ env:: set_var ( "TEST_VAR1" , "changed_value_1" ) ;
430430
431431 let input = r#"
432432 [section1]
433433 key1 = "${TEST_VAR1}"
434- key2 = "${TEST_VAR2}"
435- key3 = "${TEST_VAR3}"
436- key4 = "prefix_${TEST_VAR1}_${TEST_VAR2}_suffix"
434+ key2 = "${TEST_VAR-default}"
435+ key3 = "{{TEST_VAR3}}"
436+
437+ [section2]
438+ key4 = "prefix_${TEST_VAR1}_${TEST_VAR-default}_suffix"
437439 key5 = "a_key_without_substitution"
438440 "#
439441 . to_string ( ) ;
440442
441443 let expected_output = r#"
442444 [section1]
443- key1 = "hello"
444- key2 = "world"
445- key3 = "${TEST_VAR3}"
446- key4 = "prefix_hello_world_suffix"
445+ key1 = "changed_value_1"
446+ key2 = "${TEST_VAR-default}"
447+ key3 = "{{TEST_VAR3}}"
448+
449+ [section2]
450+ key4 = "prefix_changed_value_1_${TEST_VAR-default}_suffix"
447451 key5 = "a_key_without_substitution"
448452 "#
449453 . to_string ( ) ;
450454
451- let result = Config :: substitute_env_vars ( input) . expect ( "Failed to substitute env vars " ) ;
455+ let result = Config :: substitute_env_vars ( input) ;
452456
453457 assert_eq ! (
454458 result, expected_output,
@@ -457,6 +461,5 @@ mod tests {
457461
458462 // Clean up environment variables
459463 env:: remove_var ( "TEST_VAR1" ) ;
460- env:: remove_var ( "TEST_VAR2" ) ;
461464 }
462465}
0 commit comments