88 lines
2.1 KiB
Rust
88 lines
2.1 KiB
Rust
use axum::{
|
|
body::Bytes,
|
|
http::{HeaderValue, StatusCode, header},
|
|
response::{ErrorResponse, IntoResponse, Response},
|
|
};
|
|
use common::ron_api;
|
|
use ron::de::from_bytes;
|
|
use serde::{Serialize, de::DeserializeOwned};
|
|
|
|
use crate::consts;
|
|
|
|
pub const RON_CONTENT_TYPE: HeaderValue = HeaderValue::from_static("application/ron");
|
|
|
|
#[derive(Debug, Serialize, Clone)]
|
|
pub struct RonError {
|
|
pub error: String,
|
|
}
|
|
|
|
impl axum::response::IntoResponse for RonError {
|
|
fn into_response(self) -> Response {
|
|
match ron_api::to_string(&self) {
|
|
Ok(ron_as_str) => (
|
|
StatusCode::BAD_REQUEST,
|
|
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
|
|
ron_as_str,
|
|
)
|
|
.into_response(),
|
|
Err(error) => (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()).into_response(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<RonError> for Response {
|
|
fn from(value: RonError) -> Self {
|
|
value.into_response()
|
|
}
|
|
}
|
|
|
|
pub fn ron_error(status: StatusCode, message: &str) -> impl IntoResponse {
|
|
(
|
|
status,
|
|
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
|
|
RonError {
|
|
error: message.to_string(),
|
|
},
|
|
)
|
|
}
|
|
|
|
pub fn ron_error_not_authorized() -> ErrorResponse {
|
|
ErrorResponse::from(ron_error(
|
|
StatusCode::UNAUTHORIZED,
|
|
consts::NOT_AUTHORIZED_MESSAGE,
|
|
))
|
|
}
|
|
|
|
pub fn ron_response_ok<T>(ron: T) -> impl IntoResponse
|
|
where
|
|
T: Serialize,
|
|
{
|
|
ron_response(StatusCode::OK, ron)
|
|
}
|
|
|
|
pub fn ron_response<T>(status: StatusCode, ron: T) -> Response
|
|
where
|
|
T: Serialize,
|
|
{
|
|
match ron_api::to_string(&ron) {
|
|
Ok(ron_as_str) => (
|
|
status,
|
|
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
|
|
ron_as_str,
|
|
)
|
|
.into_response(),
|
|
Err(error) => (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()).into_response(),
|
|
}
|
|
}
|
|
|
|
pub fn parse_body<T>(body: Bytes) -> Result<T, RonError>
|
|
where
|
|
T: DeserializeOwned,
|
|
{
|
|
match from_bytes::<T>(&body) {
|
|
Ok(ron) => Ok(ron),
|
|
Err(error) => Err(RonError {
|
|
error: format!("Ron parsing error: {}", error),
|
|
}),
|
|
}
|
|
}
|