diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-11-14 14:32:26 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-11-14 14:52:51 +0100 |
commit | 6a8273808ac2ae2643b3e3d04ff86c0620cbc3a5 (patch) | |
tree | 74f4ea6e1fbf3d56ff4b2f52dd89f6d4343d3d6f /libimagstore | |
parent | 79f68a1b793c0acdf4037c97f973876d5838e260 (diff) |
Refactor to use try!() instead of match{}ing
Diffstat (limited to 'libimagstore')
-rw-r--r-- | libimagstore/src/toml_ext.rs | 52 |
1 files changed, 10 insertions, 42 deletions
diff --git a/libimagstore/src/toml_ext.rs b/libimagstore/src/toml_ext.rs index a59898b5..98f35598 100644 --- a/libimagstore/src/toml_ext.rs +++ b/libimagstore/src/toml_ext.rs @@ -83,23 +83,13 @@ impl TomlValueExt for Value { * Returns true if header field was set, false if there is already a value */ fn insert_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<bool> { - let tokens = match tokenize(spec, sep) { - Err(e) => return Err(e), - Ok(t) => t - }; - - let destination = match tokens.iter().last() { - None => return Err(SE::new(SEK::HeaderPathSyntaxError, None)), - Some(d) => d, - }; + let tokens = try!(tokenize(spec, sep)); + let destination = try!(tokens.iter().last().ok_or(SEK::HeaderPathSyntaxError.into_error)) let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens // walk N-1 tokens - let value = match walk_header(self, path_to_dest) { - Err(e) => return Err(e), - Ok(v) => v - }; + let value = try!(walk_header(self, path_to_dest)); // There is already an value at this place if extract(value, destination).is_ok() { @@ -170,24 +160,15 @@ impl TomlValueExt for Value { * will be returned */ fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> { - let tokens = match tokenize(spec, sep) { - Err(e) => return Err(e), - Ok(t) => t, - }; + let tokens = try!(tokenize(spec, sep)); debug!("tokens = {:?}", tokens); - let destination = match tokens.iter().last() { - None => return Err(SEK::HeaderPathSyntaxError.into_error()), - Some(d) => d - }; + let destination = try!(tokens.iter().last().ok_or(SEK::HeaderPathSyntaxError.into_error())) debug!("destination = {:?}", destination); let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens // walk N-1 tokens - let value = match walk_header(self, path_to_dest) { - Err(e) => return Err(e), - Ok(v) => v - }; + let value = try!(walk_header(self, path_to_dest)); debug!("walked value = {:?}", value); match *destination { @@ -269,10 +250,7 @@ impl TomlValueExt for Value { * larger than the array length. */ fn read_with_sep(&self, spec: &str, splitchr: char) -> Result<Option<Value>> { - let tokens = match tokenize(spec, splitchr) { - Err(e) => return Err(e), - Ok(t) => t, - }; + let tokens = try!(tokenize(spec, splitchr)); let mut header_clone = self.clone(); // we clone as READing is simpler this way // walk N-1 tokens @@ -287,23 +265,13 @@ impl TomlValueExt for Value { } fn delete_with_sep(&mut self, spec: &str, splitchr: char) -> Result<Option<Value>> { - let tokens = match tokenize(spec, splitchr) { - Err(e) => return Err(e), - Ok(t) => t - }; - - let destination = match tokens.iter().last() { - None => return Err(SEK::HeaderPathSyntaxError.into_error()), - Some(d) => d - }; + let tokens = try!(tokenize(spec, splitchr)); + let destination = try!(tokens.iter().last().ok_or(SEK::HeaderPathSyntaxError.into_error())); debug!("destination = {:?}", destination); let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens // walk N-1 tokens - let mut value = match walk_header(self, path_to_dest) { - Err(e) => return Err(e), - Ok(v) => v - }; + let mut value = try!(walk_header(self, path_to_dest)); debug!("walked value = {:?}", value); match *destination { |