use crate::{Fields, Protocol, Status, Version, body::EmptyBody};
use std::ops::{Deref, DerefMut};
#[derive(Clone, Debug)]
pub struct ResponseMetadata {
pub status: Status,
pub version: Option<Version>,
pub fields: Fields,
pub content_length: Option<u64>,
pub persistent: Option<bool>,
pub protocol: Option<Protocol>,
}
impl ResponseMetadata {
pub fn new(status: Status) -> Self {
Self {
status,
version: None,
fields: Fields::new(),
content_length: None,
persistent: None,
protocol: None,
}
}
}
#[derive(Clone, Debug)]
pub struct Response<B = EmptyBody> {
pub metadata: ResponseMetadata,
pub body: B,
}
impl<B> Response<B> {
pub fn new(status: Status, body: B) -> Self {
Self {
metadata: ResponseMetadata::new(status),
body,
}
}
pub fn from_parts(metadata: ResponseMetadata, body: B) -> Self {
Self { metadata, body }
}
pub fn into_parts(self) -> (ResponseMetadata, B) {
(self.metadata, self.body)
}
pub fn map_body<T>(self, map: impl FnOnce(B) -> T) -> Response<T> {
Response {
metadata: self.metadata,
body: map(self.body),
}
}
pub fn with_body<T>(self, body: T) -> Response<T> {
self.map_body(|_| body)
}
}
impl Response<EmptyBody> {
pub fn empty(status: Status) -> Self {
Self::new(status, EmptyBody)
}
}
impl<B> Deref for Response<B> {
type Target = ResponseMetadata;
fn deref(&self) -> &Self::Target {
&self.metadata
}
}
impl<B> DerefMut for Response<B> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.metadata
}
}
impl<B: crate::body::ReadableBody> Response<B> {
pub async fn read(
&mut self,
cancellation: &base_scheduler::Cancellation,
) -> Result<crate::body::BodyFrame<B::Data>, B::Error> {
self.body.read(cancellation).await
}
}