Skip to content

Commit f156246

Browse files
committed
[refactor]: cleanup clippy warnings
1 parent 8d08969 commit f156246

4 files changed

Lines changed: 46 additions & 29 deletions

File tree

cf-worker/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
1515
let headers = req.headers().into();
1616
// Basic auth check
1717
if let Ok(pass) = env.secret("SECRET_KEY") {
18-
if let Err(response) =
18+
if let Err(err) =
1919
shared_lib::auth::check_auth("admin".to_string(), pass.to_string(), &headers)
2020
{
21-
return Response::try_from(response);
21+
return Response::error(err.message, err.status.as_u16());
2222
}
2323
}
2424

shared-lib/src/auth.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,54 @@
1-
use axum::{
2-
body::Body,
3-
http::{header::HeaderMap, Response, StatusCode},
4-
response::IntoResponse,
5-
Json,
6-
};
1+
use axum::http::{header::HeaderMap, StatusCode};
2+
use axum::response::{IntoResponse, Json};
73
use base64::{engine::general_purpose, Engine as _};
84

5+
#[derive(Debug)]
6+
pub struct AuthError {
7+
pub status: StatusCode,
8+
pub message: &'static str,
9+
}
10+
11+
impl AuthError {
12+
pub fn unauthorized(message: &'static str) -> Self {
13+
Self {
14+
status: StatusCode::UNAUTHORIZED,
15+
message,
16+
}
17+
}
18+
19+
pub fn bad_request(message: &'static str) -> Self {
20+
Self {
21+
status: StatusCode::BAD_REQUEST,
22+
message,
23+
}
24+
}
25+
}
26+
27+
impl IntoResponse for AuthError {
28+
fn into_response(self) -> axum::response::Response {
29+
(self.status, Json(self.message)).into_response()
30+
}
31+
}
32+
933
pub fn check_auth(
1034
user_value: String,
1135
pass_value: String,
1236
headers: &HeaderMap,
13-
) -> Result<(), Response<Body>> {
14-
let unauthorized = |msg| (StatusCode::UNAUTHORIZED, Json(msg)).into_response();
15-
let bad_request = |msg| (StatusCode::BAD_REQUEST, Json(msg)).into_response();
16-
37+
) -> Result<(), AuthError> {
1738
let auth_header = headers
1839
.get("Authorization")
19-
.ok_or_else(|| unauthorized("No Authorization header"))?;
40+
.ok_or(AuthError::unauthorized("No Authorization header"))?;
2041
let auth_str = auth_header
2142
.to_str()
22-
.map_err(|_| bad_request("Invalid Authorization header"))?;
43+
.map_err(|_| AuthError::bad_request("Invalid Authorization header"))?;
2344
let auth = auth_str
2445
.strip_prefix("Basic ")
25-
.ok_or_else(|| bad_request("Invalid Authorization header"))?;
46+
.ok_or(AuthError::bad_request("Invalid Authorization header"))?;
2647
let decoded = general_purpose::STANDARD
2748
.decode(auth)
28-
.map_err(|_| bad_request("Invalid Authorization header: couldn't decode base64"))?;
49+
.map_err(|_| AuthError::bad_request("Invalid Authorization header: couldn't decode base64"))?;
2950
let auth = String::from_utf8(decoded).map_err(|_| {
30-
bad_request("Invalid Authorization header: couldn't convert dec/* o */ded utf8 to string")
51+
AuthError::bad_request("Invalid Authorization header: couldn't convert decoded utf8 to string")
3152
})?;
3253

3354
let mut auth_parts = auth.splitn(2, ':');
@@ -37,7 +58,7 @@ pub fn check_auth(
3758
);
3859

3960
if user != user_value || pass != pass_value {
40-
return Err(unauthorized(
61+
return Err(AuthError::unauthorized(
4162
"Invalid Authorization header: incorrect username or password",
4263
));
4364
}

shared-lib/src/structs/discord.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl From<&Vec<SonarrRequestBody>> for DiscordWebhookBody {
4848
sonarr_data[0].episodes[0].episode_number,
4949
sonarr_data[0].episodes[0].title
5050
),
51-
_ => format!("{}: {} Season {:02}", content, series_title, season_number),
51+
_ => format!("{content}: {series_title} Season {season_number:02}"),
5252
};
5353

5454
let mut episodes_with_quality: Vec<_> = sonarr_data
@@ -93,13 +93,9 @@ impl From<&Vec<SonarrRequestBody>> for DiscordWebhookBody {
9393
.into_iter()
9494
.map(
9595
|(season_number, episode_number, title, quality, count)| match count {
96-
1 => format!(
97-
"{:02}x{:02} - {} [{}]",
98-
season_number, episode_number, title, quality
99-
),
96+
1 => format!("{season_number:02}x{episode_number:02} - {title} [{quality}]"),
10097
_ => format!(
101-
"{:02}x{:02} - {} [{}] ({}x)",
102-
season_number, episode_number, title, quality, count
98+
"{season_number:02}x{episode_number:02} - {title} [{quality}] ({count}x)"
10399
),
104100
},
105101
)

standalone/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async fn main() {
6666
let milliseconds = latency.as_secs_f64() * 1000.0
6767
+ latency.subsec_nanos() as f64 / 1_000_000.0;
6868
// Format the milliseconds to a string with 2 decimal places and add 'ms' postfix
69-
format!("{:.2}ms", milliseconds)
69+
format!("{milliseconds:.2}ms")
7070
};
7171

7272
if url == "/healthcheck" {
@@ -85,7 +85,7 @@ async fn main() {
8585
let server_port = env::get_server_port();
8686
tracing::info!("Server started at localhost:{}", server_port);
8787

88-
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", server_port))
88+
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{server_port}"))
8989
.await
9090
.unwrap();
9191
axum::serve(listener, app).await.unwrap();
@@ -102,8 +102,8 @@ async fn handle_post(
102102
std::env::var("HOOKBUFFER_USER"),
103103
std::env::var("HOOKBUFFER_PASS"),
104104
) {
105-
if let Err(response) = shared_lib::auth::check_auth(user, pass, &headers) {
106-
return response;
105+
if let Err(err) = shared_lib::auth::check_auth(user, pass, &headers) {
106+
return err.into_response();
107107
}
108108
}
109109

0 commit comments

Comments
 (0)