Skip to content

Commit 287e484

Browse files
committed
feat!: [#958] improve metadata in config files
The metadata section in the configuration file is changed: TOML: ```toml [metadata] app = "torrust-tracker" purpose = "configuration" schema_version = "2.0.0" ``` JSON: ```json { "metadata": { "app": "torrust-tracker", "purpose": "configuration", "version": "2.0.0" } } ``` - `app`: the applications this config file is used for. - `purpose`: the purpose of the file containing these metadata. - `schema_version`: the schema version for the file being parsed.
1 parent d47ff21 commit 287e484

17 files changed

Lines changed: 93 additions & 43 deletions

packages/configuration/src/lib.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! Torrust Tracker, which is a `BitTorrent` tracker server.
55
//!
66
//! The current version for configuration is [`v2`].
7-
pub mod v2;
7+
pub mod v2_0_0;
88
pub mod validator;
99

1010
use std::collections::HashMap;
@@ -35,67 +35,100 @@ const ENV_VAR_CONFIG_TOML: &str = "TORRUST_TRACKER_CONFIG_TOML";
3535
/// The `tracker.toml` file location.
3636
pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_TRACKER_CONFIG_TOML_PATH";
3737

38-
pub type Configuration = v2::Configuration;
39-
pub type Core = v2::core::Core;
40-
pub type HealthCheckApi = v2::health_check_api::HealthCheckApi;
41-
pub type HttpApi = v2::tracker_api::HttpApi;
42-
pub type HttpTracker = v2::http_tracker::HttpTracker;
43-
pub type UdpTracker = v2::udp_tracker::UdpTracker;
44-
pub type Database = v2::database::Database;
45-
pub type Driver = v2::database::Driver;
46-
pub type Threshold = v2::logging::Threshold;
38+
pub type Configuration = v2_0_0::Configuration;
39+
pub type Core = v2_0_0::core::Core;
40+
pub type HealthCheckApi = v2_0_0::health_check_api::HealthCheckApi;
41+
pub type HttpApi = v2_0_0::tracker_api::HttpApi;
42+
pub type HttpTracker = v2_0_0::http_tracker::HttpTracker;
43+
pub type UdpTracker = v2_0_0::udp_tracker::UdpTracker;
44+
pub type Database = v2_0_0::database::Database;
45+
pub type Driver = v2_0_0::database::Driver;
46+
pub type Threshold = v2_0_0::logging::Threshold;
4747

4848
pub type AccessTokens = HashMap<String, String>;
4949

50-
pub const LATEST_VERSION: &str = "2";
50+
pub const LATEST_VERSION: &str = "2.0.0";
5151

5252
/// Info about the configuration specification.
5353
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
54+
#[display(fmt = "Metadata(app: {app}, purpose: {purpose}, schema_version: {schema_version})")]
5455
pub struct Metadata {
55-
#[serde(default = "Metadata::default_version")]
56+
/// The application this configuration is valid for.
57+
#[serde(default = "Metadata::default_app")]
58+
app: App,
59+
60+
/// The purpose of this parsed file.
61+
#[serde(default = "Metadata::default_purpose")]
62+
purpose: Purpose,
63+
64+
/// The schema version for the configuration.
65+
#[serde(default = "Metadata::default_schema_version")]
5666
#[serde(flatten)]
57-
version: Version,
67+
schema_version: Version,
5868
}
5969

6070
impl Default for Metadata {
6171
fn default() -> Self {
6272
Self {
63-
version: Self::default_version(),
73+
app: Self::default_app(),
74+
purpose: Self::default_purpose(),
75+
schema_version: Self::default_schema_version(),
6476
}
6577
}
6678
}
6779

6880
impl Metadata {
69-
fn default_version() -> Version {
81+
fn default_app() -> App {
82+
App::TorrustTracker
83+
}
84+
85+
fn default_purpose() -> Purpose {
86+
Purpose::Configuration
87+
}
88+
89+
fn default_schema_version() -> Version {
7090
Version::latest()
7191
}
7292
}
7393

94+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
95+
#[serde(rename_all = "kebab-case")]
96+
pub enum App {
97+
TorrustTracker,
98+
}
99+
100+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
101+
#[serde(rename_all = "lowercase")]
102+
pub enum Purpose {
103+
Configuration,
104+
}
105+
74106
/// The configuration version.
75107
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
108+
#[serde(rename_all = "lowercase")]
76109
pub struct Version {
77110
#[serde(default = "Version::default_semver")]
78-
version: String,
111+
schema_version: String,
79112
}
80113

81114
impl Default for Version {
82115
fn default() -> Self {
83116
Self {
84-
version: Self::default_semver(),
117+
schema_version: Self::default_semver(),
85118
}
86119
}
87120
}
88121

89122
impl Version {
90123
fn new(semver: &str) -> Self {
91124
Self {
92-
version: semver.to_owned(),
125+
schema_version: semver.to_owned(),
93126
}
94127
}
95128

96129
fn latest() -> Self {
97130
Self {
98-
version: LATEST_VERSION.to_string(),
131+
schema_version: LATEST_VERSION.to_string(),
99132
}
100133
}
101134

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use derive_more::{Constructor, Display};
22
use serde::{Deserialize, Serialize};
33

44
use super::network::Network;
5-
use crate::v2::database::Database;
5+
use crate::v2_0_0::database::Database;
66
use crate::validator::{SemanticValidationError, Validator};
77
use crate::{AnnouncePolicy, TrackerPolicy};
88

99
#[allow(clippy::struct_excessive_bools)]
1010
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
1111
pub struct Core {
12-
// Announce policy configuration.
12+
/// Announce policy configuration.
1313
#[serde(default = "Core::default_announce_policy")]
1414
pub announce_policy: AnnouncePolicy,
1515

16-
// Database configuration.
16+
/// Database configuration.
1717
#[serde(default = "Core::default_database")]
1818
pub database: Database,
1919

@@ -22,23 +22,23 @@ pub struct Core {
2222
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
2323
pub inactive_peer_cleanup_interval: u64,
2424

25-
// When `true` only approved torrents can be announced in the tracker.
25+
/// When `true` only approved torrents can be announced in the tracker.
2626
#[serde(default = "Core::default_listed")]
2727
pub listed: bool,
2828

29-
// Network configuration.
29+
/// Network configuration.
3030
#[serde(default = "Core::default_network")]
3131
pub net: Network,
3232

33-
// When `true` clients require a key to connect and use the tracker.
33+
/// When `true` clients require a key to connect and use the tracker.
3434
#[serde(default = "Core::default_private")]
3535
pub private: bool,
3636

37-
// Configuration specific when the tracker is running in private mode.
37+
/// Configuration specific when the tracker is running in private mode.
3838
#[serde(default = "Core::default_private_mode")]
3939
pub private_mode: Option<PrivateMode>,
4040

41-
// Tracker policy configuration.
41+
/// Tracker policy configuration.
4242
#[serde(default = "Core::default_tracker_policy")]
4343
pub tracker_policy: TrackerPolicy,
4444

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ use crate::validator::{SemanticValidationError, Validator};
255255
use crate::{Error, Info, Metadata, Version};
256256

257257
/// This configuration version
258-
const VERSION_2: &str = "2";
258+
const VERSION_2_0_0: &str = "2.0.0";
259259

260260
/// Prefix for env vars that overwrite configuration options.
261261
const CONFIG_OVERRIDE_PREFIX: &str = "TORRUST_TRACKER_CONFIG_OVERRIDE_";
@@ -267,7 +267,6 @@ const CONFIG_OVERRIDE_SEPARATOR: &str = "__";
267267
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default, Clone)]
268268
pub struct Configuration {
269269
/// Configuration metadata.
270-
#[serde(flatten)]
271270
pub metadata: Metadata,
272271

273272
/// Logging configuration
@@ -335,9 +334,9 @@ impl Configuration {
335334

336335
let config: Configuration = figment.extract()?;
337336

338-
if config.metadata.version != Version::new(VERSION_2) {
337+
if config.metadata.schema_version != Version::new(VERSION_2_0_0) {
339338
return Err(Error::UnsupportedVersion {
340-
version: config.metadata.version,
339+
version: config.metadata.schema_version,
341340
});
342341
}
343342

@@ -406,12 +405,15 @@ mod tests {
406405

407406
use std::net::{IpAddr, Ipv4Addr};
408407

409-
use crate::v2::Configuration;
408+
use crate::v2_0_0::Configuration;
410409
use crate::Info;
411410

412411
#[cfg(test)]
413412
fn default_config_toml() -> String {
414-
let config = r#"version = "2"
413+
let config = r#"[metadata]
414+
app = "torrust-tracker"
415+
purpose = "configuration"
416+
schema_version = "2.0.0"
415417
416418
[logging]
417419
threshold = "info"
File renamed without changes.

packages/configuration/src/v2/tracker_api.rs renamed to packages/configuration/src/v2_0_0/tracker_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl HttpApi {
7171

7272
#[cfg(test)]
7373
mod tests {
74-
use crate::v2::tracker_api::HttpApi;
74+
use crate::v2_0_0::tracker_api::HttpApi;
7575

7676
#[test]
7777
fn http_api_configuration_should_check_if_it_contains_a_token() {
File renamed without changes.

0 commit comments

Comments
 (0)