@@ -239,7 +239,8 @@ use std::fs;
239
239
use std:: net:: IpAddr ;
240
240
use std:: str:: FromStr ;
241
241
242
- use config:: { Config , File , FileFormat } ;
242
+ use figment:: providers:: { Format , Toml } ;
243
+ use figment:: Figment ;
243
244
use serde:: { Deserialize , Serialize } ;
244
245
use torrust_tracker_primitives:: { DatabaseDriver , TrackerMode } ;
245
246
@@ -398,18 +399,11 @@ impl Configuration {
398
399
///
399
400
/// Will return `Err` if `path` does not exist or has a bad configuration.
400
401
pub fn load_from_file ( path : & str ) -> Result < Configuration , Error > {
401
- // todo: use Figment
402
+ let figment = Figment :: new ( ) . merge ( Toml :: file ( path ) ) ;
402
403
403
- let config_builder = Config :: builder ( ) ;
404
+ let config : Configuration = figment . extract ( ) ? ;
404
405
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)
413
407
}
414
408
415
409
/// Saves the default configuration at the given path.
@@ -419,8 +413,6 @@ impl Configuration {
419
413
/// Will return `Err` if `path` is not a valid path or the configuration
420
414
/// file cannot be created.
421
415
pub fn create_default_configuration_file ( path : & str ) -> Result < Configuration , Error > {
422
- // todo: use Figment
423
-
424
416
let config = Configuration :: default ( ) ;
425
417
config. save_to_file ( path) ?;
426
418
Ok ( config)
@@ -435,12 +427,9 @@ impl Configuration {
435
427
///
436
428
/// Will return `Err` if the environment variable does not exist or has a bad configuration.
437
429
pub fn load ( info : & Info ) -> Result < Configuration , Error > {
438
- // todo: use Figment
430
+ let figment = Figment :: new ( ) . merge ( Toml :: string ( & info . tracker_toml ) ) ;
439
431
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 ( ) ?;
444
433
445
434
if let Some ( ref token) = info. api_admin_token {
446
435
config. override_api_admin_token ( token) ;
@@ -461,14 +450,13 @@ impl Configuration {
461
450
///
462
451
/// Will panic if the configuration cannot be written into the file.
463
452
pub fn save_to_file ( & self , path : & str ) -> Result < ( ) , Error > {
464
- // todo: use Figment
465
-
466
453
fs:: write ( path, self . to_toml ( ) ) . expect ( "Could not write to file!" ) ;
467
454
Ok ( ( ) )
468
455
}
469
456
470
457
/// Encodes the configuration to TOML.
471
458
fn to_toml ( & self ) -> String {
459
+ // code-review: do we need to use Figment also to serialize into toml?
472
460
toml:: to_string ( self ) . expect ( "Could not encode TOML value" )
473
461
}
474
462
}
0 commit comments