From fe51ec45f98f215a7158bf64b9f10b80ce04e720 Mon Sep 17 00:00:00 2001 From: softprops Date: Sun, 25 Jun 2017 21:21:09 -0400 Subject: replace all uses of try with ?. fixes #42 --- src/builder.rs | 8 ++--- src/errors.rs | 2 +- src/lib.rs | 94 ++++++++++++++++++++++++++++---------------------------- src/tarball.rs | 26 ++++++++-------- src/transport.rs | 6 ++-- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index f8c6899..bb7adba 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -309,7 +309,7 @@ impl ContainerOptions { /// serialize options as a string. returns None if no options are defined pub fn serialize(&self) -> Result { - Ok(try!(json::encode(&self.to_json()))) + Ok(json::encode(&self.to_json())?) } pub fn parse_from<'a, K, V>(&self, @@ -489,7 +489,7 @@ impl ExecContainerOptions { } let json_obj: Json = body.to_json(); - Ok(try!(json::encode(&json_obj))) + Ok(json::encode(&json_obj)?) } } @@ -872,7 +872,7 @@ impl NetworkCreateOptions { /// serialize options as a string. returns None if no options are defined pub fn serialize(&self) -> Result { - Ok(try!(json::encode(&self.to_json()))) + Ok(json::encode(&self.to_json())?) } pub fn parse_from<'a, K, V>(&self, @@ -953,7 +953,7 @@ impl ToJson for ContainerConnectionOptions { impl ContainerConnectionOptions { /// serialize options as a string. returns None if no options are defined pub fn serialize(&self) -> Result { - Ok(try!(json::encode(&self.to_json()))) + Ok(json::encode(&self.to_json())?) } pub fn parse_from<'a, K, V>(&self, diff --git a/src/errors.rs b/src/errors.rs index b388cbb..323b609 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -52,7 +52,7 @@ impl From for Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "Docker Error: ")); + write!(f, "Docker Error: ")?; match self { &Error::Decoding(ref err) => return err.fmt(f), &Error::Encoding(ref err) => return err.fmt(f), diff --git a/src/lib.rs b/src/lib.rs index 530e65c..74234f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,20 +91,20 @@ impl<'a, 'b> Image<'a, 'b> { /// Inspects a named image's details pub fn inspect(&self) -> Result { - let raw = try!(self.docker.get(&format!("/images/{}/json", self.name)[..])); - Ok(try!(json::decode::(&raw))) + let raw = self.docker.get(&format!("/images/{}/json", self.name)[..])?; + Ok(json::decode::(&raw)?) } /// Lists the history of the images set of changes pub fn history(&self) -> Result> { - let raw = try!(self.docker.get(&format!("/images/{}/history", self.name)[..])); - Ok(try!(json::decode::>(&raw))) + let raw = self.docker.get(&format!("/images/{}/history", self.name)[..])?; + Ok(json::decode::>(&raw)?) } /// Delete's an image pub fn delete(&self) -> Result> { - let raw = try!(self.docker.delete(&format!("/images/{}", self.name)[..])); - Ok(match try!(Json::from_str(&raw)) { + let raw = self.docker.delete(&format!("/images/{}", self.name)[..])?; + Ok(match Json::from_str(&raw)? { Json::Array(ref xs) => { xs.iter().map(|j| { let obj = j.as_object().expect("expected json object"); @@ -154,11 +154,11 @@ impl<'a> Images<'a> { let mut bytes = vec![]; - try!(tarball::dir(&mut bytes, &opts.path[..])); + tarball::dir(&mut bytes, &opts.path[..])?; - let raw = try!(self.docker.stream_post(&path.join("?"), + let raw = self.docker.stream_post(&path.join("?"), Some((Body::BufBody(&bytes[..], bytes.len()), - tar())))); + tar())))?; let it = jed::Iter::new(raw).into_iter().map(|j| { // fixme: better error handling debug!("{:?}", j); @@ -198,8 +198,8 @@ impl<'a> Images<'a> { if let Some(query) = opts.serialize() { path.push(query); } - let raw = try!(self.docker.get(&path.join("?"))); - Ok(try!(json::decode::>(&raw))) + let raw = self.docker.get(&path.join("?"))?; + Ok(json::decode::>(&raw)?) } /// Returns a reference to a set of operations available for a named image @@ -210,8 +210,8 @@ impl<'a> Images<'a> { /// Search for docker images by term pub fn search(&self, term: &str) -> Result> { let query = form_urlencoded::serialize(vec![("term", term)]); - let raw = try!(self.docker.get(&format!("/images/search?{}", query)[..])); - Ok(try!(json::decode::>(&raw))) + let raw = self.docker.get(&format!("/images/search?{}", query)[..])?; + Ok(json::decode::>(&raw)?) } /// Pull and create a new docker images from an existing image @@ -220,8 +220,8 @@ impl<'a> Images<'a> { if let Some(query) = opts.serialize() { path.push(query); } - let raw = try!(self.docker - .stream_post(&path.join("?"), None as Option<(&'a str, ContentType)>)); + let raw = self.docker + .stream_post(&path.join("?"), None as Option<(&'a str, ContentType)>)?; let it = jed::Iter::new(raw).into_iter().map(|j| { // fixme: better error handling debug!("{:?}", j); @@ -286,8 +286,8 @@ impl<'a, 'b> Container<'a, 'b> { /// Inspects the current docker container instance's details pub fn inspect(&self) -> Result { - let raw = try!(self.docker.get(&format!("/containers/{}/json", self.id)[..])); - Ok(try!(json::decode::(&raw))) + let raw = self.docker.get(&format!("/containers/{}/json", self.id)[..])?; + Ok(json::decode::(&raw)?) } /// Returns a `top` view of information about the container process @@ -297,9 +297,9 @@ impl<'a, 'b> Container<'a, 'b> { let encoded = form_urlencoded::serialize(vec![("ps_args", args)]); path.push(encoded) } - let raw = try!(self.docker.get(&path.join("?"))); + let raw = self.docker.get(&path.join("?"))?; - Ok(try!(json::decode::(&raw))) + Ok(json::decode::(&raw)?) } /// Returns a stream of logs emitted but the container instance @@ -313,8 +313,8 @@ impl<'a, 'b> Container<'a, 'b> { /// Returns a set of changes made to the container instance pub fn changes(&self) -> Result> { - let raw = try!(self.docker.get(&format!("/containers/{}/changes", self.id)[..])); - Ok(try!(json::decode::>(&raw))) + let raw = self.docker.get(&format!("/containers/{}/changes", self.id)[..])?; + Ok(json::decode::>(&raw)?) } /// Exports the current docker container into a tarball @@ -324,7 +324,7 @@ impl<'a, 'b> Container<'a, 'b> { /// Returns a stream of stats specific to this container instance pub fn stats(&self) -> Result>> { - let raw = try!(self.docker.stream_get(&format!("/containers/{}/stats", self.id)[..])); + let raw = self.docker.stream_get(&format!("/containers/{}/stats", self.id)[..])?; let it = jed::Iter::new(raw).into_iter().map(|j| { // fixme: better error handling debug!("{:?}", j); @@ -399,9 +399,9 @@ impl<'a, 'b> Container<'a, 'b> { /// Wait until the container stops pub fn wait(&self) -> Result { - let raw = try!(self.docker.post(&format!("/containers/{}/wait", self.id)[..], - None as Option<(&'a str, ContentType)>)); - Ok(try!(json::decode::(&raw))) + let raw = self.docker.post(&format!("/containers/{}/wait", self.id)[..], + None as Option<(&'a str, ContentType)>)?; + Ok(json::decode::(&raw)?) } /// Delete the container instance @@ -417,13 +417,13 @@ impl<'a, 'b> Container<'a, 'b> { if let Some(query) = opts.serialize() { path.push(query) } - try!(self.docker.delete(&path.join("?"))); + self.docker.delete(&path.join("?"))?; Ok(()) } /// Exec the specified command in the container pub fn exec(&self, opts: &ExecContainerOptions) -> Result { - let data = try!(opts.serialize()); + let data = opts.serialize()?; let mut bytes = data.as_bytes(); match self.docker .post(&format!("/containers/{}/exec", self.id)[..], @@ -467,8 +467,8 @@ impl<'a> Containers<'a> { if let Some(query) = opts.serialize() { path.push(query) } - let raw = try!(self.docker.get(&path.join("?"))); - Ok(try!(json::decode::>(&raw))) + let raw = self.docker.get(&path.join("?"))?; + Ok(json::decode::>(&raw)?) } /// Returns a reference to a set of operations available to a specific container instance @@ -478,7 +478,7 @@ impl<'a> Containers<'a> { /// Returns a builder interface for creating a new container instance pub fn create(&'a self, opts: &ContainerOptions) -> Result { - let data = try!(opts.serialize()); + let data = opts.serialize()?; let mut bytes = data.as_bytes(); let mut path = vec!["/containers/create".to_owned()]; @@ -486,9 +486,9 @@ impl<'a> Containers<'a> { path.push(form_urlencoded::serialize(vec![("name", name)])); } - let raw = try!(self.docker.post(&path.join("?"), - Some((&mut bytes, ContentType::json())))); - Ok(try!(json::decode::(&raw))) + let raw = self.docker.post(&path.join("?"), + Some((&mut bytes, ContentType::json())))?; + Ok(json::decode::(&raw)?) } } @@ -509,8 +509,8 @@ impl<'a> Networks<'a> { if let Some(query) = opts.serialize() { path.push(query); } - let raw = try!(self.docker.get(&path.join("?"))); - Ok(try!(json::decode::>(&raw))) + let raw = self.docker.get(&path.join("?"))?; + Ok(json::decode::>(&raw)?) } /// Returns a reference to a set of operations available to a specific network instance @@ -519,13 +519,13 @@ impl<'a> Networks<'a> { } pub fn create(&'a self, opts: &NetworkCreateOptions) -> Result { - let data = try!(opts.serialize()); + let data = opts.serialize()?; let mut bytes = data.as_bytes(); let path = vec!["/networks/create".to_owned()]; - let raw = try!(self.docker.post(&path.join("?"), - Some((&mut bytes, ContentType::json())))); - Ok(try!(json::decode::(&raw))) + let raw = self.docker.post(&path.join("?"), + Some((&mut bytes, ContentType::json())))?; + Ok(json::decode::(&raw)?) } } @@ -552,8 +552,8 @@ impl<'a, 'b> Network<'a, 'b> { /// Inspects the current docker network instance's details pub fn inspect(&self) -> Result { - let raw = try!(self.docker.get(&format!("/networks/{}", self.id)[..])); - Ok(try!(json::decode::(&raw))) + let raw = self.docker.get(&format!("/networks/{}", self.id)[..])?; + Ok(json::decode::(&raw)?) } /// Delete the network instance @@ -572,7 +572,7 @@ impl<'a, 'b> Network<'a, 'b> { } fn do_connection(&self, segment: &str, opts: &ContainerConnectionOptions) -> Result<()> { - let data = try!(opts.serialize()); + let data = opts.serialize()?; let mut bytes = data.as_bytes(); self.docker @@ -658,14 +658,14 @@ impl Docker { /// Returns version information associated with the docker daemon pub fn version(&self) -> Result { - let raw = try!(self.get("/version")); - Ok(try!(json::decode::(&raw))) + let raw = self.get("/version")?; + Ok(json::decode::(&raw)?) } /// Returns information associated with the docker daemon pub fn info(&self) -> Result { - let raw = try!(self.get("/info")); - Ok(try!(json::decode::(&raw))) + let raw = self.get("/info")?; + Ok(json::decode::(&raw)?) } /// Returns a simple ping response indicating the docker daemon is accessible @@ -679,7 +679,7 @@ impl Docker { if let Some(query) = opts.serialize() { path.push(query); } - let raw = try!(self.stream_get(&path.join("?")[..])); + let raw = self.stream_get(&path.join("?")[..])?; let it = jed::Iter::new(raw).into_iter().map(|j| { debug!("{:?}", j); // fixme: better error handling diff --git a/src/tarball.rs b/src/tarball.rs index 1255928..d430279 100644 --- a/src/tarball.rs +++ b/src/tarball.rs @@ -14,16 +14,16 @@ pub fn dir(buf: W, path: &str) -> io::Result<()> fn bundle(dir: &Path, f: &F, bundle_dir: bool) -> io::Result<()> where F: Fn(&Path) -> io::Result<()> { - if try!(fs::metadata(dir)).is_dir() { + if fs::metadata(dir)?.is_dir() { if bundle_dir { - try!(f(&dir)); + f(&dir)?; } - for entry in try!(fs::read_dir(dir)) { - let entry = try!(entry); - if try!(fs::metadata(entry.path())).is_dir() { - try!(bundle(&entry.path(), f, true)); + for entry in fs::read_dir(dir)? { + let entry = entry?; + if fs::metadata(entry.path())?.is_dir() { + bundle(&entry.path(), f, true)?; } else { - try!(f(&entry.path().as_path())); + f(&entry.path().as_path())?; } } } @@ -31,7 +31,7 @@ pub fn dir(buf: W, path: &str) -> io::Result<()> } { - let base_path = try!(Path::new(path).canonicalize()); + let base_path = Path::new(path).canonicalize()?; // todo: don't unwrap let mut base_path_str = base_path.to_str().unwrap().to_owned(); if let Some(last) = base_path_str.chars().last() { @@ -41,18 +41,18 @@ pub fn dir(buf: W, path: &str) -> io::Result<()> } let append = |path: &Path| { - let canonical = try!(path.canonicalize()); + let canonical = path.canonicalize()?; // todo: don't unwrap let relativized = canonical.to_str().unwrap().trim_left_matches(&base_path_str[..]); if path.is_dir() { - try!(archive.append_dir(Path::new(relativized), &canonical)) + archive.append_dir(Path::new(relativized), &canonical)? } else { - try!(archive.append_file(Path::new(relativized), &mut try!(File::open(&canonical)))) + archive.append_file(Path::new(relativized), &mut File::open(&canonical)?)? } Ok(()) }; - try!(bundle(Path::new(path), &append, false)); - try!(archive.finish()); + bundle(Path::new(path), &append, false)?; + archive.finish()?; } Ok(()) diff --git a/src/transport.rs b/src/transport.rs index 5762e59..5f6a6f9 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -55,9 +55,9 @@ impl Transport { -> Result where B: Into> { - let mut res = try!(self.stream(method, endpoint, body)); + let mut res = self.stream(method, endpoint, body)?; let mut body = String::new(); - try!(res.read_to_string(&mut body)); + res.read_to_string(&mut body)?; debug!("{} raw response: {}", endpoint, body); Ok(body) } @@ -87,7 +87,7 @@ impl Transport { Some((b, c)) => req.header(c).body(b), _ => req, }; - let mut res = try!(embodied.send()); + let mut res = embodied.send()?; match res.status { StatusCode::Ok | StatusCode::Created | StatusCode::SwitchingProtocols => { Ok(Box::new(res)) -- cgit v1.2.3