use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use recall_wire::ErrorResponse;
use serde::Serialize;
pub(super) fn json<T: Serialize>(status: StatusCode, body: &T) -> Response {
(status, Json(body)).into_response()
}
pub(super) fn error(status: StatusCode, message: &str) -> Response {
json(
status,
&ErrorResponse {
error: message.to_string(),
},
)
}
pub(super) fn internal(e: anyhow::Error) -> Response {
error(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Refusal {
status: StatusCode,
message: String,
}
impl Refusal {
pub(super) fn new(status: StatusCode, message: impl Into<String>) -> Self {
Self {
status,
message: message.into(),
}
}
pub(super) fn internal(e: anyhow::Error) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
}
}
impl IntoResponse for Refusal {
fn into_response(self) -> Response {
error(self.status, &self.message)
}
}