Skip to content

Commit

Permalink
dev: fix clippy warnings for: src/databases/database.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed May 10, 2023
1 parent 7b28120 commit ebc360e
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 297 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::bootstrap::logging;
use crate::cache::image::manager::ImageCacheService;
use crate::common::AppData;
use crate::config::Configuration;
use crate::databases::database::connect_database;
use crate::databases::database;
use crate::mailer::MailerService;
use crate::routes;
use crate::tracker::service::Service;
Expand Down Expand Up @@ -43,7 +43,7 @@ pub async fn run(configuration: Configuration) -> Running {

// Build app dependencies

let database = Arc::new(connect_database(&database_connect_url).await.expect("Database error."));
let database = Arc::new(database::connect(&database_connect_url).await.expect("Database error."));
let auth = Arc::new(AuthorizationService::new(cfg.clone(), database.clone()));
let tracker_service = Arc::new(Service::new(cfg.clone(), database.clone()).await);
let tracker_statistics_importer =
Expand Down
4 changes: 2 additions & 2 deletions src/console/commands/import_tracker_statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use text_colorizer::Colorize;

use crate::bootstrap::config::init_configuration;
use crate::bootstrap::logging;
use crate::databases::database::connect_database;
use crate::databases::database;
use crate::tracker::service::Service;
use crate::tracker::statistics_importer::StatisticsImporter;

Expand Down Expand Up @@ -77,7 +77,7 @@ pub async fn import(_args: &Arguments) {
eprintln!("Tracker url: {}", tracker_url.green());

let database = Arc::new(
connect_database(&settings.database.connect_url)
database::connect(&settings.database.connect_url)
.await
.expect("Database error."),
);
Expand Down
84 changes: 41 additions & 43 deletions src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};

/// Database drivers.
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub enum DatabaseDriver {
pub enum Driver {
Sqlite3,
Mysql,
}
Expand Down Expand Up @@ -50,7 +50,7 @@ pub enum Sorting {

/// Database errors.
#[derive(Debug)]
pub enum DatabaseError {
pub enum Error {
Error,
UnrecognizedDatabaseDriver, // when the db path does not start with sqlite or mysql
UsernameTaken,
Expand All @@ -64,7 +64,11 @@ pub enum DatabaseError {
}

/// Connect to a database.
pub async fn connect_database(db_path: &str) -> Result<Box<dyn Database>, DatabaseError> {
///
/// # Errors
///
/// This function will return an `Error::UnrecognizedDatabaseDriver` if unable to match database type.
pub async fn connect(db_path: &str) -> Result<Box<dyn Database>, Error> {
match &db_path.chars().collect::<Vec<char>>() as &[char] {
['s', 'q', 'l', 'i', 't', 'e', ..] => {
let db = SqliteDatabase::new(db_path).await;
Expand All @@ -74,66 +78,66 @@ pub async fn connect_database(db_path: &str) -> Result<Box<dyn Database>, Databa
let db = MysqlDatabase::new(db_path).await;
Ok(Box::new(db))
}
_ => Err(DatabaseError::UnrecognizedDatabaseDriver),
_ => Err(Error::UnrecognizedDatabaseDriver),
}
}

/// Trait for database implementations.
#[async_trait]
pub trait Database: Sync + Send {
/// Return current database driver.
fn get_database_driver(&self) -> DatabaseDriver;
fn get_database_driver(&self) -> Driver;

/// Add new user and return the newly inserted `user_id`.
async fn insert_user_and_get_id(&self, username: &str, email: &str, password: &str) -> Result<i64, DatabaseError>;
async fn insert_user_and_get_id(&self, username: &str, email: &str, password: &str) -> Result<i64, Error>;

/// Get `User` from `user_id`.
async fn get_user_from_id(&self, user_id: i64) -> Result<User, DatabaseError>;
async fn get_user_from_id(&self, user_id: i64) -> Result<User, Error>;

/// Get `UserAuthentication` from `user_id`.
async fn get_user_authentication_from_id(&self, user_id: i64) -> Result<UserAuthentication, DatabaseError>;
async fn get_user_authentication_from_id(&self, user_id: i64) -> Result<UserAuthentication, Error>;

/// Get `UserProfile` from `username`.
async fn get_user_profile_from_username(&self, username: &str) -> Result<UserProfile, DatabaseError>;
async fn get_user_profile_from_username(&self, username: &str) -> Result<UserProfile, Error>;

/// Get `UserCompact` from `user_id`.
async fn get_user_compact_from_id(&self, user_id: i64) -> Result<UserCompact, DatabaseError>;
async fn get_user_compact_from_id(&self, user_id: i64) -> Result<UserCompact, Error>;

/// Get a user's `TrackerKey`.
async fn get_user_tracker_key(&self, user_id: i64) -> Option<TrackerKey>;

/// Get total user count.
async fn count_users(&self) -> Result<i64, DatabaseError>;
async fn count_users(&self) -> Result<i64, Error>;

/// Ban user with `user_id`, `reason` and `date_expiry`.
async fn ban_user(&self, user_id: i64, reason: &str, date_expiry: NaiveDateTime) -> Result<(), DatabaseError>;
async fn ban_user(&self, user_id: i64, reason: &str, date_expiry: NaiveDateTime) -> Result<(), Error>;

/// Grant a user the administrator role.
async fn grant_admin_role(&self, user_id: i64) -> Result<(), DatabaseError>;
async fn grant_admin_role(&self, user_id: i64) -> Result<(), Error>;

/// Verify a user's email with `user_id`.
async fn verify_email(&self, user_id: i64) -> Result<(), DatabaseError>;
async fn verify_email(&self, user_id: i64) -> Result<(), Error>;

/// Link a `TrackerKey` to a certain user with `user_id`.
async fn add_tracker_key(&self, user_id: i64, tracker_key: &TrackerKey) -> Result<(), DatabaseError>;
async fn add_tracker_key(&self, user_id: i64, tracker_key: &TrackerKey) -> Result<(), Error>;

/// Delete user and all related user data with `user_id`.
async fn delete_user(&self, user_id: i64) -> Result<(), DatabaseError>;
async fn delete_user(&self, user_id: i64) -> Result<(), Error>;

/// Add a new category and return `category_id`.
async fn insert_category_and_get_id(&self, category_name: &str) -> Result<i64, DatabaseError>;
async fn insert_category_and_get_id(&self, category_name: &str) -> Result<i64, Error>;

/// Get `Category` from `category_id`.
async fn get_category_from_id(&self, category_id: i64) -> Result<Category, DatabaseError>;
async fn get_category_from_id(&self, category_id: i64) -> Result<Category, Error>;

/// Get `Category` from `category_name`.
async fn get_category_from_name(&self, category_name: &str) -> Result<Category, DatabaseError>;
async fn get_category_from_name(&self, category_name: &str) -> Result<Category, Error>;

/// Get all categories as `Vec<Category>`.
async fn get_categories(&self) -> Result<Vec<Category>, DatabaseError>;
async fn get_categories(&self) -> Result<Vec<Category>, Error>;

/// Delete category with `category_name`.
async fn delete_category(&self, category_name: &str) -> Result<(), DatabaseError>;
async fn delete_category(&self, category_name: &str) -> Result<(), Error>;

/// Get results of a torrent search in a paginated and sorted form as `TorrentsResponse` from `search`, `categories`, `sort`, `offset` and `page_size`.
async fn get_torrents_search_sorted_paginated(
Expand All @@ -143,7 +147,7 @@ pub trait Database: Sync + Send {
sort: &Sorting,
offset: u64,
page_size: u8,
) -> Result<TorrentsResponse, DatabaseError>;
) -> Result<TorrentsResponse, Error>;

/// Add new torrent and return the newly inserted `torrent_id` with `torrent`, `uploader_id`, `category_id`, `title` and `description`.
async fn insert_torrent_and_get_id(
Expand All @@ -153,10 +157,10 @@ pub trait Database: Sync + Send {
category_id: i64,
title: &str,
description: &str,
) -> Result<i64, DatabaseError>;
) -> Result<i64, Error>;

/// Get `Torrent` from `InfoHash`.
async fn get_torrent_from_infohash(&self, infohash: &InfoHash) -> Result<Torrent, DatabaseError> {
async fn get_torrent_from_infohash(&self, infohash: &InfoHash) -> Result<Torrent, Error> {
let torrent_info = self.get_torrent_info_from_infohash(infohash).await?;

let torrent_files = self.get_torrent_files_from_id(torrent_info.torrent_id).await?;
Expand All @@ -171,7 +175,7 @@ pub trait Database: Sync + Send {
}

/// Get `Torrent` from `torrent_id`.
async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, DatabaseError> {
async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, Error> {
let torrent_info = self.get_torrent_info_from_id(torrent_id).await?;

let torrent_files = self.get_torrent_files_from_id(torrent_id).await?;
Expand All @@ -186,44 +190,38 @@ pub trait Database: Sync + Send {
}

/// Get torrent's info as `DbTorrentInfo` from `torrent_id`.
async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, DatabaseError>;
async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, Error>;

/// Get torrent's info as `DbTorrentInfo` from torrent `InfoHash`.
async fn get_torrent_info_from_infohash(&self, info_hash: &InfoHash) -> Result<DbTorrentInfo, DatabaseError>;
async fn get_torrent_info_from_infohash(&self, info_hash: &InfoHash) -> Result<DbTorrentInfo, Error>;

/// Get all torrent's files as `Vec<TorrentFile>` from `torrent_id`.
async fn get_torrent_files_from_id(&self, torrent_id: i64) -> Result<Vec<TorrentFile>, DatabaseError>;
async fn get_torrent_files_from_id(&self, torrent_id: i64) -> Result<Vec<TorrentFile>, Error>;

/// Get all torrent's announce urls as `Vec<Vec<String>>` from `torrent_id`.
async fn get_torrent_announce_urls_from_id(&self, torrent_id: i64) -> Result<Vec<Vec<String>>, DatabaseError>;
async fn get_torrent_announce_urls_from_id(&self, torrent_id: i64) -> Result<Vec<Vec<String>>, Error>;

/// Get `TorrentListing` from `torrent_id`.
async fn get_torrent_listing_from_id(&self, torrent_id: i64) -> Result<TorrentListing, DatabaseError>;
async fn get_torrent_listing_from_id(&self, torrent_id: i64) -> Result<TorrentListing, Error>;

/// Get `TorrentListing` from `InfoHash`.
async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result<TorrentListing, DatabaseError>;
async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result<TorrentListing, Error>;

/// Get all torrents as `Vec<TorrentCompact>`.
async fn get_all_torrents_compact(&self) -> Result<Vec<TorrentCompact>, DatabaseError>;
async fn get_all_torrents_compact(&self) -> Result<Vec<TorrentCompact>, Error>;

/// Update a torrent's title with `torrent_id` and `title`.
async fn update_torrent_title(&self, torrent_id: i64, title: &str) -> Result<(), DatabaseError>;
async fn update_torrent_title(&self, torrent_id: i64, title: &str) -> Result<(), Error>;

/// Update a torrent's description with `torrent_id` and `description`.
async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), DatabaseError>;
async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), Error>;

/// Update the seeders and leechers info for a torrent with `torrent_id`, `tracker_url`, `seeders` and `leechers`.
async fn update_tracker_info(
&self,
torrent_id: i64,
tracker_url: &str,
seeders: i64,
leechers: i64,
) -> Result<(), DatabaseError>;
async fn update_tracker_info(&self, torrent_id: i64, tracker_url: &str, seeders: i64, leechers: i64) -> Result<(), Error>;

/// Delete a torrent with `torrent_id`.
async fn delete_torrent(&self, torrent_id: i64) -> Result<(), DatabaseError>;
async fn delete_torrent(&self, torrent_id: i64) -> Result<(), Error>;

/// DELETES ALL DATABASE ROWS, ONLY CALL THIS IF YOU KNOW WHAT YOU'RE DOING!
async fn delete_all_database_rows(&self) -> Result<(), DatabaseError>;
async fn delete_all_database_rows(&self) -> Result<(), Error>;
}
Loading

0 comments on commit ebc360e

Please sign in to comment.