Initial commit
This commit is contained in:
commit
bd9b07052b
81 changed files with 5516 additions and 0 deletions
64
http/src/server/response/mod.rs
Normal file
64
http/src/server/response/mod.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response as AxumResponse},
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::format::{DumpParams, Format};
|
||||
use crate::requests::{IsResponse, status_code::HasStatusCode};
|
||||
|
||||
macro_rules! header {
|
||||
($name:expr => $value:expr) => {
|
||||
(
|
||||
axum::http::HeaderName::from_static($name),
|
||||
axum::http::HeaderValue::from_static($value),
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub fn err<E: IsResponse>(format: Format, error: E) -> AxumResponse {
|
||||
#[derive(Serialize)]
|
||||
struct Failure<E> {
|
||||
pub error: E,
|
||||
}
|
||||
|
||||
impl<T: HasStatusCode> HasStatusCode for Failure<T> {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
self.error.status_code()
|
||||
}
|
||||
}
|
||||
|
||||
respond(format, Failure { error })
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub fn ok<O: IsResponse>(format: Format, ok: O) -> AxumResponse {
|
||||
#[derive(Serialize)]
|
||||
struct Success<T> {
|
||||
ok: T,
|
||||
}
|
||||
|
||||
impl<T: HasStatusCode> HasStatusCode for Success<T> {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
self.ok.status_code()
|
||||
}
|
||||
}
|
||||
|
||||
respond(format, Success { ok })
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub fn respond<O: IsResponse>(format: Format, res: O) -> AxumResponse {
|
||||
let status_code = res.status_code();
|
||||
let headers = [
|
||||
header!("Server" => "Kurisu-desu"),
|
||||
header!("Content-Type" => format.mime_type()),
|
||||
];
|
||||
|
||||
let mut body = Vec::with_capacity(128);
|
||||
let dump_params = DumpParams { pretty: false };
|
||||
format.dump(dump_params, &res, &mut body);
|
||||
|
||||
(status_code, headers, body).into_response()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue