Skip to content

Commit 8614d7f

Browse files
CopilotDamnCrab
andauthored
fix: address code review suggestions (docs + grammar)
Agent-Logs-Url: https://github.com/DamnCrab/torrust-tracker/sessions/1b7dc46e-9918-4a95-91fe-29c1b375eb8a Co-authored-by: DamnCrab <42539593+DamnCrab@users.noreply.github.com>
1 parent 0ea3ea1 commit 8614d7f

2 files changed

Lines changed: 23 additions & 29 deletions

File tree

packages/tracker-core/src/databases/driver/postgres.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! This module provides an implementation of the [`Database`] trait for
44
//! `PostgreSQL` using the `r2d2_postgres` connection pool. It configures the
5-
//! PostgreSQL connection based on a URL, creates the necessary tables (for
5+
//! `PostgreSQL` connection based on a URL, creates the necessary tables (for
66
//! torrent metrics, torrent whitelist, and authentication keys), and implements
77
//! all CRUD operations required by the persistence layer.
88
//!
@@ -344,15 +344,15 @@ impl Database for Postgres {
344344
let info_hash_str = info_hash.to_string();
345345
self.with_connection(move |conn| {
346346
let rows_affected = conn.execute("INSERT INTO whitelist (info_hash) VALUES ($1)", &[&info_hash_str])?;
347-
Ok(rows_affected as usize)
347+
Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize"))
348348
})
349349
}
350350

351351
fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
352352
let info_hash_str = info_hash.to_string();
353353
self.with_connection(move |conn| {
354354
let rows_affected = conn.execute("DELETE FROM whitelist WHERE info_hash = $1", &[&info_hash_str])?;
355-
Ok(rows_affected as usize)
355+
Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize"))
356356
})
357357
}
358358

@@ -384,32 +384,29 @@ impl Database for Postgres {
384384
let key_str = auth_key.key.to_string();
385385
let valid_until = auth_key.valid_until;
386386
self.with_connection(move |conn| {
387-
let rows_affected = match valid_until {
388-
Some(valid_until) => {
389-
let valid_until_i64 = i64::try_from(valid_until.as_secs()).unwrap();
390-
conn.execute(
391-
"INSERT INTO keys (key, valid_until) VALUES ($1, $2)",
392-
&[&key_str, &valid_until_i64],
393-
)?
394-
}
395-
None => {
396-
let null_value: Option<i64> = None;
397-
conn.execute(
398-
"INSERT INTO keys (key, valid_until) VALUES ($1, $2)",
399-
&[&key_str, &null_value],
400-
)?
401-
}
387+
let rows_affected = if let Some(valid_until) = valid_until {
388+
let valid_until_i64 = i64::try_from(valid_until.as_secs()).unwrap();
389+
conn.execute(
390+
"INSERT INTO keys (key, valid_until) VALUES ($1, $2)",
391+
&[&key_str, &valid_until_i64],
392+
)?
393+
} else {
394+
let null_value: Option<i64> = None;
395+
conn.execute(
396+
"INSERT INTO keys (key, valid_until) VALUES ($1, $2)",
397+
&[&key_str, &null_value],
398+
)?
402399
};
403400

404-
Ok(rows_affected as usize)
401+
Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize"))
405402
})
406403
}
407404

408405
fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error> {
409406
let key_str = key.to_string();
410407
self.with_connection(move |conn| {
411408
let rows_affected = conn.execute("DELETE FROM keys WHERE key = $1", &[&key_str])?;
412-
Ok(rows_affected as usize)
409+
Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize"))
413410
})
414411
}
415412
}
@@ -524,7 +521,7 @@ mod tests {
524521
driver
525522
}
526523

527-
/// Runs the full PostgreSQL driver test suite using testcontainers.
524+
/// Runs the full `PostgreSQL` driver test suite using testcontainers.
528525
///
529526
/// Enable with:
530527
/// `TORRUST_TRACKER_CORE_RUN_POSTGRES_DRIVER_TEST=true cargo test`
@@ -555,19 +552,16 @@ mod tests {
555552
Ok(())
556553
}
557554

558-
/// Runs the full PostgreSQL driver test suite against a local PostgreSQL
555+
/// Runs the full `PostgreSQL` driver test suite against a local `PostgreSQL`
559556
/// instance specified via environment variable.
560557
///
561558
/// Enable with:
562559
/// `TORRUST_TRACKER_CORE_POSTGRES_DATABASE_URL="postgresql://user:pass@host:port/db" cargo test`
563560
#[tokio::test]
564561
async fn run_postgres_driver_tests_local() -> Result<(), Box<dyn std::error::Error + 'static>> {
565-
let db_url = match std::env::var("TORRUST_TRACKER_CORE_POSTGRES_DATABASE_URL") {
566-
Ok(url) => url,
567-
Err(_) => {
568-
println!("Skipping the local PostgreSQL driver tests.");
569-
return Ok(());
570-
}
562+
let Ok(db_url) = std::env::var("TORRUST_TRACKER_CORE_POSTGRES_DATABASE_URL") else {
563+
println!("Skipping the local PostgreSQL driver tests.");
564+
return Ok(());
571565
};
572566

573567
let mut config = Core::default();

packages/tracker-core/src/databases/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub enum Error {
8080

8181
/// Indicates a failure to connect to the database (generic).
8282
///
83-
/// This error variant wraps connection-related errors for drivers that do not use MySQL URL errors.
83+
/// This error variant wraps connection-related errors for drivers that do not use `MySQL` URL errors.
8484
#[error("Failed to connect to {driver} database: {source}")]
8585
GenericConnectionError {
8686
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,

0 commit comments

Comments
 (0)