74 lines
1.6 KiB
Rust
74 lines
1.6 KiB
Rust
use axum::{
|
|
body::Bytes,
|
|
http::{header, HeaderValue, StatusCode},
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use common::ron_api;
|
|
use ron::de::from_bytes;
|
|
use serde::{de::DeserializeOwned, Serialize};
|
|
|
|
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 {
|
|
let ron_as_str = ron_api::to_string(&self);
|
|
(
|
|
StatusCode::BAD_REQUEST,
|
|
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
|
|
ron_as_str,
|
|
)
|
|
.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_response_ok<T>(ron: T) -> impl IntoResponse
|
|
where
|
|
T: Serialize,
|
|
{
|
|
ron_response(StatusCode::OK, ron)
|
|
}
|
|
|
|
pub fn ron_response<T>(status: StatusCode, ron: T) -> impl IntoResponse
|
|
where
|
|
T: Serialize,
|
|
{
|
|
let ron_as_str = ron_api::to_string(&ron);
|
|
(
|
|
status,
|
|
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
|
|
ron_as_str,
|
|
)
|
|
}
|
|
|
|
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),
|
|
}),
|
|
}
|
|
}
|