Skip to content

Commit

Permalink
refactor: replace Config by Figment in Configuration implementation
Browse files Browse the repository at this point in the history
This replaces the crate `config` with `figment` in the Configuration
implementation.
  • Loading branch information
josecelano committed May 8, 2024
1 parent 002fb30 commit 265d89d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
13 changes: 7 additions & 6 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::{env, fs};

use config::ConfigError;
use derive_more::Constructor;
use thiserror::Error;
use torrust_tracker_located_error::{DynError, Located, LocatedError};
use torrust_tracker_located_error::{DynError, LocatedError};

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

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

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

impl From<ConfigError> for Error {
impl From<figment::Error> for Error {
#[track_caller]
fn from(err: ConfigError) -> Self {
fn from(err: figment::Error) -> Self {
Self::ConfigError {
source: Located(err).into(),
source: (Arc::new(err) as DynError).into(),
}
}
}
Expand Down
28 changes: 8 additions & 20 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ use std::fs;
use std::net::IpAddr;
use std::str::FromStr;

use config::{Config, File, FileFormat};
use figment::providers::{Format, Toml};
use figment::Figment;
use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};

Expand Down Expand Up @@ -398,18 +399,11 @@ impl Configuration {
///
/// Will return `Err` if `path` does not exist or has a bad configuration.
pub fn load_from_file(path: &str) -> Result<Configuration, Error> {
// todo: use Figment
let figment = Figment::new().merge(Toml::file(path));

let config_builder = Config::builder();
let config: Configuration = figment.extract()?;

#[allow(unused_assignments)]
let mut config = Config::default();

config = config_builder.add_source(File::with_name(path)).build()?;

let torrust_config: Configuration = config.try_deserialize()?;

Ok(torrust_config)
Ok(config)
}

/// Saves the default configuration at the given path.
Expand All @@ -419,8 +413,6 @@ impl Configuration {
/// Will return `Err` if `path` is not a valid path or the configuration
/// file cannot be created.
pub fn create_default_configuration_file(path: &str) -> Result<Configuration, Error> {
// todo: use Figment

let config = Configuration::default();
config.save_to_file(path)?;
Ok(config)
Expand All @@ -435,12 +427,9 @@ impl Configuration {
///
/// Will return `Err` if the environment variable does not exist or has a bad configuration.
pub fn load(info: &Info) -> Result<Configuration, Error> {
// todo: use Figment
let figment = Figment::new().merge(Toml::string(&info.tracker_toml));

let config_builder = Config::builder()
.add_source(File::from_str(&info.tracker_toml, FileFormat::Toml))
.build()?;
let mut config: Configuration = config_builder.try_deserialize()?;
let mut config: Configuration = figment.extract()?;

if let Some(ref token) = info.api_admin_token {
config.override_api_admin_token(token);
Expand All @@ -461,14 +450,13 @@ impl Configuration {
///
/// Will panic if the configuration cannot be written into the file.
pub fn save_to_file(&self, path: &str) -> Result<(), Error> {
// todo: use Figment

fs::write(path, self.to_toml()).expect("Could not write to file!");
Ok(())
}

/// Encodes the configuration to TOML.
fn to_toml(&self) -> String {
// code-review: do we need to use Figment also to serialize into toml?
toml::to_string(self).expect("Could not encode TOML value")
}
}
Expand Down

0 comments on commit 265d89d

Please sign in to comment.