diff options
Diffstat (limited to 'src/transport.rs')
-rw-r--r-- | src/transport.rs | 82 |
1 files changed, 45 insertions, 37 deletions
diff --git a/src/transport.rs b/src/transport.rs index 5f6a6f9..489eb97 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -18,24 +18,20 @@ use hyperlocal::DomainUrl; use rustc_serialize::json; pub fn tar() -> ContentType { - ContentType(mime::Mime(mime::TopLevel::Application, - mime::SubLevel::Ext(String::from("tar")), - vec![])) + ContentType(mime::Mime( + mime::TopLevel::Application, + mime::SubLevel::Ext(String::from("tar")), + vec![], + )) } /// Transports are types which define the means of communication /// with the docker daemon pub enum Transport { /// A network tcp interface - Tcp { - client: Client, - host: String, - }, + Tcp { client: Client, host: String }, /// A Unix domain socket - Unix { - client: Client, - path: String, - }, + Unix { client: Client, path: String }, } impl fmt::Debug for Transport { @@ -48,12 +44,14 @@ impl fmt::Debug for Transport { } impl Transport { - pub fn request<'a, B>(&'a self, - method: Method, - endpoint: &str, - body: Option<(B, ContentType)>) - -> Result<String> - where B: Into<Body<'a>> + pub fn request<'a, B>( + &'a self, + method: Method, + endpoint: &str, + body: Option<(B, ContentType)>, + ) -> Result<String> + where + B: Into<Body<'a>>, { let mut res = self.stream(method, endpoint, body)?; let mut body = String::new(); @@ -62,25 +60,32 @@ impl Transport { Ok(body) } - pub fn stream<'c, B>(&'c self, - method: Method, - endpoint: &str, - body: Option<(B, ContentType)>) - -> Result<Box<Read>> - where B: Into<Body<'c>> + pub fn stream<'c, B>( + &'c self, + method: Method, + endpoint: &str, + body: Option<(B, ContentType)>, + ) -> Result<Box<Read>> + where + B: Into<Body<'c>>, { let headers = { let mut headers = header::Headers::new(); - headers.set(header::Host{ hostname: "".to_owned(), port: None }); + headers.set(header::Host { + hostname: "".to_owned(), + port: None, + }); headers }; 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)) - } + 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)), }.headers(headers); let embodied = match body { @@ -89,9 +94,9 @@ impl Transport { }; let mut res = embodied.send()?; match res.status { - StatusCode::Ok | StatusCode::Created | StatusCode::SwitchingProtocols => { - Ok(Box::new(res)) - } + StatusCode::Ok | + StatusCode::Created | + StatusCode::SwitchingProtocols => Ok(Box::new(res)), StatusCode::NoContent => Ok(Box::new(BufReader::new("".as_bytes()))), // todo: constantize these StatusCode::BadRequest => { @@ -121,7 +126,10 @@ impl Transport { StatusCode::InternalServerError => { Err(Error::Fault { code: res.status, - message: get_error_message(&mut res).unwrap_or("internal server error".to_owned()) + message: get_error_message(&mut res).unwrap_or( + "internal server error" + .to_owned(), + ), }) } _ => unreachable!(), @@ -135,15 +143,15 @@ fn get_error_message(res: &mut Response) -> Option<String> { let mut output = String::new(); if res.read_to_string(&mut output).is_ok() { let json_response = json::Json::from_str(output.as_str()).ok(); - let message = json_response.as_ref() + let message = json_response + .as_ref() .and_then(|x| x.as_object()) .and_then(|x| x.get("message")) .and_then(|x| x.as_string()) .map(|x| x.to_owned()); message - } - else { + } else { None } } |