use std::fmt::Debug;
use std::ops::Deref;
use http::{HeaderMap, StatusCode};
use crate::api::Body;
#[derive(Debug)]
pub struct Reply<B>
where
B: Body,
{
pub status: StatusCode,
pub headers: HeaderMap,
pub body: B,
}
impl<B: Body> Reply<B> {
#[must_use]
pub fn ok(body: B) -> Self {
Self {
status: StatusCode::OK,
headers: HeaderMap::new(),
body,
}
}
#[must_use]
pub fn created(body: B) -> Self {
Self {
status: StatusCode::CREATED,
headers: HeaderMap::new(),
body,
}
}
#[must_use]
pub fn accepted(body: B) -> Self {
Self {
status: StatusCode::ACCEPTED,
headers: HeaderMap::new(),
body,
}
}
#[must_use]
pub fn is_success(&self) -> bool {
self.status.is_success()
}
#[must_use]
pub const fn status(mut self, status: StatusCode) -> Self {
self.status = status;
self
}
#[must_use]
pub fn headers(mut self, headers: HeaderMap) -> Self {
self.headers = headers;
self
}
}
impl<B: Body> From<B> for Reply<B> {
fn from(body: B) -> Self {
Self {
status: StatusCode::OK,
headers: HeaderMap::new(),
body,
}
}
}
impl<B: Body> Deref for Reply<B> {
type Target = B;
fn deref(&self) -> &Self::Target {
&self.body
}
}