use axum::{
http::{HeaderMap, HeaderName, HeaderValue, StatusCode, header},
response::{IntoResponse, Response as AxumResponse},
};
pub mod builders;
pub mod stream;
pub use builders::{bytes, html, json, no_content, redirect, text};
pub use stream::stream;
#[derive(Debug, Clone)]
pub struct Response {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Vec<u8>,
}
impl Response {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn status(mut self, status: StatusCode) -> Self {
self.status = status;
self
}
#[must_use]
pub fn header(mut self, key: &str, value: &str) -> Self {
if let Ok(name) = HeaderName::try_from(key)
&& let Ok(v) = HeaderValue::try_from(value)
{
self.headers.insert(name, v);
}
self
}
pub fn content_type(mut self, content_type: &str) -> Self {
if let Ok(v) = HeaderValue::try_from(content_type) {
self.headers.insert(header::CONTENT_TYPE, v);
}
self
}
pub fn into_axum_response(mut self) -> AxumResponse {
if !self.headers.contains_key(header::SERVER) {
self.headers
.insert(header::SERVER, HeaderValue::from_static("Astrea"));
}
(self.status, self.headers, self.body).into_response()
}
}
impl Default for Response {
fn default() -> Self {
Self {
status: StatusCode::OK,
headers: HeaderMap::new(),
body: Vec::new(),
}
}
}
impl IntoResponse for Response {
fn into_response(self) -> AxumResponse {
self.into_axum_response()
}
}