@@ -239,7 +239,8 @@ use std::fs;
239239use std:: net:: IpAddr ;
240240use std:: str:: FromStr ;
241241
242- use config:: { Config , File , FileFormat } ;
242+ use figment:: providers:: { Format , Toml } ;
243+ use figment:: Figment ;
243244use serde:: { Deserialize , Serialize } ;
244245use torrust_tracker_primitives:: { DatabaseDriver , TrackerMode } ;
245246
@@ -398,18 +399,11 @@ impl Configuration {
398399 ///
399400 /// Will return `Err` if `path` does not exist or has a bad configuration.
400401 pub fn load_from_file ( path : & str ) -> Result < Configuration , Error > {
401- // todo: use Figment
402+ let figment = Figment :: new ( ) . merge ( Toml :: file ( path ) ) ;
402403
403- let config_builder = Config :: builder ( ) ;
404+ let config : Configuration = figment . extract ( ) ? ;
404405
405- #[ allow( unused_assignments) ]
406- let mut config = Config :: default ( ) ;
407-
408- config = config_builder. add_source ( File :: with_name ( path) ) . build ( ) ?;
409-
410- let torrust_config: Configuration = config. try_deserialize ( ) ?;
411-
412- Ok ( torrust_config)
406+ Ok ( config)
413407 }
414408
415409 /// Saves the default configuration at the given path.
@@ -419,8 +413,6 @@ impl Configuration {
419413 /// Will return `Err` if `path` is not a valid path or the configuration
420414 /// file cannot be created.
421415 pub fn create_default_configuration_file ( path : & str ) -> Result < Configuration , Error > {
422- // todo: use Figment
423-
424416 let config = Configuration :: default ( ) ;
425417 config. save_to_file ( path) ?;
426418 Ok ( config)
@@ -435,12 +427,9 @@ impl Configuration {
435427 ///
436428 /// Will return `Err` if the environment variable does not exist or has a bad configuration.
437429 pub fn load ( info : & Info ) -> Result < Configuration , Error > {
438- // todo: use Figment
430+ let figment = Figment :: new ( ) . merge ( Toml :: string ( & info . tracker_toml ) ) ;
439431
440- let config_builder = Config :: builder ( )
441- . add_source ( File :: from_str ( & info. tracker_toml , FileFormat :: Toml ) )
442- . build ( ) ?;
443- let mut config: Configuration = config_builder. try_deserialize ( ) ?;
432+ let mut config: Configuration = figment. extract ( ) ?;
444433
445434 if let Some ( ref token) = info. api_admin_token {
446435 config. override_api_admin_token ( token) ;
@@ -461,14 +450,13 @@ impl Configuration {
461450 ///
462451 /// Will panic if the configuration cannot be written into the file.
463452 pub fn save_to_file ( & self , path : & str ) -> Result < ( ) , Error > {
464- // todo: use Figment
465-
466453 fs:: write ( path, self . to_toml ( ) ) . expect ( "Could not write to file!" ) ;
467454 Ok ( ( ) )
468455 }
469456
470457 /// Encodes the configuration to TOML.
471458 fn to_toml ( & self ) -> String {
459+ // code-review: do we need to use Figment also to serialize into toml?
472460 toml:: to_string ( self ) . expect ( "Could not encode TOML value" )
473461 }
474462}
0 commit comments