Skip to content

Commit b400962

Browse files
committed
fix: format
1 parent 8b761c8 commit b400962

22 files changed

+195
-425
lines changed

build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
fn main() {
33
// trigger recompilation when a new migration is added
44
println!("cargo:rerun-if-changed=migrations");
5-
}
5+
}

src/databases/sqlite.rs

+21-60
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ impl Database for SqliteDatabase {
5454
.map_err(|_| DatabaseError::Error)?;
5555

5656
// add password hash for account
57-
let insert_user_auth_result =
58-
query("INSERT INTO torrust_user_authentication (user_id, password_hash) VALUES (?, ?)")
59-
.bind(user_id)
60-
.bind(password_hash)
61-
.execute(&mut tx)
62-
.await
63-
.map_err(|_| DatabaseError::Error);
57+
let insert_user_auth_result = query("INSERT INTO torrust_user_authentication (user_id, password_hash) VALUES (?, ?)")
58+
.bind(user_id)
59+
.bind(password_hash)
60+
.execute(&mut tx)
61+
.await
62+
.map_err(|_| DatabaseError::Error);
6463

6564
// rollback transaction on error
6665
if let Err(e) = insert_user_auth_result {
@@ -109,23 +108,15 @@ impl Database for SqliteDatabase {
109108
.map_err(|_| DatabaseError::UserNotFound)
110109
}
111110

112-
async fn get_user_authentication_from_id(
113-
&self,
114-
user_id: i64,
115-
) -> Result<UserAuthentication, DatabaseError> {
116-
query_as::<_, UserAuthentication>(
117-
"SELECT * FROM torrust_user_authentication WHERE user_id = ?",
118-
)
119-
.bind(user_id)
120-
.fetch_one(&self.pool)
121-
.await
122-
.map_err(|_| DatabaseError::UserNotFound)
111+
async fn get_user_authentication_from_id(&self, user_id: i64) -> Result<UserAuthentication, DatabaseError> {
112+
query_as::<_, UserAuthentication>("SELECT * FROM torrust_user_authentication WHERE user_id = ?")
113+
.bind(user_id)
114+
.fetch_one(&self.pool)
115+
.await
116+
.map_err(|_| DatabaseError::UserNotFound)
123117
}
124118

125-
async fn get_user_profile_from_username(
126-
&self,
127-
username: &str,
128-
) -> Result<UserProfile, DatabaseError> {
119+
async fn get_user_profile_from_username(&self, username: &str) -> Result<UserProfile, DatabaseError> {
129120
query_as::<_, UserProfile>("SELECT * FROM torrust_user_profiles WHERE username = ?")
130121
.bind(username)
131122
.fetch_one(&self.pool)
@@ -164,12 +155,7 @@ impl Database for SqliteDatabase {
164155
.map_err(|_| DatabaseError::Error)
165156
}
166157

167-
async fn ban_user(
168-
&self,
169-
user_id: i64,
170-
reason: &str,
171-
date_expiry: NaiveDateTime,
172-
) -> Result<(), DatabaseError> {
158+
async fn ban_user(&self, user_id: i64, reason: &str, date_expiry: NaiveDateTime) -> Result<(), DatabaseError> {
173159
// date needs to be in ISO 8601 format
174160
let date_expiry_string = date_expiry.format("%Y-%m-%d %H:%M:%S").to_string();
175161

@@ -207,11 +193,7 @@ impl Database for SqliteDatabase {
207193
.map_err(|_| DatabaseError::Error)
208194
}
209195

210-
async fn add_tracker_key(
211-
&self,
212-
user_id: i64,
213-
tracker_key: &TrackerKey,
214-
) -> Result<(), DatabaseError> {
196+
async fn add_tracker_key(&self, user_id: i64, tracker_key: &TrackerKey) -> Result<(), DatabaseError> {
215197
let key = tracker_key.key.clone();
216198

217199
query("INSERT INTO torrust_tracker_keys (user_id, tracker_key, date_expiry) VALUES ($1, $2, $3)")
@@ -361,10 +343,7 @@ impl Database for SqliteDatabase {
361343
category_filter_query
362344
);
363345

364-
let count_query = format!(
365-
"SELECT COUNT(*) as count FROM ({}) AS count_table",
366-
query_string
367-
);
346+
let count_query = format!("SELECT COUNT(*) as count FROM ({}) AS count_table", query_string);
368347

369348
let count_result: Result<i64, DatabaseError> = query_as(&count_query)
370349
.bind(title.clone())
@@ -411,11 +390,7 @@ impl Database for SqliteDatabase {
411390
let (pieces, root_hash): (String, bool) = if let Some(pieces) = &torrent.info.pieces {
412391
(bytes_to_hex(pieces.as_ref()), false)
413392
} else {
414-
let root_hash = torrent
415-
.info
416-
.root_hash
417-
.as_ref()
418-
.ok_or(DatabaseError::Error)?;
393+
let root_hash = torrent.info.root_hash.as_ref().ok_or(DatabaseError::Error)?;
419394
(root_hash.to_string(), true)
420395
};
421396

@@ -562,10 +537,7 @@ impl Database for SqliteDatabase {
562537
))
563538
}
564539

565-
async fn get_torrent_info_from_id(
566-
&self,
567-
torrent_id: i64,
568-
) -> Result<DbTorrentInfo, DatabaseError> {
540+
async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, DatabaseError> {
569541
query_as::<_, DbTorrentInfo>(
570542
"SELECT name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE torrent_id = ?",
571543
)
@@ -604,10 +576,7 @@ impl Database for SqliteDatabase {
604576
.map_err(|_| DatabaseError::TorrentNotFound)
605577
}
606578

607-
async fn get_torrent_listing_from_id(
608-
&self,
609-
torrent_id: i64,
610-
) -> Result<TorrentListing, DatabaseError> {
579+
async fn get_torrent_listing_from_id(&self, torrent_id: i64) -> Result<TorrentListing, DatabaseError> {
611580
query_as::<_, TorrentListing>(
612581
"SELECT tt.torrent_id, tp.username AS uploader, tt.info_hash, ti.title, ti.description, tt.category_id, tt.date_uploaded, tt.size AS file_size,
613582
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
@@ -632,11 +601,7 @@ impl Database for SqliteDatabase {
632601
.map_err(|_| DatabaseError::Error)
633602
}
634603

635-
async fn update_torrent_title(
636-
&self,
637-
torrent_id: i64,
638-
title: &str,
639-
) -> Result<(), DatabaseError> {
604+
async fn update_torrent_title(&self, torrent_id: i64, title: &str) -> Result<(), DatabaseError> {
640605
query("UPDATE torrust_torrent_info SET title = $1 WHERE torrent_id = $2")
641606
.bind(title)
642607
.bind(torrent_id)
@@ -661,11 +626,7 @@ impl Database for SqliteDatabase {
661626
})
662627
}
663628

664-
async fn update_torrent_description(
665-
&self,
666-
torrent_id: i64,
667-
description: &str,
668-
) -> Result<(), DatabaseError> {
629+
async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), DatabaseError> {
669630
query("UPDATE torrust_torrent_info SET description = $1 WHERE torrent_id = $2")
670631
.bind(description)
671632
.bind(torrent_id)

src/models/tracker_key.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ pub struct NewTrackerKey {
1717
pub struct Duration {
1818
pub secs: i64,
1919
pub nanos: i64,
20-
}
20+
}

src/models/user.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct User {
1111
#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)]
1212
pub struct UserAuthentication {
1313
pub user_id: i64,
14-
pub password_hash: String
14+
pub password_hash: String,
1515
}
1616

1717
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, sqlx::FromRow)]

src/routes/user.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,17 @@ pub async fn ban_user(req: HttpRequest, app_data: WebAppData) -> ServiceResult<i
282282

283283
#[cfg(test)]
284284
mod tests {
285-
use crate::models::user::UserAuthentication;
286-
287285
use super::verify_password;
286+
use crate::models::user::UserAuthentication;
288287

289288
#[test]
290289
fn password_hashed_with_pbkdf2_sha256_should_be_verified() {
291290
let password = "12345678".as_bytes();
292-
let password_hash = "$pbkdf2-sha256$i=10000,l=32$pZIh8nilm+cg6fk5Ubf2zQ$AngLuZ+sGUragqm4bIae/W+ior0TWxYFFaTx8CulqtY".to_string();
291+
let password_hash =
292+
"$pbkdf2-sha256$i=10000,l=32$pZIh8nilm+cg6fk5Ubf2zQ$AngLuZ+sGUragqm4bIae/W+ior0TWxYFFaTx8CulqtY".to_string();
293293
let user_authentication = UserAuthentication {
294294
user_id: 1i64,
295-
password_hash
295+
password_hash,
296296
};
297297

298298
assert!(verify_password(password, &user_authentication).is_ok());
@@ -302,16 +302,14 @@ mod tests {
302302
#[test]
303303
fn password_hashed_with_argon2_should_be_verified() {
304304
let password = "87654321".as_bytes();
305-
let password_hash = "$argon2id$v=19$m=4096,t=3,p=1$ycK5lJ4xmFBnaJ51M1j1eA$kU3UlNiSc3JDbl48TCj7JBDKmrT92DOUAgo4Yq0+nMw".to_string();
305+
let password_hash =
306+
"$argon2id$v=19$m=4096,t=3,p=1$ycK5lJ4xmFBnaJ51M1j1eA$kU3UlNiSc3JDbl48TCj7JBDKmrT92DOUAgo4Yq0+nMw".to_string();
306307
let user_authentication = UserAuthentication {
307308
user_id: 1i64,
308-
password_hash
309+
password_hash,
309310
};
310311

311312
assert!(verify_password(password, &user_authentication).is_ok());
312313
assert!(verify_password("incorrect password".as_bytes(), &user_authentication).is_err());
313314
}
314315
}
315-
316-
317-

src/upgrades/from_v1_0_0_to_v2_0_0/databases/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use std::sync::Arc;
2+
13
use self::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
24
use self::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
3-
use std::sync::Arc;
45

56
pub mod sqlite_v1_0_0;
67
pub mod sqlite_v2_0_0;

src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,10 @@ impl SqliteDatabaseV1_0_0 {
6565
}
6666

6767
pub async fn get_categories_order_by_id(&self) -> Result<Vec<CategoryRecordV1>, DatabaseError> {
68-
query_as::<_, CategoryRecordV1>(
69-
"SELECT category_id, name FROM torrust_categories ORDER BY category_id ASC",
70-
)
71-
.fetch_all(&self.pool)
72-
.await
73-
.map_err(|_| DatabaseError::Error)
68+
query_as::<_, CategoryRecordV1>("SELECT category_id, name FROM torrust_categories ORDER BY category_id ASC")
69+
.fetch_all(&self.pool)
70+
.await
71+
.map_err(|_| DatabaseError::Error)
7472
}
7573

7674
pub async fn get_users(&self) -> Result<Vec<UserRecordV1>, sqlx::Error> {
@@ -99,10 +97,8 @@ impl SqliteDatabaseV1_0_0 {
9997
}
10098

10199
pub async fn get_torrent_files(&self) -> Result<Vec<TorrentFileRecordV1>, sqlx::Error> {
102-
query_as::<_, TorrentFileRecordV1>(
103-
"SELECT * FROM torrust_torrent_files ORDER BY file_id ASC",
104-
)
105-
.fetch_all(&self.pool)
106-
.await
100+
query_as::<_, TorrentFileRecordV1>("SELECT * FROM torrust_torrent_files ORDER BY file_id ASC")
101+
.fetch_all(&self.pool)
102+
.await
107103
}
108104
}

0 commit comments

Comments
 (0)