Skip to main content

watchword_core/
axum08.rs

1use axum08::Json;
2use axum08::http::StatusCode;
3use axum08::response::{IntoResponse, Response};
4
5use crate::{AuthError, AuthResponse};
6
7/// Answers with the error as its JSON body, such as
8/// `{"error":"conflict","detail":"email"}`, and its
9/// [status](AuthError::status).
10#[cfg_attr(watchword_docsrs, doc(cfg(feature = "axum08")))]
11impl IntoResponse for AuthError {
12    fn into_response(self) -> Response {
13        let status =
14            StatusCode::from_u16(self.status()).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
15        (status, Json(self)).into_response()
16    }
17}
18
19/// Answers with the value the response carries as its JSON body, and
20/// [`Done`](AuthResponse::Done) with 204 No Content, as watchword's routes
21/// do.
22#[cfg_attr(watchword_docsrs, doc(cfg(feature = "axum08")))]
23impl IntoResponse for AuthResponse {
24    fn into_response(self) -> Response {
25        match self {
26            AuthResponse::Config(value) => Json(value).into_response(),
27            AuthResponse::User(value) => Json(value).into_response(),
28            AuthResponse::Users(value) => Json(value).into_response(),
29            AuthResponse::CreatedUser(value) => Json(value).into_response(),
30            AuthResponse::Link(value) => Json(value).into_response(),
31            AuthResponse::RegisterInfo(value) => Json(value).into_response(),
32            AuthResponse::Cloudflare(value) => Json(value).into_response(),
33            AuthResponse::Done => StatusCode::NO_CONTENT.into_response(),
34        }
35    }
36}