Skip to content

Commit 9ddc079

Browse files
committed
fmt: format the world
1 parent 06bb34b commit 9ddc079

32 files changed

+790
-647
lines changed

src/auth.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
use actix_web::HttpRequest;
2-
use crate::models::user::{UserClaims, UserCompact};
3-
use jsonwebtoken::{decode, DecodingKey, Validation, Algorithm, encode, Header, EncodingKey};
4-
use crate::utils::time::current_time;
5-
use crate::errors::ServiceError;
61
use std::sync::Arc;
2+
3+
use actix_web::HttpRequest;
4+
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
5+
76
use crate::config::Configuration;
87
use crate::databases::database::Database;
8+
use crate::errors::ServiceError;
9+
use crate::models::user::{UserClaims, UserCompact};
10+
use crate::utils::time::current_time;
911

1012
pub struct AuthorizationService {
1113
cfg: Arc<Configuration>,
12-
database: Arc<Box<dyn Database>>
14+
database: Arc<Box<dyn Database>>,
1315
}
1416

1517
impl AuthorizationService {
1618
pub fn new(cfg: Arc<Configuration>, database: Arc<Box<dyn Database>>) -> AuthorizationService {
17-
AuthorizationService {
18-
cfg,
19-
database
20-
}
19+
AuthorizationService { cfg, database }
2120
}
2221

2322
pub async fn sign_jwt(&self, user: UserCompact) -> String {
@@ -28,17 +27,9 @@ impl AuthorizationService {
2827
// TODO: create config option for setting the token validity in seconds
2928
let exp_date = current_time() + 1_209_600; // two weeks from now
3029

31-
let claims = UserClaims {
32-
user,
33-
exp: exp_date,
34-
};
30+
let claims = UserClaims { user, exp: exp_date };
3531

36-
let token = encode(
37-
&Header::default(),
38-
&claims,
39-
&EncodingKey::from_secret(key),
40-
)
41-
.unwrap();
32+
let token = encode(&Header::default(), &claims, &EncodingKey::from_secret(key)).unwrap();
4233

4334
token
4435
}
@@ -53,11 +44,11 @@ impl AuthorizationService {
5344
) {
5445
Ok(token_data) => {
5546
if token_data.claims.exp < current_time() {
56-
return Err(ServiceError::TokenExpired)
47+
return Err(ServiceError::TokenExpired);
5748
}
5849
Ok(token_data.claims)
59-
},
60-
Err(_) => Err(ServiceError::TokenInvalid)
50+
}
51+
Err(_) => Err(ServiceError::TokenInvalid),
6152
}
6253
}
6354

@@ -73,14 +64,15 @@ impl AuthorizationService {
7364
Err(e) => Err(e),
7465
}
7566
}
76-
None => Err(ServiceError::TokenNotFound)
67+
None => Err(ServiceError::TokenNotFound),
7768
}
7869
}
7970

8071
pub async fn get_user_compact_from_request(&self, req: &HttpRequest) -> Result<UserCompact, ServiceError> {
8172
let claims = self.get_claims_from_request(req).await?;
8273

83-
self.database.get_user_compact_from_id(claims.user.user_id)
74+
self.database
75+
.get_user_compact_from_id(claims.user.user_id)
8476
.await
8577
.map_err(|_| ServiceError::UserNotFound)
8678
}

src/common.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::sync::Arc;
2-
use crate::config::Configuration;
2+
33
use crate::auth::AuthorizationService;
4+
use crate::config::Configuration;
45
use crate::databases::database::Database;
5-
use crate::tracker::TrackerService;
66
use crate::mailer::MailerService;
7+
use crate::tracker::TrackerService;
78

89
pub type Username = String;
910

@@ -14,11 +15,17 @@ pub struct AppData {
1415
pub database: Arc<Box<dyn Database>>,
1516
pub auth: Arc<AuthorizationService>,
1617
pub tracker: Arc<TrackerService>,
17-
pub mailer: Arc<MailerService>
18+
pub mailer: Arc<MailerService>,
1819
}
1920

