Skip to content

Commit 7519ecc

Browse files
committed
refactor: [torrust#852] enrich field types in Configuration struct
1 parent ceb3074 commit 7519ecc

File tree

7 files changed

+46
-27
lines changed

7 files changed

+46
-27
lines changed

packages/configuration/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,20 @@ pub struct TslConfig {
171171
#[serde_as(as = "NoneAsEmptyString")]
172172
pub ssl_key_path: Option<Utf8PathBuf>,
173173
}
174+
175+
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
176+
#[serde(rename_all = "lowercase")]
177+
pub enum LogLevel {
178+
/// A level lower than all log levels.
179+
Off,
180+
/// Corresponds to the `Error` log level.
181+
Error,
182+
/// Corresponds to the `Warn` log level.
183+
Warn,
184+
/// Corresponds to the `Info` log level.
185+
Info,
186+
/// Corresponds to the `Debug` log level.
187+
Debug,
188+
/// Corresponds to the `Trace` log level.
189+
Trace,
190+
}

packages/configuration/src/v1/mod.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ pub mod tracker_api;
236236
pub mod udp_tracker;
237237

238238
use std::fs;
239-
use std::net::IpAddr;
240-
use std::str::FromStr;
239+
use std::net::{IpAddr, Ipv4Addr};
241240

242241
use figment::providers::{Env, Format, Serialized, Toml};
243242
use figment::Figment;
@@ -248,15 +247,15 @@ use self::health_check_api::HealthCheckApi;
248247
use self::http_tracker::HttpTracker;
249248
use self::tracker_api::HttpApi;
250249
use self::udp_tracker::UdpTracker;
251-
use crate::{AnnouncePolicy, Error, Info};
250+
use crate::{AnnouncePolicy, Error, Info, LogLevel};
252251

253252
/// Core configuration for the tracker.
254253
#[allow(clippy::struct_excessive_bools)]
255254
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
256255
pub struct Configuration {
257256
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
258257
/// `Debug` and `Trace`. Default is `Info`.
259-
pub log_level: Option<String>,
258+
pub log_level: Option<LogLevel>,
260259
/// Tracker mode. See [`TrackerMode`] for more information.
261260
pub mode: TrackerMode,
262261

@@ -284,7 +283,7 @@ pub struct Configuration {
284283
/// is using a loopback IP address, the tracker assumes that the peer is
285284
/// in the same network as the tracker and will use the tracker's IP
286285
/// address instead.
287-
pub external_ip: Option<String>,
286+
pub external_ip: Option<IpAddr>,
288287
/// Weather the tracker should collect statistics about tracker usage.
289288
/// If enabled, the tracker will collect statistics like the number of
290289
/// connections handled, the number of announce requests handled, etc.
@@ -330,15 +329,15 @@ impl Default for Configuration {
330329
let announce_policy = AnnouncePolicy::default();
331330

332331
let mut configuration = Configuration {
333-
log_level: Option::from(String::from("info")),
332+
log_level: Some(LogLevel::Info),
334333
mode: TrackerMode::Public,
335334
db_driver: DatabaseDriver::Sqlite3,
336335
db_path: String::from("./storage/tracker/lib/database/sqlite3.db"),
337336
announce_interval: announce_policy.interval,
338337
min_announce_interval: announce_policy.interval_min,
339338
max_peer_timeout: 900,
340339
on_reverse_proxy: false,
341-
external_ip: Some(String::from("0.0.0.0")),
340+
external_ip: Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))),
342341
tracker_usage_statistics: true,
343342
persistent_torrent_completed_stat: false,
344343
inactive_peer_cleanup_interval: 600,
@@ -363,13 +362,7 @@ impl Configuration {
363362
/// and `None` otherwise.
364363
#[must_use]
365364
pub fn get_ext_ip(&self) -> Option<IpAddr> {
366-
match &self.external_ip {
367-
None => None,
368-
Some(external_ip) => match IpAddr::from_str(external_ip) {
369-
Ok(external_ip) => Some(external_ip),
370-
Err(_) => None,
371-
},
372-
}
365+
self.external_ip.as_ref().map(|external_ip| *external_ip)
373366
}
374367

375368
/// Saves the default configuration at the given path.
@@ -432,6 +425,8 @@ impl Configuration {
432425
#[cfg(test)]
433426
mod tests {
434427

428+
use std::net::{IpAddr, Ipv4Addr};
429+
435430
use crate::v1::Configuration;
436431
use crate::Info;
437432

@@ -495,7 +490,7 @@ mod tests {
495490
fn configuration_should_contain_the_external_ip() {
496491
let configuration = Configuration::default();
497492

498-
assert_eq!(configuration.external_ip, Some(String::from("0.0.0.0")));
493+
assert_eq!(configuration.external_ip, Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
499494
}
500495

501496
#[test]

packages/test-helpers/src/configuration.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::env;
33
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
44

5-
use torrust_tracker_configuration::Configuration;
5+
use torrust_tracker_configuration::{Configuration, LogLevel};
66
use torrust_tracker_primitives::TrackerMode;
77

88
use crate::random;
@@ -28,7 +28,7 @@ pub fn ephemeral() -> Configuration {
2828
// For example: a test for the UDP tracker should disable the API and HTTP tracker.
2929

3030
let mut config = Configuration {
31-
log_level: Some("off".to_owned()), // Change to `debug` for tests debugging
31+
log_level: Some(LogLevel::Off), // Change to `debug` for tests debugging
3232
..Default::default()
3333
};
3434

@@ -125,7 +125,7 @@ pub fn ephemeral_mode_private_whitelisted() -> Configuration {
125125
pub fn ephemeral_with_external_ip(ip: IpAddr) -> Configuration {
126126
let mut cfg = ephemeral();
127127

128-
cfg.external_ip = Some(ip.to_string());
128+
cfg.external_ip = Some(ip);
129129

130130
cfg
131131
}

src/bootstrap/logging.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
//! - `Trace`
1111
//!
1212
//! Refer to the [configuration crate documentation](https://docs.rs/torrust-tracker-configuration) to know how to change log settings.
13-
use std::str::FromStr;
1413
use std::sync::Once;
1514

1615
use log::{info, LevelFilter};
17-
use torrust_tracker_configuration::Configuration;
16+
use torrust_tracker_configuration::{Configuration, LogLevel};
1817

1918
static INIT: Once = Once::new();
2019

@@ -31,10 +30,17 @@ pub fn setup(cfg: &Configuration) {
3130
});
3231
}
3332

34-
fn config_level_or_default(log_level: &Option<String>) -> LevelFilter {
33+
fn config_level_or_default(log_level: &Option<LogLevel>) -> LevelFilter {
3534
match log_level {
3635
None => log::LevelFilter::Info,
37-
Some(level) => LevelFilter::from_str(level).unwrap(),
36+
Some(level) => match level {
37+
LogLevel::Off => LevelFilter::Off,
38+
LogLevel::Error => LevelFilter::Error,
39+
LogLevel::Warn => LevelFilter::Warn,
40+
LogLevel::Info => LevelFilter::Info,
41+
LogLevel::Debug => LevelFilter::Debug,
42+
LogLevel::Trace => LevelFilter::Trace,
43+
},
3844
}
3945
}
4046

src/servers/apis/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@
130130
//! > **NOTICE**: You can generate a self-signed certificate for localhost using
131131
//! OpenSSL. See [Let's Encrypt](https://letsencrypt.org/docs/certificates-for-localhost/).
132132
//! That's particularly useful for testing purposes. Once you have the certificate
133-
//! you need to set the [`ssl_cert_path`](torrust_tracker_configuration::HttpApi::ssl_cert_path)
134-
//! and [`ssl_key_path`](torrust_tracker_configuration::HttpApi::ssl_key_path)
133+
//! you need to set the [`ssl_cert_path`](torrust_tracker_configuration::HttpApi::tsl_config.ssl_cert_path)
134+
//! and [`ssl_key_path`](torrust_tracker_configuration::HttpApi::tsl_config.ssl_key_path)
135135
//! options in the configuration file with the paths to the certificate
136136
//! (`localhost.crt`) and key (`localhost.key`) files.
137137
//!

src/servers/http/v1/services/announce.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ mod tests {
145145

146146
fn tracker_with_an_ipv6_external_ip(stats_event_sender: Box<dyn statistics::EventSender>) -> Tracker {
147147
let mut configuration = configuration::ephemeral();
148-
configuration.external_ip =
149-
Some(IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)).to_string());
148+
configuration.external_ip = Some(IpAddr::V6(Ipv6Addr::new(
149+
0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969,
150+
)));
150151

151152
Tracker::new(&configuration, Some(stats_event_sender), statistics::Repo::new()).unwrap()
152153
}

src/servers/udp/handlers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ mod tests {
426426
}
427427

428428
pub fn with_external_ip(mut self, external_ip: &str) -> Self {
429-
self.configuration.external_ip = Some(external_ip.to_owned());
429+
self.configuration.external_ip = Some(external_ip.to_owned().parse().expect("valid IP address"));
430430
self
431431
}
432432

0 commit comments

Comments
 (0)