2020-11-19 15:21:32 +00:00
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
#[cfg(feature="serve_dir")]
|
|
|
|
use tokio::fs::File;
|
2020-11-14 03:43:29 +00:00
|
|
|
|
2020-11-20 14:40:57 +00:00
|
|
|
use std::borrow::Borrow;
|
|
|
|
|
2020-11-14 08:55:21 +00:00
|
|
|
use crate::types::Document;
|
|
|
|
|
2020-11-14 03:43:29 +00:00
|
|
|
pub enum Body {
|
|
|
|
Bytes(Vec<u8>),
|
|
|
|
Reader(Box<dyn AsyncRead + Send + Sync + Unpin>),
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:40:57 +00:00
|
|
|
impl<D: Borrow<Document>> From<D> for Body {
|
|
|
|
fn from(document: D) -> Self {
|
|
|
|
Self::from(document.borrow().to_string())
|
2020-11-14 08:55:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 03:43:29 +00:00
|
|
|
impl From<Vec<u8>> for Body {
|
|
|
|
fn from(bytes: Vec<u8>) -> Self {
|
|
|
|
Self::Bytes(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a [u8]> for Body {
|
|
|
|
fn from(bytes: &[u8]) -> Self {
|
|
|
|
Self::Bytes(bytes.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Body {
|
|
|
|
fn from(text: String) -> Self {
|
|
|
|
Self::Bytes(text.into_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a str> for Body {
|
|
|
|
fn from(text: &str) -> Self {
|
|
|
|
Self::Bytes(text.to_owned().into_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:21:32 +00:00
|
|
|
#[cfg(feature="serve_dir")]
|
2020-11-14 03:43:29 +00:00
|
|
|
impl From<File> for Body {
|
|
|
|
fn from(file: File) -> Self {
|
|
|
|
Self::Reader(Box::new(file))
|
|
|
|
}
|
|
|
|
}
|