use axum::http::{HeaderValue, StatusCode};
use axum::response::{IntoResponse, Response};
use serde::Serialize;
pub struct Created<T> {
pub body: T,
pub location: Option<&'static str>,
}
impl<T> Created<T> {
#[inline]
pub fn new(body: T) -> Self {
Self {
body,
location: None,
}
}
#[inline]
pub fn at(body: T, location: &'static str) -> Self {
Self {
body,
location: Some(location),
}
}
}
impl<T: Serialize> IntoResponse for Created<T> {
fn into_response(self) -> Response {
let mut r = (StatusCode::CREATED, axum::Json(self.body)).into_response();
if let Some(loc) = self.location {
if let Ok(v) = HeaderValue::from_str(loc) {
r.headers_mut().insert("Location", v);
}
}
r
}
}
pub struct NoContent;
impl IntoResponse for NoContent {
fn into_response(self) -> Response {
StatusCode::NO_CONTENT.into_response()
}
}
pub struct Accepted<T>(pub T);
impl<T: Serialize> IntoResponse for Accepted<T> {
fn into_response(self) -> Response {
(StatusCode::ACCEPTED, axum::Json(self.0)).into_response()
}
}