use bytes::Bytes;
use error::Error;
use internal;
use std::fmt;
use std::fs::File;
use std::io::{self, Cursor, Read, Seek, SeekFrom};
use std::str;
pub struct Body(Inner);
enum Inner {
Empty,
Bytes(Cursor<Bytes>),
Streaming(Box<Read + Send>),
}
impl Body {
pub fn from_reader(reader: impl Read + Send + 'static) -> Body {
Body(Inner::Streaming(Box::new(reader)))
}
pub fn is_empty(&self) -> bool {
self.len() == Some(0)
}
pub fn is_seekable(&self) -> bool {
match &self.0 {
Inner::Empty => true,
Inner::Bytes(_) => true,
Inner::Streaming(_) => false,
}
}
pub fn len(&self) -> Option<usize> {
match &self.0 {
&Inner::Empty => Some(0),
&Inner::Bytes(ref bytes) => Some(bytes.get_ref().len()),
&Inner::Streaming(_) => None,
}
}
pub fn text(&mut self) -> Result<String, Error> {
match &mut self.0 {
&mut Inner::Empty => Ok(String::new()),
&mut Inner::Bytes(ref bytes) => str::from_utf8(bytes.get_ref())
.map(Into::into)
.map_err(Into::into),
&mut Inner::Streaming(ref mut reader) => {
let mut string = String::new();
reader.read_to_string(&mut string)?;
Ok(string)
},
}
}
#[cfg(feature = "json")]
pub fn json(&mut self) -> Result<::json::JsonValue, Error> {
let text = self.text()?;
Ok(::json::parse(&text)?)
}
}
impl Read for Body {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match &mut self.0 {
&mut Inner::Empty => Ok(0),
&mut Inner::Bytes(ref mut bytes) => bytes.read(buf),
&mut Inner::Streaming(ref mut reader) => reader.read(buf),
}
}
}
impl Seek for Body {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match &mut self.0 {
&mut Inner::Empty => match pos {
SeekFrom::Start(0) | SeekFrom::End(0) | SeekFrom::Current(0) => Ok(0),
_ => Err(io::ErrorKind::InvalidInput.into()),
},
&mut Inner::Bytes(ref mut bytes) => bytes.seek(pos),
_ => Err(io::ErrorKind::InvalidInput.into()),
}
}
}
impl Default for Body {
fn default() -> Self {
Body(Inner::Empty)
}
}
impl From<()> for Body {
fn from(_: ()) -> Self {
Self::default()
}
}
impl From<Vec<u8>> for Body {
fn from(body: Vec<u8>) -> Self {
Bytes::from(body).into()
}
}
impl<'a> From<&'a [u8]> for Body {
fn from(body: &'a [u8]) -> Self {
Bytes::from(body).into()
}
}
impl From<Bytes> for Body {
fn from(body: Bytes) -> Self {
Body(Inner::Bytes(Cursor::new(body)))
}
}
impl From<String> for Body {
fn from(body: String) -> Self {
body.into_bytes().into()
}
}
impl<'a> From<&'a str> for Body {
fn from(body: &'a str) -> Self {
body.as_bytes().into()
}
}
impl From<File> for Body {
fn from(body: File) -> Self {
Self::from_reader(body)
}
}
impl<T: Into<Body>> From<Option<T>> for Body {
fn from(body: Option<T>) -> Self {
match body {
Some(body) => body.into(),
None => Self::default(),
}
}
}
impl fmt::Debug for Body {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.0 {
Inner::Empty => write!(f, "Empty"),
Inner::Bytes(bytes) => write!(f, "Memory({})", internal::format_byte_string(bytes.get_ref())),
Inner::Streaming(_) => write!(f, "Streaming"),
}
}
}