Skip to content

Commit 38eabc4

Browse files
committed
clippy: fix src/databases/mysql.rs
1 parent 87160bd commit 38eabc4

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
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 derive_more::{Display, Error};
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::protocol::common::InfoHash;
88
use crate::tracker::key::AuthKey;
@@ -23,7 +23,7 @@ pub fn connect(db_driver: &Drivers, db_path: &str) -> Result<Box<dyn Database>,
2323
Box::new(db)
2424
}
2525
Drivers::MySQL => {
26-
let db = MysqlDatabase::new(db_path)?;
26+
let db = Mysql::new(db_path)?;
2727
Box::new(db)
2828
}
2929
};

src/databases/mysql.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ use crate::databases::database::{Database, Error};
1313
use crate::protocol::common::{InfoHash, AUTH_KEY_LENGTH};
1414
use crate::tracker::key::AuthKey;
1515

16-
pub struct MysqlDatabase {
16+
pub struct Mysql {
1717
pool: Pool<MysqlConnectionManager>,
1818
}
1919

20-
impl MysqlDatabase {
20+
impl Mysql {
21+
/// # Errors
22+
///
23+
/// Will return `r2d2::Error` if `db_path` is not able to create `MySQL` database.
2124
pub fn new(db_path: &str) -> Result<Self, r2d2::Error> {
2225
let opts = Opts::from_url(db_path).expect("Failed to connect to MySQL database.");
2326
let builder = OptsBuilder::from_opts(opts);
@@ -31,7 +34,7 @@ impl MysqlDatabase {
3134
}
3235

3336
#[async_trait]
34-
impl Database for MysqlDatabase {
37+
impl Database for Mysql {
3538
fn create_database_tables(&self) -> Result<(), database::Error> {
3639
let create_whitelist_table = "
3740
CREATE TABLE IF NOT EXISTS whitelist (
@@ -57,7 +60,7 @@ impl Database for MysqlDatabase {
5760
PRIMARY KEY (`id`),
5861
UNIQUE (`key`)
5962
);",
60-
AUTH_KEY_LENGTH as i8
63+
i8::try_from(AUTH_KEY_LENGTH).expect("Auth Key Length Should fit within a i8!")
6164
);
6265

6366
let mut conn = self.pool.get().map_err(|_| database::Error::DatabaseError)?;
@@ -95,7 +98,7 @@ impl Database for MysqlDatabase {
9598
"SELECT `key`, valid_until FROM `keys`",
9699
|(key, valid_until): (String, i64)| AuthKey {
97100
key,
98-
valid_until: Some(Duration::from_secs(valid_until as u64)),
101+
valid_until: Some(Duration::from_secs(valid_until.unsigned_abs())),
99102
},
100103
)
101104
.map_err(|_| database::Error::QueryReturnedNoRows)?;
@@ -188,7 +191,7 @@ impl Database for MysqlDatabase {
188191
{
189192
Some((key, valid_until)) => Ok(AuthKey {
190193
key,
191-
valid_until: Some(Duration::from_secs(valid_until as u64)),
194+
valid_until: Some(Duration::from_secs(valid_until.unsigned_abs())),
192195
}),
193196
None => Err(database::Error::InvalidQuery),
194197
}

0 commit comments

Comments
 (0)