Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add struct Accepted as a shortcut #3224

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions axum/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ impl IntoResponse for NoContent {
}
}

/// An empty response with 202 Accepted status.
///
/// Due to historical and implementation reasons, the `IntoResponse` implementation of `()`
/// (unit type) returns an empty response with 200 [`StatusCode::OK`] status.
/// If you specifically want a 202 [`StatusCode::ACCEPTED`] status, you can use either `StatusCode` type
/// directly, or this shortcut struct for self-documentation.
///
/// ```
/// use axum::{extract::Path, response::Accepted};
///
/// async fn delete_user(Path(user): Path<String>) -> Result<Accepted, String> {
/// // ...access database...
/// # drop(user);
/// Ok(Accepted)
/// }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Accepted;

impl IntoResponse for Accepted {
fn into_response(self) -> Response {
StatusCode::ACCEPTED.into_response()
}
}

#[cfg(test)]
mod tests {
use crate::extract::Extension;
Expand Down Expand Up @@ -256,4 +281,12 @@ mod tests {
StatusCode::NO_CONTENT,
)
}

#[test]
fn accepted() {
assert_eq!(
super::Accepted.into_response().status(),
StatusCode::ACCEPTED,
)
}
}
Loading