* Create a minimalistic toast

* Profile editing (WIP)
This commit is contained in:
Greg Burri 2024-12-04 17:39:56 +01:00
parent 327b2d0a5b
commit 1c79cc890d
25 changed files with 1133 additions and 575 deletions

71
backend/src/ron_utils.rs Normal file
View file

@ -0,0 +1,71 @@
use axum::{
async_trait,
body::Bytes,
extract::{FromRequest, Request},
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<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) => {
return Err(RonError {
error: format!("Ron parsing error: {}", error),
});
}
}
}