Skip to content

Commit

Permalink
refactor: [torrust#1258] make things private or pub(crate) when possi…
Browse files Browse the repository at this point in the history
…ble.

Limit the exposed funtionality for pacakges. Specially the new
`tracker-core` package which has not been published yet.
  • Loading branch information
josecelano committed Feb 11, 2025
1 parent ea2d73d commit 53b94a3
Show file tree
Hide file tree
Showing 29 changed files with 348 additions and 311 deletions.
8 changes: 4 additions & 4 deletions packages/tracker-core/src/announce_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ mod tests {
use torrust_tracker_test_helpers::configuration;

use crate::announce_handler::AnnounceHandler;
use crate::core_tests::initialize_handlers;
use crate::scrape_handler::ScrapeHandler;
use crate::test_helpers::tests::initialize_handlers;

fn public_tracker() -> (Arc<AnnounceHandler>, Arc<ScrapeHandler>) {
let config = configuration::ephemeral_public();
Expand Down Expand Up @@ -244,7 +244,7 @@ mod tests {
peer_ip, public_tracker, sample_peer_1, sample_peer_2, sample_peer_3,
};
use crate::announce_handler::PeersWanted;
use crate::core_tests::{sample_info_hash, sample_peer};
use crate::test_helpers::tests::{sample_info_hash, sample_peer};

mod should_assign_the_ip_to_the_peer {

Expand Down Expand Up @@ -411,7 +411,7 @@ mod tests {

use crate::announce_handler::tests::the_announce_handler::{peer_ip, public_tracker};
use crate::announce_handler::PeersWanted;
use crate::core_tests::{completed_peer, leecher, sample_info_hash, seeder, started_peer};
use crate::test_helpers::tests::{completed_peer, leecher, sample_info_hash, seeder, started_peer};

#[tokio::test]
async fn when_the_peer_is_a_seeder() {
Expand Down Expand Up @@ -474,8 +474,8 @@ mod tests {

use crate::announce_handler::tests::the_announce_handler::peer_ip;
use crate::announce_handler::{AnnounceHandler, PeersWanted};
use crate::core_tests::{sample_info_hash, sample_peer};
use crate::databases::setup::initialize_database;
use crate::test_helpers::tests::{sample_info_hash, sample_peer};
use crate::torrent::manager::TorrentsManager;
use crate::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
Expand Down
8 changes: 4 additions & 4 deletions packages/tracker-core/src/authentication/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl KeysHandler {
/// # Errors
///
/// Will return a `database::Error` if unable to add the `auth_key` to the database.
pub async fn generate_permanent_peer_key(&self) -> Result<PeerKey, databases::error::Error> {
pub(crate) async fn generate_permanent_peer_key(&self) -> Result<PeerKey, databases::error::Error> {
self.generate_expiring_peer_key(None).await
}

Expand Down Expand Up @@ -170,7 +170,7 @@ impl KeysHandler {
/// # Arguments
///
/// * `key` - The pre-generated key.
pub async fn add_permanent_peer_key(&self, key: Key) -> Result<PeerKey, databases::error::Error> {
pub(crate) async fn add_permanent_peer_key(&self, key: Key) -> Result<PeerKey, databases::error::Error> {
self.add_expiring_peer_key(key, None).await
}

Expand All @@ -188,7 +188,7 @@ impl KeysHandler {
/// * `key` - The pre-generated key.
/// * `lifetime` - The duration in seconds for the new key. The key will be
/// no longer valid after `lifetime` seconds.
pub async fn add_expiring_peer_key(
pub(crate) async fn add_expiring_peer_key(
&self,
key: Key,
valid_until: Option<DurationSinceUnixEpoch>,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl KeysHandler {
}

/// It removes an authentication key from memory.
pub async fn remove_in_memory_auth_key(&self, key: &Key) {
pub(crate) async fn remove_in_memory_auth_key(&self, key: &Key) {
self.in_memory_key_repository.remove(key).await;
}

Expand Down
10 changes: 6 additions & 4 deletions packages/tracker-core/src/authentication/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ pub type ParseKeyError = peer_key::ParseKeyError;
///
/// For more information see function [`generate_key`](crate::authentication::key::generate_key) to generate the
/// [`PeerKey`](crate::authentication::PeerKey).
pub const AUTH_KEY_LENGTH: usize = 32;
pub(crate) const AUTH_KEY_LENGTH: usize = 32;

/// It generates a new permanent random key [`PeerKey`].
#[cfg(test)]
#[must_use]
pub fn generate_permanent_key() -> PeerKey {
pub(crate) fn generate_permanent_key() -> PeerKey {
generate_key(None)
}

/// It generates a new expiring random key [`PeerKey`].
#[cfg(test)]
#[must_use]
pub fn generate_expiring_key(lifetime: Duration) -> PeerKey {
pub(crate) fn generate_expiring_key(lifetime: Duration) -> PeerKey {
generate_key(Some(lifetime))
}

Expand All @@ -85,7 +87,7 @@ pub fn generate_expiring_key(lifetime: Duration) -> PeerKey {
///
/// * `lifetime`: if `None` the key will be permanent.
#[must_use]
pub fn generate_key(lifetime: Option<Duration>) -> PeerKey {
pub(crate) fn generate_key(lifetime: Option<Duration>) -> PeerKey {
let random_key = Key::random();

if let Some(lifetime) = lifetime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ pub struct InMemoryKeyRepository {

impl InMemoryKeyRepository {
/// It adds a new authentication key.
pub async fn insert(&self, auth_key: &PeerKey) {
pub(crate) async fn insert(&self, auth_key: &PeerKey) {
self.keys.write().await.insert(auth_key.key.clone(), auth_key.clone());
}

/// It removes an authentication key.
pub async fn remove(&self, key: &Key) {
pub(crate) async fn remove(&self, key: &Key) {
self.keys.write().await.remove(key);
}

pub async fn get(&self, key: &Key) -> Option<PeerKey> {
pub(crate) async fn get(&self, key: &Key) -> Option<PeerKey> {
self.keys.read().await.get(key).cloned()
}

/// It clears all the authentication keys.
pub async fn clear(&self) {
#[allow(dead_code)]
pub(crate) async fn clear(&self) {
let mut keys = self.keys.write().await;
keys.clear();
}
Expand Down
215 changes: 0 additions & 215 deletions packages/tracker-core/src/core_tests.rs

This file was deleted.

8 changes: 4 additions & 4 deletions packages/tracker-core/src/databases/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub mod sqlite;
/// # Errors
///
/// Will return `Error` if unable to build the driver.
pub fn build(driver: &Driver, db_path: &str) -> Result<Box<dyn Database>, Error> {
pub(crate) fn build(driver: &Driver, db_path: &str) -> Result<Box<dyn Database>, Error> {
let database: Box<dyn Database> = match driver {
Driver::Sqlite3 => Box::new(Sqlite::new(db_path)?),
Driver::MySQL => Box::new(Mysql::new(db_path)?),
Expand All @@ -85,7 +85,7 @@ pub fn build(driver: &Driver, db_path: &str) -> Result<Box<dyn Database>, Error>
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use std::sync::Arc;
use std::time::Duration;

Expand Down Expand Up @@ -152,8 +152,8 @@ mod tests {

use std::sync::Arc;

use crate::core_tests::sample_info_hash;
use crate::databases::Database;
use crate::test_helpers::tests::sample_info_hash;

pub fn it_should_save_and_load_persistent_torrents(driver: &Arc<Box<dyn Database>>) {
let infohash = sample_info_hash();
Expand Down Expand Up @@ -232,8 +232,8 @@ mod tests {

use std::sync::Arc;

use crate::core_tests::random_info_hash;
use crate::databases::Database;
use crate::test_helpers::tests::random_info_hash;

pub fn it_should_load_the_whitelist(driver: &Arc<Box<dyn Database>>) {
let infohash = random_info_hash();
Expand Down
2 changes: 1 addition & 1 deletion packages/tracker-core/src/databases/driver/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::authentication::{self, Key};

const DRIVER: Driver = Driver::MySQL;

pub struct Mysql {
pub(crate) struct Mysql {
pool: Pool<MySqlConnectionManager>,
}

Expand Down
Loading

0 comments on commit 53b94a3

Please sign in to comment.