diff options
author | softprops <d.tangren@gmail.com> | 2016-01-03 02:21:58 -0500 |
---|---|---|
committer | softprops <d.tangren@gmail.com> | 2016-01-03 02:21:58 -0500 |
commit | 6b9f98106c57af84dd1260234b90fed78cbd3806 (patch) | |
tree | 11032c38c86ae29cdce59c32e4b458e45873e691 /src/transport.rs | |
parent | b9170de9dcd95342ea869097d33fed81ac6d04cb (diff) |
rustfmt
Diffstat (limited to 'src/transport.rs')
-rw-r--r-- | src/transport.rs | 113 |
1 files changed, 63 insertions, 50 deletions
diff --git a/src/transport.rs b/src/transport.rs index a2c8d72..1eda618 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -2,7 +2,6 @@ extern crate hyper; extern crate mime; -extern crate unix_socket; use hyper::Client; use hyper::client; @@ -10,59 +9,69 @@ use self::hyper::buffer::BufReader; use self::hyper::header::ContentType; use self::hyper::status::StatusCode; use hyper::method::Method; -use self::mime::{ Attr, Mime, Value }; +use self::mime::{Attr, Mime, Value}; use self::mime::TopLevel::Application; use self::mime::SubLevel::Json; use std::fmt; use std::ops::DerefMut; -use std::io::{ Error, ErrorKind, Read, Result, Write }; +use std::io::{Error, ErrorKind, Read, Result, Write}; use hyperlocal::DomainUrl; fn lift_status_err(status: u16) -> Result<Box<Read>> { - match status { - 400 => Err(Error::new(ErrorKind::InvalidInput, "bad parameter")), - 404 => Err(Error::new(ErrorKind::InvalidInput, "not found")), - 406 => Err(Error::new(ErrorKind::InvalidInput, "not acceptable")), - 409 => Err(Error::new(ErrorKind::InvalidInput, "conflict found")), - 500 => Err(Error::new(ErrorKind::InvalidInput, "interal server error")), - _ => unreachable!() - } + match status { + 400 => Err(Error::new(ErrorKind::InvalidInput, "bad parameter")), + 404 => Err(Error::new(ErrorKind::InvalidInput, "not found")), + 406 => Err(Error::new(ErrorKind::InvalidInput, "not acceptable")), + 409 => Err(Error::new(ErrorKind::InvalidInput, "conflict found")), + 500 => Err(Error::new(ErrorKind::InvalidInput, "interal server error")), + _ => unreachable!(), + } } pub enum Transport { - Tcp { client: Client, host: String }, - Unix { client: Client, path: String } + Tcp { + client: Client, + host: String, + }, + Unix { + client: Client, + path: String, + }, } impl fmt::Debug for Transport { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Transport::Tcp { ref host, .. } => { - write!(f, "Tcp({})", host) - }, - Transport::Unix { ref path, .. } => { - write!(f, "Unix({})", path) - } + Transport::Tcp { ref host, .. } => write!(f, "Tcp({})", host), + Transport::Unix { ref path, .. } => write!(f, "Unix({})", path), } } } impl Transport { - pub fn request(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<String> { - let mut res = match self.stream(method, endpoint, body) { - Ok(r) => r, - Err(e) => panic!("failed request {:?}", e) - }; - let mut body = String::new(); - res.read_to_string(&mut body).map(|_| body) - } + pub fn request(&mut self, + method: Method, + endpoint: &str, + body: Option<Body>) + -> Result<String> { + let mut res = match self.stream(method, endpoint, body) { + Ok(r) => r, + Err(e) => panic!("failed request {:?}", e), + }; + let mut body = String::new(); + res.read_to_string(&mut body).map(|_| body) + } - pub fn stream(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<Box<Read>> { + pub fn stream(&mut self, + method: Method, + endpoint: &str, + body: Option<Body>) + -> Result<Box<Read>> { println!("requesting {:?} {:?}", self, endpoint); let req = match *self { Transport::Tcp { ref client, ref host } => { client.request(method, &format!("{}{}", host, endpoint)[..]) - }, + } Transport::Unix { ref client, ref path } => { client.request(method, DomainUrl::new(&path, endpoint)) } @@ -71,35 +80,39 @@ impl Transport { let embodied = match body { Some(Body { read: r, size: l }) => { let reader: &mut Read = *r.deref_mut(); - let content_type: Mime = Mime(Application, Json, vec![(Attr::Charset, Value::Utf8)]); + let content_type: Mime = Mime(Application, + Json, + vec![(Attr::Charset, Value::Utf8)]); req.header(ContentType(content_type)).body(client::Body::SizedBody(reader, l)) - }, - _ => req + } + _ => req, }; - let res = match embodied.send() { - Ok(r) => r, - Err(e) => panic!("failed request {:?}", e) - }; - match res.status { - StatusCode::Ok | StatusCode::Created | StatusCode::SwitchingProtocols => - Ok(Box::new(res)), - StatusCode::NoContent => - Ok(Box::new(BufReader::new("".as_bytes()))), - status => - lift_status_err(status.to_u16()) + let res = match embodied.send() { + Ok(r) => r, + Err(e) => panic!("failed request {:?}", e), + }; + match res.status { + StatusCode::Ok | StatusCode::Created | StatusCode::SwitchingProtocols => { + Ok(Box::new(res)) + } + StatusCode::NoContent => Ok(Box::new(BufReader::new("".as_bytes()))), + status => lift_status_err(status.to_u16()), + } } - } } #[doc(hidden)] pub struct Body<'a> { - read: &'a mut Box<&'a mut Read>, - size: u64 + read: &'a mut Box<&'a mut Read>, + size: u64, } impl<'a> Body<'a> { - /// Create a new body instance - pub fn new(read: &'a mut Box<&'a mut Read>, size: u64) -> Body<'a> { - Body { read: read, size: size } - } + /// Create a new body instance + pub fn new(read: &'a mut Box<&'a mut Read>, size: u64) -> Body<'a> { + Body { + read: read, + size: size, + } + } } |