use axum::http::{header, StatusCode};
use axum::response::{IntoResponse, Response};
use serde::Serialize;
#[derive(Serialize, Debug)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum ActionStatus<'a> {
Ok,
Err { reason: std::borrow::Cow<'a, str> },
}
#[must_use]
pub fn ok_response() -> Response {
(
StatusCode::OK,
[(header::CONTENT_TYPE, "application/json")],
serde_json::to_string(&ActionStatus::Ok).unwrap(),
)
.into_response()
}
#[must_use]
pub fn invalid_info_hash_param_response(info_hash: &str) -> Response {
bad_request_response(&format!(
"Invalid URL: invalid infohash param: string \"{info_hash}\", expected a 40 character long string"
))
}
#[must_use]
pub fn invalid_auth_key_param_response(invalid_key: &str) -> Response {
bad_request_response(&format!("Invalid auth key id param \"{invalid_key}\""))
}
#[must_use]
pub fn bad_request_response(body: &str) -> Response {
(
StatusCode::BAD_REQUEST,
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
body.to_owned(),
)
.into_response()
}
#[must_use]
pub fn unhandled_rejection_response(reason: String) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
format!("Unhandled rejection: {:?}", ActionStatus::Err { reason: reason.into() }),
)
.into_response()
}