Replace mime type "application/ron" by "text/ron" and put it in the common crate

This commit is contained in:
Greg Burri 2025-05-09 23:45:32 +02:00
parent f9899c1aeb
commit 1c2e910dea
7 changed files with 96 additions and 129 deletions

View file

@ -22,12 +22,12 @@ where
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
match req.headers().get(header::CONTENT_TYPE) {
Some(content_type) => {
if content_type != ron_utils::RON_CONTENT_TYPE {
if content_type != common::consts::MIME_TYPE_RON {
return Err(ron_utils::ron_error(
StatusCode::BAD_REQUEST,
&format!(
"Invalid content type, must be {:?}",
ron_utils::RON_CONTENT_TYPE
common::consts::MIME_TYPE_RON
),
)
.into_response());

View file

@ -1,6 +1,6 @@
use axum::{
body::Bytes,
http::{HeaderValue, StatusCode, header},
http::{StatusCode, header},
response::{ErrorResponse, IntoResponse, Response},
};
use common::ron_api;
@ -9,8 +9,6 @@ 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,
@ -21,7 +19,7 @@ impl axum::response::IntoResponse for RonError {
match ron_api::to_string(&self) {
Ok(ron_as_str) => (
StatusCode::BAD_REQUEST,
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
[(header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)],
ron_as_str,
)
.into_response(),
@ -39,7 +37,7 @@ impl From<RonError> for Response {
pub fn ron_error(status: StatusCode, message: &str) -> impl IntoResponse {
(
status,
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
[(header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)],
RonError {
error: message.to_string(),
},
@ -67,7 +65,7 @@ where
match ron_api::to_string(&ron) {
Ok(ron_as_str) => (
status,
[(header::CONTENT_TYPE, RON_CONTENT_TYPE)],
[(header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)],
ron_as_str,
)
.into_response(),

View file

@ -31,7 +31,7 @@ pub async fn ron_error_to_html(
let response = next.run(req).await;
if let Some(content_type) = response.headers().get(header::CONTENT_TYPE) {
if content_type == ron_utils::RON_CONTENT_TYPE {
if content_type == common::consts::MIME_TYPE_RON {
let message = match body::to_bytes(response.into_body(), usize::MAX).await {
Ok(bytes) => String::from_utf8(bytes.to_vec()).unwrap_or_default(),
Err(error) => error.to_string(),