Skip to content

Commit 7652fdf

Browse files
committed
Replace ok_true() fn with OkResponse struct
This will allow us to use the struct in our OpenAPI description.
1 parent 8b1508f commit 7652fdf

File tree

6 files changed

+47
-31
lines changed

6 files changed

+47
-31
lines changed

src/controllers/helpers.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
use crate::util::errors::AppResult;
1+
use axum::Json;
22
use axum::response::{IntoResponse, Response};
3-
use axum_extra::json;
43

54
pub mod authorization;
65
pub(crate) mod pagination;
76

87
pub(crate) use self::pagination::Paginate;
98

10-
pub fn ok_true() -> AppResult<Response> {
11-
let json = json!({ "ok": true });
12-
Ok(json.into_response())
9+
#[derive(Debug, Serialize, utoipa::ToSchema)]
10+
pub struct OkResponse {
11+
#[schema(example = true)]
12+
ok: bool,
13+
}
14+
15+
impl Default for OkResponse {
16+
fn default() -> Self {
17+
Self::new()
18+
}
19+
}
20+
21+
impl OkResponse {
22+
pub fn new() -> Self {
23+
Self { ok: true }
24+
}
25+
}
26+
27+
impl IntoResponse for OkResponse {
28+
fn into_response(self) -> Response {
29+
Json(self).into_response()
30+
}
1331
}

src/controllers/krate/follow.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
33
use crate::app::AppState;
44
use crate::auth::AuthCheck;
5-
use crate::controllers::helpers::ok_true;
5+
use crate::controllers::helpers::OkResponse;
66
use crate::controllers::krate::CratePath;
77
use crate::models::{Crate, Follow};
88
use crate::schema::*;
99
use crate::util::errors::{AppResult, crate_not_found};
10-
use axum::response::Response;
1110
use axum_extra::json;
1211
use axum_extra::response::ErasedJson;
1312
use diesel::prelude::*;
@@ -41,7 +40,7 @@ async fn follow_target(
4140
tag = "crates",
4241
responses((status = 200, description = "Successful Response")),
4342
)]
44-
pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<Response> {
43+
pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<OkResponse> {
4544
let mut conn = app.db_write().await?;
4645
let user_id = AuthCheck::default().check(&req, &mut conn).await?.user_id();
4746
let follow = follow_target(&path.name, &mut conn, user_id).await?;
@@ -51,7 +50,7 @@ pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResu
5150
.execute(&mut conn)
5251
.await?;
5352

54-
ok_true()
53+
Ok(OkResponse::new())
5554
}
5655

5756
/// Unfollow a crate.
@@ -66,13 +65,13 @@ pub async fn follow_crate(app: AppState, path: CratePath, req: Parts) -> AppResu
6665
tag = "crates",
6766
responses((status = 200, description = "Successful Response")),
6867
)]
69-
pub async fn unfollow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<Response> {
68+
pub async fn unfollow_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<OkResponse> {
7069
let mut conn = app.db_write().await?;
7170
let user_id = AuthCheck::default().check(&req, &mut conn).await?.user_id();
7271
let follow = follow_target(&path.name, &mut conn, user_id).await?;
7372
diesel::delete(&follow).execute(&mut conn).await?;
7473

75-
ok_true()
74+
Ok(OkResponse::new())
7675
}
7776

7877
/// Check if a crate is followed.

src/controllers/user/email_notifications.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::app::AppState;
22
use crate::auth::AuthCheck;
3-
use crate::controllers::helpers::ok_true;
3+
use crate::controllers::helpers::OkResponse;
44
use crate::models::{CrateOwner, OwnerKind};
55
use crate::schema::crate_owners;
66
use crate::util::errors::AppResult;
77
use axum::Json;
8-
use axum::response::Response;
98
use diesel::prelude::*;
109
use diesel_async::RunQueryDsl;
1110
use http::request::Parts;
@@ -36,7 +35,7 @@ pub async fn update_email_notifications(
3635
app: AppState,
3736
parts: Parts,
3837
Json(updates): Json<Vec<CrateEmailNotifications>>,
39-
) -> AppResult<Response> {
38+
) -> AppResult<OkResponse> {
4039
use diesel::pg::upsert::excluded;
4140

4241
let updates: HashMap<i32, bool> = updates
@@ -89,5 +88,5 @@ pub async fn update_email_notifications(
8988
.execute(&mut conn)
9089
.await?;
9190

92-
ok_true()
91+
Ok(OkResponse::new())
9392
}

src/controllers/user/email_verification.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use super::update::UserConfirmEmail;
22
use crate::app::AppState;
33
use crate::auth::AuthCheck;
4-
use crate::controllers::helpers::ok_true;
4+
use crate::controllers::helpers::OkResponse;
55
use crate::models::Email;
66
use crate::util::errors::AppResult;
77
use crate::util::errors::{BoxedAppError, bad_request};
88
use axum::extract::Path;
9-
use axum::response::Response;
109
use crates_io_database::schema::emails;
1110
use diesel::dsl::sql;
1211
use diesel::prelude::*;
@@ -24,7 +23,10 @@ use http::request::Parts;
2423
tag = "users",
2524
responses((status = 200, description = "Successful Response")),
2625
)]
27-
pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> AppResult<Response> {
26+
pub async fn confirm_user_email(
27+
state: AppState,
28+
Path(token): Path<String>,
29+
) -> AppResult<OkResponse> {
2830
let mut conn = state.db_write().await?;
2931

3032
let updated_rows = diesel::update(emails::table.filter(emails::token.eq(&token)))
@@ -36,7 +38,7 @@ pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> A
3638
return Err(bad_request("Email belonging to token not found."));
3739
}
3840

39-
ok_true()
41+
Ok(OkResponse::new())
4042
}
4143

