use std::any::Any;
use std::convert::TryInto;
use std::fmt;
use crate::dep::hyperium::http::Extensions as HyperExtensions;
use crate::dep::hyperium::http::response::{Parts as HyperiumParts, Response as HyperiumResponse};
use crate::header::{HeaderMap, HeaderName, HeaderValue};
use crate::status::StatusCode;
use crate::version::Version;
use crate::{Body, Result};
use rama_core::extensions::{Extensions, ExtensionsMut, ExtensionsRef};
#[derive(Clone)]
pub struct Response<T = Body> {
head: Parts,
body: T,
}
impl<T> From<HyperiumResponse<T>> for Response<T> {
fn from(value: HyperiumResponse<T>) -> Self {
let (parts, body) = value.into_parts();
Self::from_parts(parts.into(), body)
}
}
impl<T> From<Response<T>> for HyperiumResponse<T> {
fn from(value: Response<T>) -> Self {
let (parts, body) = value.into_parts();
let mut hyper_extensions = parts
.extensions
.get::<HyperExtensions>()
.cloned()
.unwrap_or_default();
hyper_extensions.insert(parts.extensions);
let mut response = Self::new(body);
*response.status_mut() = parts.status;
*response.version_mut() = parts.version;
*response.headers_mut() = parts.headers;
*response.extensions_mut() = hyper_extensions;
response
}
}
#[non_exhaustive]
#[derive(Clone)]
pub struct Parts {
pub status: StatusCode,
pub version: Version,
pub headers: HeaderMap<HeaderValue>,
pub extensions: Extensions,
}
impl From<HyperiumParts> for Parts {
fn from(mut value: HyperiumParts) -> Self {
let mut rama_extensions = value.extensions.remove::<Extensions>().unwrap_or_default();
rama_extensions.insert(value.extensions);
Self {
extensions: rama_extensions,
headers: value.headers,
version: value.version,
status: value.status,
}
}
}
impl From<Parts> for HyperiumParts {
fn from(parts: Parts) -> Self {
let request = Response::from_parts(parts, ());
let request = HyperiumResponse::from(request);
let (parts, _) = request.into_parts();
parts
}
}
impl ExtensionsRef for Parts {
fn extensions(&self) -> &Extensions {
&self.extensions
}
}
impl ExtensionsMut for Parts {
fn extensions_mut(&mut self) -> &mut Extensions {
&mut self.extensions
}
}
#[derive(Debug)]
#[must_use]
pub struct Builder {
inner: Result<Parts>,
}
impl Response<()> {
#[inline]
pub fn builder() -> Builder {
Builder::new()
}
#[inline]
pub fn builder_with_extensions(ext: Extensions) -> Builder {
Builder::new_with_extensions(ext)
}
}
impl<T> Response<T> {
#[inline]
pub fn new(body: T) -> Self {
Self {
head: Parts::new(),
body,
}
}
#[inline]
pub fn from_parts(parts: Parts, body: T) -> Self {
Self { head: parts, body }
}
#[inline]
pub fn status(&self) -> StatusCode {
self.head.status
}
#[inline]
pub fn status_mut(&mut self) -> &mut StatusCode {
&mut self.head.status
}
#[inline]
pub fn version(&self) -> Version {
self.head.version
}
#[inline]
pub fn version_mut(&mut self) -> &mut Version {
&mut self.head.version
}
#[inline]
pub fn headers(&self) -> &HeaderMap<HeaderValue> {
&self.head.headers
}
#[inline]
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue> {
&mut self.head.headers
}
#[inline]
pub fn body(&self) -> &T {
&self.body
}
#[inline]
pub fn body_mut(&mut self) -> &mut T {
&mut self.body
}
#[inline]
pub fn into_body(self) -> T {
self.body
}
#[inline]
pub fn into_parts(self) -> (Parts, T) {
(self.head, self.body)
}
#[inline]
pub fn map<F, U>(self, f: F) -> Response<U>
where
F: FnOnce(T) -> U,
{
Response {
body: f(self.body),
head: self.head,
}
}
}
impl<T: Default> Default for Response<T> {
#[inline]
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: fmt::Debug> fmt::Debug for Response<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Response")
.field("status", &self.status())
.field("version", &self.version())
.field("headers", self.headers())
.field("body", self.body())
.finish()
}
}
impl<T> ExtensionsRef for Response<T> {
fn extensions(&self) -> &Extensions {
&self.head.extensions
}
}
impl<T> ExtensionsMut for Response<T> {
fn extensions_mut(&mut self) -> &mut Extensions {
&mut self.head.extensions
}
}
impl Parts {
fn new() -> Self {
Self {
status: StatusCode::default(),
version: Version::default(),
headers: HeaderMap::default(),
extensions: Extensions::default(),
}
}
}
impl fmt::Debug for Parts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Parts")
.field("status", &self.status)
.field("version", &self.version)
.field("headers", &self.headers)
.finish()
}
}
impl Builder {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn new_with_extensions(ext: Extensions) -> Self {
Self {
inner: Ok(Parts {
status: StatusCode::default(),
version: Version::default(),
headers: HeaderMap::default(),
extensions: ext,
}),
}
}
pub fn status<T>(self, status: T) -> Self
where
T: TryInto<StatusCode>,
<T as TryInto<StatusCode>>::Error: Into<crate::Error>,
{
self.and_then(move |mut head| {
head.status = status.try_into().map_err(Into::into)?;
Ok(head)
})
}
pub fn version(self, version: Version) -> Self {
self.and_then(move |mut head| {
head.version = version;
Ok(head)
})
}
pub fn header<K, V>(self, key: K, value: V) -> Self
where
K: TryInto<HeaderName>,
<K as TryInto<HeaderName>>::Error: Into<crate::Error>,
V: TryInto<HeaderValue>,
<V as TryInto<HeaderValue>>::Error: Into<crate::Error>,
{
self.and_then(move |mut head| {
let name = key.try_into().map_err(Into::into)?;
let value = value.try_into().map_err(Into::into)?;
head.headers.try_append(name, value)?;
Ok(head)
})
}
#[must_use]
pub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>> {
self.inner.as_ref().ok().map(|h| &h.headers)
}
pub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>> {
self.inner.as_mut().ok().map(|h| &mut h.headers)
}
pub fn extension<T>(self, extension: T) -> Self
where
T: Clone + Any + Send + Sync + std::fmt::Debug + 'static,
{
self.and_then(move |mut head| {
head.extensions.insert(extension);
Ok(head)
})
}
#[must_use]
pub fn extensions_ref(&self) -> Option<&Extensions> {
self.inner.as_ref().ok().map(|h| &h.extensions)
}
pub fn extensions_mut(&mut self) -> Option<&mut Extensions> {
self.inner.as_mut().ok().map(|h| &mut h.extensions)
}
pub fn body<T>(self, body: T) -> Result<Response<T>> {
self.inner.map(move |head| Response { head, body })
}
fn and_then<F>(self, func: F) -> Self
where
F: FnOnce(Parts) -> Result<Parts>,
{
Self {
inner: self.inner.and_then(func),
}
}
}
impl Default for Builder {
#[inline]
fn default() -> Self {
Self {
inner: Ok(Parts::new()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_can_map_a_body_from_one_type_to_another() {
let response = Response::builder().body("some string").unwrap();
let mapped_response = response.map(|s| {
assert_eq!(s, "some string");
123u32
});
assert_eq!(mapped_response.body(), &123u32);
}
}