Skip to content

Commit 593ac6f

Browse files
committed
dev: fix clippy warnings for: src/databases/mysql.rs
1 parent ebc360e commit 593ac6f

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/databases/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use async_trait::async_trait;
22
use chrono::NaiveDateTime;
33
use serde::{Deserialize, Serialize};
44

5-
use crate::databases::mysql::MysqlDatabase;
5+
use crate::databases::mysql::Mysql;
66
use crate::databases::sqlite::SqliteDatabase;
77
use crate::models::info_hash::InfoHash;
88
use crate::models::response::TorrentsResponse;
@@ -75,7 +75,7 @@ pub async fn connect(db_path: &str) -> Result<Box<dyn Database>, Error> {
7575
Ok(Box::new(db))
7676
}
7777
['m', 'y', 's', 'q', 'l', ..] => {
78-
let db = MysqlDatabase::new(db_path).await;
78+
let db = Mysql::new(db_path).await;
7979
Ok(Box::new(db))
8080
}
8181
_ => Err(Error::UnrecognizedDatabaseDriver),

src/databases/mysql.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};
1414
use crate::utils::clock::current_time;
1515
use crate::utils::hex::bytes_to_hex;
1616

17-
pub struct MysqlDatabase {
17+
pub struct Mysql {
1818
pub pool: MySqlPool,
1919
}
2020

21-
impl MysqlDatabase {
21+
impl Mysql {
2222
pub async fn new(database_url: &str) -> Self {
2323
let db = MySqlPoolOptions::new()
2424
.connect(database_url)
@@ -35,7 +35,7 @@ impl MysqlDatabase {
3535
}
3636

3737
#[async_trait]
38-
impl Database for MysqlDatabase {
38+
impl Database for Mysql {
3939
fn get_database_driver(&self) -> Driver {
4040
Driver::Mysql
4141
}
@@ -92,7 +92,7 @@ impl Database for MysqlDatabase {
9292
match insert_user_profile_result {
9393
Ok(_) => {
9494
let _ = tx.commit().await;
95-
Ok(user_id as i64)
95+
Ok(i64::overflowing_add_unsigned(0, user_id).0)
9696
}
9797
Err(e) => {
9898
let _ = tx.rollback().await;
@@ -133,11 +133,16 @@ impl Database for MysqlDatabase {
133133
.map_err(|_| database::Error::UserNotFound)
134134
}
135135

136+
/// Gets User Tracker Key
137+
///
138+
/// # Panics
139+
///
140+
/// Will panic if the input time overflows the `u64` seconds overflows the `i64` type.
141+
/// (this will naturally happen in 292.5 billion years)
136142
async fn get_user_tracker_key(&self, user_id: i64) -> Option<TrackerKey> {
137143
const HOUR_IN_SECONDS: i64 = 3600;
138144

139-
// casting current_time() to i64 will overflow in the year 2262
140-
let current_time_plus_hour = (current_time() as i64) + HOUR_IN_SECONDS;
145+
let current_time_plus_hour = i64::try_from(current_time()).unwrap().saturating_add(HOUR_IN_SECONDS);
141146

142147
// get tracker key that is valid for at least one hour from now
143148
query_as::<_, TrackerKey>("SELECT tracker_key AS 'key', date_expiry AS valid_until FROM torrust_tracker_keys WHERE user_id = ? AND date_expiry > ? ORDER BY date_expiry DESC")
@@ -233,7 +238,7 @@ impl Database for MysqlDatabase {
233238
.bind(category_name)
234239
.execute(&self.pool)
235240
.await
236-
.map(|v| v.last_insert_id() as i64)
241+
.map(|v| i64::try_from(v.last_insert_id()).expect("last ID is larger than i64"))
237242
.map_err(|e| match e {
238243
sqlx::Error::Database(err) => {
239244
if err.message().contains("UNIQUE") {
@@ -325,13 +330,13 @@ impl Database for MysqlDatabase {
325330
i += 1;
326331
}
327332
}
328-
if !category_filters.is_empty() {
333+
if category_filters.is_empty() {
334+
String::new()
335+
} else {
329336
format!(
330337
"INNER JOIN torrust_categories tc ON tt.category_id = tc.category_id AND ({}) ",
331338
category_filters
332339
)
333-
} else {
334-
String::new()
335340
}
336341
} else {
337342
String::new()
@@ -365,18 +370,19 @@ impl Database for MysqlDatabase {
365370

366371
let res: Vec<TorrentListing> = sqlx::query_as::<_, TorrentListing>(&query_string)
367372
.bind(title)
368-
.bind(offset as i64)
373+
.bind(i64::saturating_add_unsigned(0, offset))
369374
.bind(limit)
370375
.fetch_all(&self.pool)
371376
.await
372377
.map_err(|_| database::Error::Error)?;
373378

374379
Ok(TorrentsResponse {
375-
total: count as u32,
380+
total: u32::try_from(count).expect("variable `count` is larger than u32"),
376381
results: res,
377382
})
378383
}
379384

385+
#[allow(clippy::too_many_lines)]
380386
async fn insert_torrent_and_get_id(
381387
&self,
382388
torrent: &Torrent,
@@ -416,7 +422,7 @@ impl Database for MysqlDatabase {
416422
.bind(root_hash)
417423
.execute(&self.pool)
418424
.await
419-
.map(|v| v.last_insert_id() as i64)
425+
.map(|v| i64::try_from(v.last_insert_id()).expect("last ID is larger than i64"))
420426
.map_err(|e| match e {
421427
sqlx::Error::Database(err) => {
422428
if err.message().contains("info_hash") {

0 commit comments

Comments
 (0)