Skip to content

Commit b92401f

Browse files
committed
Merge torrust#871: Add privacy methods to the TrackerMode enum
0c9da2f feat: [torrust#870] implement traits Dispaly and FromStr for TrackerMode (Jose Celano) 74d8f79 feat: [torrust#870] remove Copy trait from TrackerMode (Jose Celano) 932e66e feat: [torrust#870] add privacy methods to the TrackerMode (Jose Celano) Pull request description: The tracker mode can be: - Public (Non-whitelisted) - Listed (Whitelisted) - Private (Non-whitelisted) - PrivateListed (Whitelisted) There should have been two different flags (in my opinion): - Visibility: public or private - Whitelisted: true or false So we would have the same four combinations: - Not whitelisted: - Public - Private - Whitelisted - Public - Private That's a pending refactor. For this PR, the goal is just to align this enum with what we added to the Index so we can use it in the Index via the primitive crate. See https://github.com/torrust/torrust-index/blob/develop/src/config.rs#L140-L171 ACKs for top commit: josecelano: ACK 0c9da2f Tree-SHA512: 526cbd57d4d7e5ff5668c870dc2d24a27b60e73ac07da99dab1139337729e04dbce1704d348a64bd1466440640b8652f3ff20d847e933ed800463b2100ef261d
2 parents d4eaea9 + 0c9da2f commit b92401f

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

packages/primitives/src/lib.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! by the tracker server crate, but also by other crates in the Torrust
66
//! ecosystem.
77
use std::collections::BTreeMap;
8+
use std::fmt;
9+
use std::str::FromStr;
810
use std::time::Duration;
911

1012
use info_hash::InfoHash;
@@ -67,7 +69,7 @@ pub type PersistentTorrents = BTreeMap<InfoHash, u32>;
6769
///
6870
/// Refer to [Torrust Tracker Configuration](https://docs.rs/torrust-tracker-configuration)
6971
/// to know how to configure the tracker to run in each mode.
70-
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug)]
72+
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
7173
pub enum TrackerMode {
7274
/// Will track every new info hash and serve every peer.
7375
#[serde(rename = "public")]
@@ -85,3 +87,47 @@ pub enum TrackerMode {
8587
#[serde(rename = "private_listed")]
8688
PrivateListed,
8789
}
90+
91+
impl Default for TrackerMode {
92+
fn default() -> Self {
93+
Self::Public
94+
}
95+
}
96+
97+
impl fmt::Display for TrackerMode {
98+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99+
let display_str = match self {
100+
TrackerMode::Public => "public",
101+
TrackerMode::Listed => "listed",
102+
TrackerMode::Private => "private",
103+
TrackerMode::PrivateListed => "private_listed",
104+
};
105+
write!(f, "{display_str}")
106+
}
107+
}
108+
109+
impl FromStr for TrackerMode {
110+
type Err = String;
111+
112+
fn from_str(s: &str) -> Result<Self, Self::Err> {
113+
match s.to_lowercase().as_str() {
114+
"public" => Ok(TrackerMode::Public),
115+
"listed" => Ok(TrackerMode::Listed),
116+
"private" => Ok(TrackerMode::Private),
117+
"private_listed" => Ok(TrackerMode::PrivateListed),
118+
_ => Err(format!("Unknown tracker mode: {s}")),
119+
}
120+
}
121+
}
122+
123+
impl TrackerMode {
124+
#[must_use]
125+
pub fn is_open(&self) -> bool {
126+
matches!(self, TrackerMode::Public | TrackerMode::Listed)
127+
}
128+
129+
#[must_use]
130+
pub fn is_close(&self) -> bool {
131+
!self.is_open()
132+
}
133+
}

src/core/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ impl Tracker {
547547
) -> Result<Tracker, databases::error::Error> {
548548
let database = Arc::new(databases::driver::build(&config.db_driver, &config.db_path)?);
549549

550-
let mode = config.mode;
550+
let mode = config.mode.clone();
551551

552552
Ok(Tracker {
553553
//config,

0 commit comments

Comments
 (0)