@@ -70,17 +70,19 @@ impl ConfigPrefix {
7070}
7171
7272impl Config {
73- pub fn parse ( prefix : ConfigPrefix , filename : & PathBuf ) -> Result < Self , String > {
73+ pub fn parse ( prefix : ConfigPrefix , filename : Option < & PathBuf > ) -> Result < Self , String > {
7474 let config_defaults = include_str ! ( "../default_values.toml" ) ;
7575
76- let mut config_content = std:: fs:: read_to_string ( filename)
77- . map_err ( |e| format ! ( "Failed to read config file: {}" , e) ) ?;
76+ let mut figment_config = Figment :: new ( ) . merge ( Toml :: string ( config_defaults) ) ;
7877
79- config_content = Self :: substitute_env_vars ( config_content) ?;
78+ if let Some ( path) = filename {
79+ let mut config_content = std:: fs:: read_to_string ( path)
80+ . map_err ( |e| format ! ( "Failed to read config file: {}" , e) ) ?;
81+ config_content = Self :: substitute_env_vars ( config_content) ?;
82+ figment_config = figment_config. merge ( Toml :: string ( & config_content) ) ;
83+ }
8084
81- let config: ConfigWrapper = Figment :: new ( )
82- . merge ( Toml :: string ( config_defaults) )
83- . merge ( Toml :: string ( & config_content) )
85+ let config: ConfigWrapper = figment_config
8486 . merge ( Env :: prefixed ( prefix. get_prefix ( ) ) . split ( "__" ) )
8587 . extract ( )
8688 . map_err ( |e| e. to_string ( ) ) ?;
@@ -386,7 +388,7 @@ mod tests {
386388 fn test_minimal_config ( ) {
387389 Config :: parse (
388390 ConfigPrefix :: Service ,
389- & PathBuf :: from ( "minimal-config-example.toml" ) ,
391+ Some ( PathBuf :: from ( "minimal-config-example.toml" ) ) . as_ref ( ) ,
390392 )
391393 . unwrap ( ) ;
392394 }
@@ -396,7 +398,7 @@ mod tests {
396398 // Generate full config by deserializing the minimal config and let the code fill in the defaults.
397399 let max_config = Config :: parse (
398400 ConfigPrefix :: Service ,
399- & PathBuf :: from ( "minimal-config-example.toml" ) ,
401+ Some ( PathBuf :: from ( "minimal-config-example.toml" ) ) . as_ref ( ) ,
400402 )
401403 . unwrap ( ) ;
402404 let max_config_file: Config = toml:: from_str (
@@ -418,7 +420,7 @@ mod tests {
418420
419421 Config :: parse (
420422 ConfigPrefix :: Service ,
421- & PathBuf :: from ( "minimal-config-example.toml" ) ,
423+ Some ( PathBuf :: from ( "minimal-config-example.toml" ) ) . as_ref ( ) ,
422424 )
423425 . unwrap ( ) ;
424426
@@ -458,7 +460,7 @@ mod tests {
458460 // This should fail because the subgraphs.network.query_url field is missing
459461 Config :: parse (
460462 ConfigPrefix :: Service ,
461- & PathBuf :: from ( temp_minimal_config_path. path ( ) ) ,
463+ Some ( PathBuf :: from ( temp_minimal_config_path. path ( ) ) ) . as_ref ( ) ,
462464 )
463465 . unwrap_err ( ) ;
464466
@@ -467,7 +469,7 @@ mod tests {
467469
468470 let config = Config :: parse (
469471 ConfigPrefix :: Service ,
470- & PathBuf :: from ( temp_minimal_config_path. path ( ) ) ,
472+ Some ( PathBuf :: from ( temp_minimal_config_path. path ( ) ) ) . as_ref ( ) ,
471473 )
472474 . unwrap ( ) ;
473475
@@ -485,7 +487,7 @@ mod tests {
485487
486488 let config = Config :: parse (
487489 ConfigPrefix :: Service ,
488- & PathBuf :: from ( "minimal-config-example.toml" ) ,
490+ Some ( PathBuf :: from ( "minimal-config-example.toml" ) ) . as_ref ( ) ,
489491 )
490492 . unwrap ( ) ;
491493
@@ -569,7 +571,7 @@ mod tests {
569571 // This should fail because the QUERY_URL env variable is not setup
570572 Config :: parse (
571573 ConfigPrefix :: Service ,
572- & PathBuf :: from ( temp_minimal_config_path. path ( ) ) ,
574+ Some ( PathBuf :: from ( temp_minimal_config_path. path ( ) ) ) . as_ref ( ) ,
573575 )
574576 . unwrap_err ( ) ;
575577
@@ -578,7 +580,7 @@ mod tests {
578580
579581 let config = Config :: parse (
580582 ConfigPrefix :: Service ,
581- & PathBuf :: from ( temp_minimal_config_path. path ( ) ) ,
583+ Some ( PathBuf :: from ( temp_minimal_config_path. path ( ) ) ) . as_ref ( ) ,
582584 )
583585 . unwrap ( ) ;
584586
@@ -663,7 +665,7 @@ mod tests {
663665 // Parse the config with new datbase vars
664666 let config = Config :: parse (
665667 ConfigPrefix :: Service ,
666- & PathBuf :: from ( temp_minimal_config_path. path ( ) ) ,
668+ Some ( PathBuf :: from ( temp_minimal_config_path. path ( ) ) ) . as_ref ( ) ,
667669 )
668670 . unwrap ( ) ;
669671
0 commit comments