4244
/// Regenerate and send an email verification token.
@@ -57,7 +59,7 @@ pub async fn resend_email_verification(
5759
state: AppState,
5860
Path(param_user_id): Path<i32>,
5961
req: Parts,
60-
) -> AppResult<Response> {
62+
) -> AppResult<OkResponse> {
6163
let mut conn = state.db_write().await?;
6264
let auth = AuthCheck::default().check(&req, &mut conn).await?;
6365

@@ -91,7 +93,7 @@ pub async fn resend_email_verification(
9193
})
9294
.await?;
9395

94-
ok_true()
96+
Ok(OkResponse::new())
9597
}
9698

9799
#[cfg(test)]

src/controllers/user/update.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::app::AppState;
22
use crate::auth::AuthCheck;
3-
use crate::controllers::helpers::ok_true;
3+
use crate::controllers::helpers::OkResponse;
44
use crate::models::NewEmail;
55
use crate::schema::users;
66
use crate::util::errors::{AppResult, bad_request, server_error};
77
use axum::Json;
88
use axum::extract::Path;
9-
use axum::response::Response;
109
use diesel::prelude::*;
1110
use diesel_async::RunQueryDsl;
1211
use http::request::Parts;
@@ -47,7 +46,7 @@ pub async fn update_user(
4746
Path(param_user_id): Path<i32>,
4847
req: Parts,
4948
Json(user_update): Json<UserUpdate>,
50-
) -> AppResult<Response> {
49+
) -> AppResult<OkResponse> {
5150
let mut conn = state.db_write().await?;
5251
let auth = AuthCheck::default().check(&req, &mut conn).await?;
5352

@@ -116,7 +115,7 @@ pub async fn update_user(
116115
let _ = state.emails.send(user_email, email).await;
117116
}
118117

119-
ok_true()
118+
Ok(OkResponse::new())
120119
}
121120

122121
pub struct UserConfirmEmail<'a> {

src/controllers/version/yank.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
use super::CrateVersionPath;
44
use super::update::{authenticate, perform_version_yank_update};
55
use crate::app::AppState;
6-
use crate::controllers::helpers::ok_true;
6+
use crate::controllers::helpers::OkResponse;
77
use crate::rate_limiter::LimitedAction;
88
use crate::util::errors::AppResult;
9-
use axum::response::Response;
109
use http::request::Parts;
1110

1211
/// Yank a crate version.
@@ -35,7 +34,7 @@ pub async fn yank_version(
3534
app: AppState,
3635
path: CrateVersionPath,
3736
req: Parts,
38-
) -> AppResult<Response> {
37+
) -> AppResult<OkResponse> {
3938
modify_yank(path, app, req, true).await
4039
}
4140

@@ -55,7 +54,7 @@ pub async fn unyank_version(
5554
app: AppState,
5655
path: CrateVersionPath,
5756
req: Parts,
58-
) -> AppResult<Response> {
57+
) -> AppResult<OkResponse> {
5958
modify_yank(path, app, req, false).await
6059
}
6160

@@ -65,7 +64,7 @@ async fn modify_yank(
6564
state: AppState,
6665
req: Parts,
6766
yanked: bool,
68-
) -> AppResult<Response> {
67+
) -> AppResult<OkResponse> {
6968
// FIXME: Should reject bad requests before authentication, but can't due to
7069
// lifetime issues with `req`.
7170

@@ -89,5 +88,5 @@ async fn modify_yank(
8988
)
9089
.await?;
9190

92-
ok_true()
91+
Ok(OkResponse::new())
9392
}

0 commit comments

Comments
 (0)