pub use hyper::client::Response;
use std::io::{self, Read};
use serialize::{Deserialize, Deserializer};
use ::Result;
pub trait FromResponse: Send + Sized + 'static {
fn from_response<D>(des: &D, response: Response) -> Result<Self>
where D: Deserializer;
}
impl<T> FromResponse for T where T: Deserialize + Send + 'static {
fn from_response<D>(des: &D, mut response: Response) -> Result<Self>
where D: Deserializer {
des.deserialize(&mut response)
}
}
pub struct Raw(pub Response);
impl Into<Response> for Raw {
fn into(self) -> Response {
self.0
}
}
impl Read for Raw {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
impl FromResponse for Raw {
fn from_response<D>(_des: &D, response: Response) -> Result<Self>
where D: Deserializer {
Ok(Raw(response))
}
}
pub struct WithRaw<T> {
pub raw: Response,
pub value: T,
}
impl<T> FromResponse for WithRaw<T> where T: Deserialize + Send + 'static {
fn from_response<D>(des: &D, mut response: Response) -> Result<Self>
where D: Deserializer {
let val = try!(des.deserialize(&mut response));
Ok(WithRaw {
raw: response,
value: val
})
}
}
pub struct TryWithRaw<T> {
pub raw: Response,
pub result: Result<T>,
}
impl<T> FromResponse for TryWithRaw<T> where T: Deserialize + Send + 'static {
fn from_response<D>(des: &D, mut response: Response) -> Result<Self>
where D: Deserializer {
let res = des.deserialize(&mut response);
Ok(TryWithRaw {
raw: response,
result: res,
})
}
}
impl<T> Into<Result<T>> for TryWithRaw<T> {
fn into(self) -> Result<T> {
self.result
}
}