Skip to content

Commit

Permalink
Merge torrust#871: Add privacy methods to the TrackerMode enum
Browse files Browse the repository at this point in the history
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
  • Loading branch information
josecelano committed May 21, 2024
2 parents d4eaea9 + 0c9da2f commit b92401f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
48 changes: 47 additions & 1 deletion packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! by the tracker server crate, but also by other crates in the Torrust
//! ecosystem.
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::time::Duration;

use info_hash::InfoHash;
Expand Down Expand Up @@ -67,7 +69,7 @@ pub type PersistentTorrents = BTreeMap<InfoHash, u32>;
///
/// Refer to [Torrust Tracker Configuration](https://docs.rs/torrust-tracker-configuration)
/// to know how to configure the tracker to run in each mode.
#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
pub enum TrackerMode {
/// Will track every new info hash and serve every peer.
#[serde(rename = "public")]
Expand All @@ -85,3 +87,47 @@ pub enum TrackerMode {
#[serde(rename = "private_listed")]
PrivateListed,
}

impl Default for TrackerMode {
fn default() -> Self {
Self::Public
}
}

impl fmt::Display for TrackerMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
TrackerMode::Public => "public",
TrackerMode::Listed => "listed",
TrackerMode::Private => "private",
TrackerMode::PrivateListed => "private_listed",
};
write!(f, "{display_str}")
}
}

impl FromStr for TrackerMode {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"public" => Ok(TrackerMode::Public),
"listed" => Ok(TrackerMode::Listed),
"private" => Ok(TrackerMode::Private),
"private_listed" => Ok(TrackerMode::PrivateListed),
_ => Err(format!("Unknown tracker mode: {s}")),
}
}
}

impl TrackerMode {
#[must_use]
pub fn is_open(&self) -> bool {
matches!(self, TrackerMode::Public | TrackerMode::Listed)
}

#[must_use]
pub fn is_close(&self) -> bool {
!self.is_open()
}
}
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ impl Tracker {
) -> Result<Tracker, databases::error::Error> {
let database = Arc::new(databases::driver::build(&config.db_driver, &config.db_path)?);

let mode = config.mode;
let mode = config.mode.clone();

Ok(Tracker {
//config,
Expand Down

0 comments on commit b92401f

Please sign in to comment.