Skip to content

Commit 24d98b4

Browse files
committed
refactor: replace Config by Figment in Configuration implementation
This replaces the crate `config` with `figment` in the Configuration implementation.
1 parent 62ef6a7 commit 24d98b4

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

packages/configuration/src/lib.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ use std::collections::HashMap;
1010
use std::sync::Arc;
1111
use std::{env, fs};
1212

13-
use config::ConfigError;
1413
use derive_more::Constructor;
1514
use thiserror::Error;
16-
use torrust_tracker_located_error::{DynError, Located, LocatedError};
15+
use torrust_tracker_located_error::{DynError, LocatedError};
1716

1817
/// The maximum number of returned peers for a torrent.
1918
pub const TORRENT_PEERS_LIMIT: usize = 74;
@@ -142,17 +141,19 @@ pub enum Error {
142141

143142
/// Unable to load the configuration from the configuration file.
144143
#[error("Failed processing the configuration: {source}")]
145-
ConfigError { source: LocatedError<'static, ConfigError> },
144+
ConfigError {
145+
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
146+
},
146147

147148
#[error("The error for errors that can never happen.")]
148149
Infallible,
149150
}
150151

151-
impl From<ConfigError> for Error {
152+
impl From<figment::Error> for Error {
152153
#[track_caller]
153-
fn from(err: ConfigError) -> Self {
154+
fn from(err: figment::Error) -> Self {
154155
Self::ConfigError {
155-
source: Located(err).into(),
156+
source: (Arc::new(err) as DynError).into(),
156157
}
157158
}
158159
}

packages/configuration/src/v1/mod.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ use std::fs;
239239
use std::net::IpAddr;
240240
use std::str::FromStr;
241241

242-
use config::{Config, File, FileFormat};
242+
use figment::providers::{Format, Toml};
243+
use figment::Figment;
243244
use serde::{Deserialize, Serialize};
244245
use 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

Comments
 (0)