Skip to content

Commit 75bef77

Browse files
committed
clippy: fix src/http/handlers.rs
1 parent b5ce7e9 commit 75bef77

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/http/handlers.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,38 @@ use crate::tracker::torrent::{TorrentError, TorrentStats};
1919
use crate::tracker::TorrentTracker;
2020

2121
/// Authenticate `InfoHash` using optional `AuthKey`
22+
///
23+
/// # Errors
24+
///
25+
/// Will return `ServerError` that wraps the `TorrentError` if unable to `authenticate_request`.
2226
pub async fn authenticate(
2327
info_hash: &InfoHash,
2428
auth_key: &Option<AuthKey>,
2529
tracker: Arc<TorrentTracker>,
2630
) -> Result<(), ServerError> {
27-
match tracker.authenticate_request(info_hash, auth_key).await {
28-
Ok(_) => Ok(()),
29-
Err(e) => {
30-
let err = match e {
31-
TorrentError::TorrentNotWhitelisted => ServerError::TorrentNotWhitelisted,
32-
TorrentError::PeerNotAuthenticated => ServerError::PeerNotAuthenticated,
33-
TorrentError::PeerKeyNotValid => ServerError::PeerKeyNotValid,
34-
TorrentError::NoPeersFound => ServerError::NoPeersFound,
35-
TorrentError::CouldNotSendResponse => ServerError::InternalServerError,
36-
TorrentError::InvalidInfoHash => ServerError::InvalidInfoHash,
37-
};
38-
39-
Err(err)
40-
}
41-
}
31+
tracker.authenticate_request(info_hash, auth_key).await.map_err(|e| match e {
32+
TorrentError::TorrentNotWhitelisted => ServerError::TorrentNotWhitelisted,
33+
TorrentError::PeerNotAuthenticated => ServerError::PeerNotAuthenticated,
34+
TorrentError::PeerKeyNotValid => ServerError::PeerKeyNotValid,
35+
TorrentError::NoPeersFound => ServerError::NoPeersFound,
36+
TorrentError::CouldNotSendResponse => ServerError::InternalServerError,
37+
TorrentError::InvalidInfoHash => ServerError::InvalidInfoHash,
38+
})
4239
}
4340

4441
/// Handle announce request
42+
///
43+
/// # Errors
44+
///
45+
/// Will return `warp::Rejection` that wraps the `ServerError` if unable to `send_scrape_response`.
4546
pub async fn handle_announce(
4647
announce_request: request::Announce,
4748
auth_key: Option<AuthKey>,
4849
tracker: Arc<TorrentTracker>,
4950
) -> WebResult<impl Reply> {
50-
if let Err(e) = authenticate(&announce_request.info_hash, &auth_key, tracker.clone()).await {
51-
return Err(reject::custom(e));
52-
}
51+
authenticate(&announce_request.info_hash, &auth_key, tracker.clone())
52+
.await
53+
.map_err(reject::custom)?;
5354

5455
debug!("{:?}", announce_request);
5556

@@ -76,14 +77,18 @@ pub async fn handle_announce(
7677

7778
send_announce_response(
7879
&announce_request,
79-
torrent_stats,
80-
peers,
80+
&torrent_stats,
81+
&peers,
8182
announce_interval,
8283
tracker.config.min_announce_interval,
8384
)
8485
}
8586

8687
/// Handle scrape request
88+
///
89+
/// # Errors
90+
///
91+
/// Will return `warp::Rejection` that wraps the `ServerError` if unable to `send_scrape_response`.
8792
pub async fn handle_scrape(
8893
scrape_request: request::Scrape,
8994
auth_key: Option<AuthKey>,
@@ -134,10 +139,11 @@ pub async fn handle_scrape(
134139
}
135140

136141
/// Send announce response
142+
#[allow(clippy::ptr_arg)]
137143
fn send_announce_response(
138144
announce_request: &request::Announce,
139-
torrent_stats: TorrentStats,
140-
peers: Vec<TorrentPeer>,
145+
torrent_stats: &TorrentStats,
146+
peers: &Vec<TorrentPeer>,
141147
interval: u32,
142148
interval_min: u32,
143149
) -> WebResult<impl Reply> {
@@ -180,7 +186,11 @@ fn send_scrape_response(files: HashMap<InfoHash, ScrapeResponseEntry>) -> WebRes
180186
}
181187

182188
/// Handle all server errors and send error reply
183-
pub async fn send_error(r: Rejection) -> std::result::Result<impl Reply, Infallible> {
189+
///
190+
/// # Errors
191+
///
192+
/// Will not return a error, `Infallible`, but instead convert the `ServerError` into a `Response`.
193+
pub fn send_error(r: &Rejection) -> std::result::Result<impl Reply, Infallible> {
184194
let body = if let Some(server_error) = r.find::<ServerError>() {
185195
debug!("{:?}", server_error);
186196
Error {

src/http/routes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ use super::handlers::{handle_announce, handle_scrape, send_error};
88
use crate::tracker::TorrentTracker;
99

1010
/// All routes
11+
#[must_use]
1112
pub fn routes(tracker: Arc<TorrentTracker>) -> impl Filter<Extract = impl warp::Reply, Error = Infallible> + Clone {
12-
announce(tracker.clone()).or(scrape(tracker)).recover(send_error)
13+
announce(tracker.clone())
14+
.or(scrape(tracker))
15+
.recover(|q| async move { send_error(&q) })
1316
}
1417

1518
/// GET /announce or /announce/<key>

0 commit comments

Comments
 (0)