Skip to content

Commit 4a70ee0

Browse files
committed
dev: apply clippy auto-fixes
1 parent f1270ff commit 4a70ee0

File tree

23 files changed

+106
-74
lines changed

23 files changed

+106
-74
lines changed

src/cache/cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct BytesCache {
2727
}
2828

2929
impl BytesCache {
30+
#[must_use]
3031
pub fn new() -> Self {
3132
Self {
3233
bytes_table: IndexMap::new(),
@@ -36,6 +37,7 @@ impl BytesCache {
3637
}
3738

3839
// With a total capacity in bytes.
40+
#[must_use]
3941
pub fn with_capacity(capacity: usize) -> Self {
4042
let mut new = Self::new();
4143

@@ -45,6 +47,7 @@ impl BytesCache {
4547
}
4648

4749
// With a limit for individual entry sizes.
50+
#[must_use]
4851
pub fn with_entry_size_limit(entry_size_limit: usize) -> Self {
4952
let mut new = Self::new();
5053

@@ -77,6 +80,7 @@ impl BytesCache {
7780
}
7881

7982
// Size of all the entry bytes combined.
83+
#[must_use]
8084
pub fn total_size(&self) -> usize {
8185
let mut size: usize = 0;
8286

src/cache/image/manager.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum Error {
1919

2020
type UserQuotas = HashMap<i64, ImageCacheQuota>;
2121

22+
#[must_use]
2223
pub fn now_in_secs() -> u64 {
2324
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
2425
Ok(n) => n.as_secs(),
@@ -36,6 +37,7 @@ pub struct ImageCacheQuota {
3637
}
3738

3839
impl ImageCacheQuota {
40+
#[must_use]
3941
pub fn new(user_id: i64, max_usage: usize, period_secs: u64) -> Self {
4042
Self {
4143
user_id,
@@ -66,6 +68,7 @@ impl ImageCacheQuota {
6668
self.date_start_secs = now_in_secs();
6769
}
6870

71+
#[must_use]
6972
pub fn is_reached(&self) -> bool {
7073
self.usage >= self.max_usage
7174
}

src/console/commands/import_tracker_statistics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::env;
44
use std::sync::Arc;
55

66
use derive_more::{Display, Error};
7-
use text_colorizer::*;
7+
use text_colorizer::Colorize;
88

99
use crate::bootstrap::config::init_configuration;
1010
use crate::bootstrap::logging;

src/databases/mysql.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl Database for MysqlDatabase {
467467
// flatten the nested vec (this will however remove the)
468468
let announce_urls = announce_urls.iter().flatten().collect::<Vec<&String>>();
469469

470-
for tracker_url in announce_urls.iter() {
470+
for tracker_url in &announce_urls {
471471
let _ = query("INSERT INTO torrust_torrent_announce_urls (torrent_id, tracker_url) VALUES (?, ?)")
472472
.bind(torrent_id)
473473
.bind(tracker_url)
@@ -520,7 +520,7 @@ impl Database for MysqlDatabase {
520520
match insert_torrent_info_result {
521521
Ok(_) => {
522522
let _ = tx.commit().await;
523-
Ok(torrent_id as i64)
523+
Ok(torrent_id)
524524
}
525525
Err(e) => {
526526
let _ = tx.rollback().await;
@@ -560,7 +560,12 @@ impl Database for MysqlDatabase {
560560
let torrent_files: Vec<TorrentFile> = db_torrent_files
561561
.into_iter()
562562
.map(|tf| TorrentFile {
563-
path: tf.path.unwrap_or_default().split('/').map(|v| v.to_string()).collect(),
563+
path: tf
564+
.path
565+
.unwrap_or_default()
566+
.split('/')
567+
.map(std::string::ToString::to_string)
568+
.collect(),
564569
length: tf.length,
565570
md5sum: tf.md5sum,
566571
})

src/databases/sqlite.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl Database for SqliteDatabase {
9292
match insert_user_profile_result {
9393
Ok(_) => {
9494
let _ = tx.commit().await;
95-
Ok(user_id as i64)
95+
Ok(user_id)
9696
}
9797
Err(e) => {
9898
let _ = tx.rollback().await;
@@ -410,7 +410,7 @@ impl Database for SqliteDatabase {
410410
.bind(root_hash)
411411
.execute(&self.pool)
412412
.await
413-
.map(|v| v.last_insert_rowid() as i64)
413+
.map(|v| v.last_insert_rowid())
414414
.map_err(|e| match e {
415415
sqlx::Error::Database(err) => {
416416
if err.message().contains("info_hash") {
@@ -462,7 +462,7 @@ impl Database for SqliteDatabase {
462462
// flatten the nested vec (this will however remove the)
463463
let announce_urls = announce_urls.iter().flatten().collect::<Vec<&String>>();
464464

465-
for tracker_url in announce_urls.iter() {
465+
for tracker_url in &announce_urls {
466466
let _ = query("INSERT INTO torrust_torrent_announce_urls (torrent_id, tracker_url) VALUES (?, ?)")
467467
.bind(torrent_id)
468468
.bind(tracker_url)
@@ -515,7 +515,7 @@ impl Database for SqliteDatabase {
515515
match insert_torrent_info_result {
516516
Ok(_) => {
517517
let _ = tx.commit().await;
518-
Ok(torrent_id as i64)
518+
Ok(torrent_id)
519519
}
520520
Err(e) => {
521521
let _ = tx.rollback().await;
@@ -555,7 +555,12 @@ impl Database for SqliteDatabase {
555555
let torrent_files: Vec<TorrentFile> = db_torrent_files
556556
.into_iter()
557557
.map(|tf| TorrentFile {
558-
path: tf.path.unwrap_or_default().split('/').map(|v| v.to_string()).collect(),
558+
path: tf
559+
.path
560+
.unwrap_or_default()
561+
.split('/')
562+
.map(std::string::ToString::to_string)
563+
.collect(),
559564
length: tf.length,
560565
md5sum: tf.md5sum,
561566
})

src/errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl ResponseError for ServiceError {
192192

193193
impl From<sqlx::Error> for ServiceError {
194194
fn from(e: sqlx::Error) -> Self {
195-
eprintln!("{:?}", e);
195+
eprintln!("{e:?}");
196196

197197
if let Some(err) = e.as_database_error() {
198198
return if err.code() == Some(Cow::from("2067")) {
@@ -229,28 +229,28 @@ impl From<DatabaseError> for ServiceError {
229229

230230
impl From<argon2::password_hash::Error> for ServiceError {
231231
fn from(e: argon2::password_hash::Error) -> Self {
232-
eprintln!("{}", e);
232+
eprintln!("{e}");
233233
ServiceError::InternalServerError
234234
}
235235
}
236236

237237
impl From<std::io::Error> for ServiceError {
238238
fn from(e: std::io::Error) -> Self {
239-
eprintln!("{}", e);
239+
eprintln!("{e}");
240240
ServiceError::InternalServerError
241241
}
242242
}
243243

244244
impl From<Box<dyn error::Error>> for ServiceError {
245245
fn from(e: Box<dyn error::Error>) -> Self {
246-
eprintln!("{}", e);
246+
eprintln!("{e}");
247247
ServiceError::InternalServerError
248248
}
249249
}
250250

251251
impl From<serde_json::Error> for ServiceError {
252252
fn from(e: serde_json::Error) -> Self {
253-
eprintln!("{}", e);
253+
eprintln!("{e}");
254254
ServiceError::InternalServerError
255255
}
256256
}

src/models/response.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct TorrentResponse {
4848
}
4949

5050
impl TorrentResponse {
51+
#[must_use]
5152
pub fn from_listing(torrent_listing: TorrentListing) -> TorrentResponse {
5253
TorrentResponse {
5354
torrent_id: torrent_listing.torrent_id,
@@ -57,7 +58,7 @@ impl TorrentResponse {
5758
description: torrent_listing.description,
5859
category: Category {
5960
category_id: 0,
60-
name: "".to_string(),
61+
name: String::new(),
6162
num_torrents: 0,
6263
},
6364
upload_date: torrent_listing.date_uploaded,
@@ -66,7 +67,7 @@ impl TorrentResponse {
6667
leechers: torrent_listing.leechers,
6768
files: vec![],
6869
trackers: vec![],
69-
magnet_link: "".to_string(),
70+
magnet_link: String::new(),
7071
}
7172
}
7273
}

src/models/torrent_file.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,28 @@ pub struct TorrentInfo {
4242
impl TorrentInfo {
4343
/// torrent file can only hold a pieces key or a root hash key:
4444
/// http://www.bittorrent.org/beps/bep_0030.html
45+
#[must_use]
4546
pub fn get_pieces_as_string(&self) -> String {
4647
match &self.pieces {
47-
None => "".to_string(),
48+
None => String::new(),
4849
Some(byte_buf) => bytes_to_hex(byte_buf.as_ref()),
4950
}
5051
}
5152

53+
#[must_use]
5254
pub fn get_root_hash_as_i64(&self) -> i64 {
5355
match &self.root_hash {
5456
None => 0i64,
5557
Some(root_hash) => root_hash.parse::<i64>().unwrap(),
5658
}
5759
}
5860

61+
#[must_use]
5962
pub fn is_a_single_file_torrent(&self) -> bool {
6063
self.length.is_some()
6164
}
6265

66+
#[must_use]
6367
pub fn is_a_multiple_file_torrent(&self) -> bool {
6468
self.files.is_some()
6569
}
@@ -90,6 +94,7 @@ pub struct Torrent {
9094
}
9195

9296
impl Torrent {
97+
#[must_use]
9398
pub fn from_db_info_files_and_announce_urls(
9499
torrent_info: DbTorrentInfo,
95100
torrent_files: Vec<TorrentFile>,
@@ -170,6 +175,7 @@ impl Torrent {
170175
}
171176
}
172177

178+
#[must_use]
173179
pub fn calculate_info_hash_as_bytes(&self) -> [u8; 20] {
174180
let info_bencoded = ser::to_bytes(&self.info).unwrap();
175181
let mut hasher = Sha1::new();
@@ -180,10 +186,12 @@ impl Torrent {
180186
sum_bytes
181187
}
182188

189+
#[must_use]
183190
pub fn info_hash(&self) -> String {
184191
bytes_to_hex(&self.calculate_info_hash_as_bytes())
185192
}
186193

194+
#[must_use]
187195
pub fn file_size(&self) -> i64 {
188196
if self.info.length.is_some() {
189197
self.info.length.unwrap()
@@ -201,6 +209,7 @@ impl Torrent {
201209
}
202210
}
203211

212+
#[must_use]
204213
pub fn announce_urls(&self) -> Vec<String> {
205214
if self.announce_list.is_none() {
206215
return vec![self.announce.clone().unwrap()];
@@ -214,10 +223,12 @@ impl Torrent {
214223
.collect::<Vec<String>>()
215224
}
216225

226+
#[must_use]
217227
pub fn is_a_single_file_torrent(&self) -> bool {
218228
self.info.is_a_single_file_torrent()
219229
}
220230

231+
#[must_use]
221232
pub fn is_a_multiple_file_torrent(&self) -> bool {
222233
self.info.is_a_multiple_file_torrent()
223234
}

src/routes/torrent.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,9 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<Torr
370370
let torrent_buffer = vec![0u8];
371371
let mut torrent_cursor = Cursor::new(torrent_buffer);
372372

373-
let mut title = "".to_string();
374-
let mut description = "".to_string();
375-
let mut category = "".to_string();
373+
let mut title = String::new();
374+
let mut description = String::new();
375+
let mut category = String::new();
376376

377377
while let Ok(Some(mut field)) = payload.try_next().await {
378378
match field.content_disposition().get_name().unwrap() {

src/routes/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub async fn register(req: HttpRequest, mut payload: web::Json<Register>, app_da
9696
return Err(ServiceError::UsernameInvalid);
9797
}
9898

99-
let email = payload.email.as_ref().unwrap_or(&"".to_string()).to_string();
99+
let email = payload.email.as_ref().unwrap_or(&String::new()).to_string();
100100

101101
let user_id = app_data
102102
.database

0 commit comments

Comments
 (0)