2021
impl AppData {
21-
pub fn new(cfg: Arc<Configuration>, database: Arc<Box<dyn Database>>, auth: Arc<AuthorizationService>, tracker: Arc<TrackerService>, mailer: Arc<MailerService>) -> AppData {
22+
pub fn new(
23+
cfg: Arc<Configuration>,
24+
database: Arc<Box<dyn Database>>,
25+
auth: Arc<AuthorizationService>,
26+
tracker: Arc<TrackerService>,
27+
mailer: Arc<MailerService>,
28+
) -> AppData {
2229
AppData {
2330
cfg,
2431
database,

src/config.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::fs;
2-
use config::{ConfigError, Config, File};
32
use std::path::Path;
4-
use serde::{Serialize, Deserialize};
3+
4+
use config::{Config, ConfigError, File};
5+
use serde::{Deserialize, Serialize};
56
use tokio::sync::RwLock;
67

78
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -14,7 +15,7 @@ pub enum TrackerMode {
1415
Public,
1516
Private,
1617
Whitelisted,
17-
PrivateWhitelisted
18+
PrivateWhitelisted,
1819
}
1920

2021
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -36,7 +37,7 @@ pub struct Network {
3637
pub enum EmailOnSignup {
3738
Required,
3839
Optional,
39-
None
40+
None,
4041
}
4142

4243
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -76,35 +77,35 @@ pub struct TorrustConfig {
7677

7778
#[derive(Debug)]
7879
pub struct Configuration {
79-
pub settings: RwLock<TorrustConfig>
80+
pub settings: RwLock<TorrustConfig>,
8081
}
8182

8283
impl Configuration {
8384
pub fn default() -> Configuration {
8485
let torrust_config = TorrustConfig {
8586
website: Website {
86-
name: "Torrust".to_string()
87+
name: "Torrust".to_string(),
8788
},
8889
tracker: Tracker {
8990
url: "udp://localhost:6969".to_string(),
9091
mode: TrackerMode::Public,
9192
api_url: "http://localhost:1212".to_string(),
9293
token: "MyAccessToken".to_string(),
93-
token_valid_seconds: 7257600
94+
token_valid_seconds: 7257600,
9495
},
9596
net: Network {
9697
port: 3000,
97-
base_url: None
98+
base_url: None,
9899
},
99100
auth: Auth {
100101
email_on_signup: EmailOnSignup::Optional,
101102
min_password_length: 6,
102103
max_password_length: 64,
103-
secret_key: "MaxVerstappenWC2021".to_string()
104+
secret_key: "MaxVerstappenWC2021".to_string(),
104105
},
105106
database: Database {
106107
connect_url: "sqlite://data.db?mode=rwc".to_string(),
107-
torrent_info_update_interval: 3600
108+
torrent_info_update_interval: 3600,
108109
},
109110
mail: Mail {
110111
email_verification_enabled: false,
@@ -113,12 +114,12 @@ impl Configuration {
113114
username: "".to_string(),
114115
password: "".to_string(),
115116
server: "".to_string(),
116-
port: 25
117-
}
117+
port: 25,
118+
},
118119
};
119120

120121
Configuration {
121-
settings: RwLock::new(torrust_config)
122+
settings: RwLock::new(torrust_config),
122123
}
123124
}
124125

@@ -134,7 +135,9 @@ impl Configuration {
134135
eprintln!("Creating config file..");
135136
let config = Configuration::default();
136137
let _ = config.save_to_file().await;
137-
return Err(ConfigError::Message(format!("Please edit the config.TOML in the root folder and restart the tracker.")))
138+
return Err(ConfigError::Message(format!(
139+
"Please edit the config.TOML in the root folder and restart the tracker."
140+
)));
138141
}
139142

140143
let torrust_config: TorrustConfig = match config.try_into() {
@@ -143,11 +146,11 @@ impl Configuration {
143146
}?;
144147

145148
Ok(Configuration {
146-
settings: RwLock::new(torrust_config)
149+
settings: RwLock::new(torrust_config),
147150
})
148151
}
149152

150-
pub async fn save_to_file(&self) -> Result<(), ()>{
153+
pub async fn save_to_file(&self) -> Result<(), ()> {
151154
let settings = self.settings.read().await;
152155

153156
let toml_string = toml::to_string(&*settings).expect("Could not encode TOML value");
@@ -178,7 +181,7 @@ impl Configuration {
178181
website_name: settings_lock.website.name.clone(),
179182
tracker_url: settings_lock.tracker.url.clone(),
180183
tracker_mode: settings_lock.tracker.mode.clone(),
181-
email_on_signup: settings_lock.auth.email_on_signup.clone()
184+
email_on_signup: settings_lock.auth.email_on_signup.clone(),
182185
}
183186
}
184187
}
@@ -188,5 +191,5 @@ pub struct ConfigurationPublic {
188191
website_name: String,
189192
tracker_url: String,
190193
tracker_mode: TrackerMode,
191-
email_on_signup: EmailOnSignup
194+
email_on_signup: EmailOnSignup,
192195
}

src/databases/database.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use async_trait::async_trait;
2-
use chrono::{NaiveDateTime};
3-
use serde::{Serialize, Deserialize};
2+
use chrono::NaiveDateTime;
3+
use serde::{Deserialize, Serialize};
44

55
use crate::databases::mysql::MysqlDatabase;
66
use crate::databases::sqlite::SqliteDatabase;
7-
use crate::models::response::{TorrentsResponse};
7+
use crate::models::response::TorrentsResponse;
88
use crate::models::torrent::TorrentListing;
99
use crate::models::torrent_file::{DbTorrentInfo, Torrent, TorrentFile};
1010
use crate::models::tracker_key::TrackerKey;
@@ -14,7 +14,7 @@ use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};
1414
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
1515
pub enum DatabaseDriver {
1616
Sqlite3,
17-
Mysql
17+
Mysql,
1818
}
1919

2020
/// Compact representation of torrent.
@@ -29,7 +29,7 @@ pub struct TorrentCompact {
2929
pub struct Category {
3030
pub category_id: i64,
3131
pub name: String,
32-
pub num_torrents: i64
32+
pub num_torrents: i64,
3333
}
3434

3535
/// Sorting options for torrents.
@@ -73,9 +73,7 @@ pub async fn connect_database(db_path: &str) -> Result<Box<dyn Database>, Databa
7373
let db = MysqlDatabase::new(db_path).await;
7474
Ok(Box::new(db))
7575
}
76-
_ => {
77-
Err(DatabaseError::UnrecognizedDatabaseDriver)
78-
}
76+
_ => Err(DatabaseError::UnrecognizedDatabaseDriver),
7977
}
8078
}
8179

@@ -137,10 +135,24 @@ pub trait Database: Sync + Send {
137135
async fn delete_category(&self, category_name: &str) -> Result<(), DatabaseError>;
138136

139137
/// Get results of a torrent search in a paginated and sorted form as `TorrentsResponse` from `search`, `categories`, `sort`, `offset` and `page_size`.
140-
async fn get_torrents_search_sorted_paginated(&self, search: &Option<String>, categories: &Option<Vec<String>>, sort: &Sorting, offset: u64, page_size: u8) -> Result<TorrentsResponse, DatabaseError>;
138+
async fn get_torrents_search_sorted_paginated(
139+
&self,
140+
search: &Option<String>,
141+
categories: &Option<Vec<String>>,
142+
sort: &Sorting,
143+
offset: u64,
144+
page_size: u8,
145+
) -> Result<TorrentsResponse, DatabaseError>;
141146

142147
/// Add new torrent and return the newly inserted `torrent_id` with `torrent`, `uploader_id`, `category_id`, `title` and `description`.
143-
async fn insert_torrent_and_get_id(&self, torrent: &Torrent, uploader_id: i64, category_id: i64, title: &str, description: &str) -> Result<i64, DatabaseError>;
148+
async fn insert_torrent_and_get_id(
149+
&self,
150+
torrent: &Torrent,
151+
uploader_id: i64,
152+
category_id: i64,
153+
title: &str,
154+
description: &str,
155+
) -> Result<i64, DatabaseError>;
144156

145157
/// Get `Torrent` from `torrent_id`.
146158
async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, DatabaseError>;
@@ -167,7 +179,13 @@ pub trait Database: Sync + Send {
167179
async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), DatabaseError>;
168180

169181
/// Update the seeders and leechers info for a torrent with `torrent_id`, `tracker_url`, `seeders` and `leechers`.
170-
async fn update_tracker_info(&self, torrent_id: i64, tracker_url: &str, seeders: i64, leechers: i64) -> Result<(), DatabaseError>;
182+
async fn update_tracker_info(
183+
&self,
184+
torrent_id: i64,
185+
tracker_url: &str,
186+
seeders: i64,
187+
leechers: i64,
188+
) -> Result<(), DatabaseError>;
171189

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

src/databases/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub mod database;
2-
pub mod sqlite;
32
pub mod mysql;
3+
pub mod sqlite;

0 commit comments

Comments
 (0)