From d4031758d663e6552a7e8682c1a264e399c185f0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 21 Oct 2017 16:17:35 +0200 Subject: [PATCH] Use ? operator instead of try!() macro --- lib/etc/libimaginteraction/src/format.rs | 34 +++++++++---------- lib/etc/libimaginteraction/src/readline.rs | 38 +++++++++++----------- lib/etc/libimaginteraction/src/ui.rs | 6 ++-- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/etc/libimaginteraction/src/format.rs b/lib/etc/libimaginteraction/src/format.rs index 409c072e..e39f92cb 100644 --- a/lib/etc/libimaginteraction/src/format.rs +++ b/lib/etc/libimaginteraction/src/format.rs @@ -95,9 +95,9 @@ impl HelperDef for ColorizeYellowHelper { } fn colorize(color: Colour, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { - let p = try!(h.param(0).ok_or(RenderError::new("Too few arguments"))); + let p = h.param(0).ok_or(RenderError::new("Too few arguments"))?; - try!(write!(rc.writer(), "{}", color.paint(p.value().render()))); + write!(rc.writer(), "{}", color.paint(p.value().render()))?; Ok(()) } @@ -107,9 +107,9 @@ pub struct UnderlineHelper; impl HelperDef for UnderlineHelper { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { - let p = try!(h.param(0).ok_or(RenderError::new("Too few arguments"))); + let p = h.param(0).ok_or(RenderError::new("Too few arguments"))?; let s = Style::new().underline(); - try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); + write!(rc.writer(), "{}", s.paint(p.value().render()))?; Ok(()) } } @@ -120,9 +120,9 @@ pub struct BoldHelper; impl HelperDef for BoldHelper { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { - let p = try!(h.param(0).ok_or(RenderError::new("Too few arguments"))); + let p = h.param(0).ok_or(RenderError::new("Too few arguments"))?; let s = Style::new().bold(); - try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); + write!(rc.writer(), "{}", s.paint(p.value().render()))?; Ok(()) } } @@ -133,9 +133,9 @@ pub struct BlinkHelper; impl HelperDef for BlinkHelper { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { - let p = try!(h.param(0).ok_or(RenderError::new("Too few arguments"))); + let p = h.param(0).ok_or(RenderError::new("Too few arguments"))?; let s = Style::new().blink(); - try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); + write!(rc.writer(), "{}", s.paint(p.value().render()))?; Ok(()) } } @@ -146,15 +146,15 @@ pub struct StrikethroughHelper; impl HelperDef for StrikethroughHelper { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { - let p = try!(h.param(0).ok_or(RenderError::new("Too few arguments"))); + let p = h.param(0).ok_or(RenderError::new("Too few arguments"))?; let s = Style::new().strikethrough(); - try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); + write!(rc.writer(), "{}", s.paint(p.value().render()))?; Ok(()) } } fn param_to_number(idx: usize, h: &Helper) -> Result { - match try!(h.param(idx).ok_or(RenderError::new("Too few arguments"))).value() { + match h.param(idx).ok_or(RenderError::new("Too few arguments"))?.value() { &Value::Number(ref num) => num.as_u64().ok_or_else(|| RenderError::new("Number cannot be parsed")), _ => Err(RenderError::new("Type error: First argument should be a number")), } @@ -166,9 +166,9 @@ pub struct LeftPadHelper; impl HelperDef for LeftPadHelper { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { let count = param_to_number(0, h)? as usize; - let text = try!(h.param(1).ok_or(RenderError::new("Too few arguments"))); + let text = h.param(1).ok_or(RenderError::new("Too few arguments"))?; let text = format!("{:>width$}", text.value().render(), width = count); - try!(write!(rc.writer(), "{}", text)); + write!(rc.writer(), "{}", text)?; Ok(()) } } @@ -179,9 +179,9 @@ pub struct RightPadHelper; impl HelperDef for RightPadHelper { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { let count = param_to_number(0, h)? as usize; - let text = try!(h.param(1).ok_or(RenderError::new("Too few arguments"))); + let text = h.param(1).ok_or(RenderError::new("Too few arguments"))?; let text = format!("{:width$}", text.value().render(), width = count); - try!(write!(rc.writer(), "{}", text)); + write!(rc.writer(), "{}", text)?; Ok(()) } } @@ -192,8 +192,8 @@ pub struct AbbrevHelper; impl HelperDef for AbbrevHelper { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { let count = param_to_number(0, h)? as usize; - let text = try!(h.param(1).ok_or(RenderError::new("Too few arguments"))).value().render(); - try!(write!(rc.writer(), "{}", text.chars().take(count).collect::())); + let text = h.param(1).ok_or(RenderError::new("Too few arguments"))?.value().render(); + write!(rc.writer(), "{}", text.chars().take(count).collect::())?; Ok(()) } } diff --git a/lib/etc/libimaginteraction/src/readline.rs b/lib/etc/libimaginteraction/src/readline.rs index 3f9a6399..86aa51a9 100644 --- a/lib/etc/libimaginteraction/src/readline.rs +++ b/lib/etc/libimaginteraction/src/readline.rs @@ -34,48 +34,48 @@ pub struct Readline { impl Readline { pub fn new(rt: &Runtime) -> Result { - let c = try!(rt.config().ok_or(IEK::NoConfigError)); + let c = rt.config().ok_or(IEK::NoConfigError)?; - let histfile = try!(c.lookup("ui.cli.readline_history_file").ok_or(IEK::ConfigError)); - let histsize = try!(c.lookup("ui.cli.readline_history_size").ok_or(IEK::ConfigError)); - let histigndups = try!(c.lookup("ui.cli.readline_history_ignore_dups").ok_or(IEK::ConfigError)); - let histignspace = try!(c.lookup("ui.cli.readline_history_ignore_space").ok_or(IEK::ConfigError)); - let prompt = try!(c.lookup("ui.cli.readline_prompt").ok_or(IEK::ConfigError)); + let histfile = c.lookup("ui.cli.readline_history_file").ok_or(IEK::ConfigError)?; + let histsize = c.lookup("ui.cli.readline_history_size").ok_or(IEK::ConfigError)?; + let histigndups = c.lookup("ui.cli.readline_history_ignore_dups").ok_or(IEK::ConfigError)?; + let histignspace = c.lookup("ui.cli.readline_history_ignore_space").ok_or(IEK::ConfigError)?; + let prompt = c.lookup("ui.cli.readline_prompt").ok_or(IEK::ConfigError)?; - let histfile = try!(match histfile { + let histfile = match histfile { Value::String(s) => PathBuf::from(s), _ => Err(IE::from_kind(IEK::ConfigTypeError)) .chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ReadlineError) - }); + }?; - let histsize = try!(match histsize { + let histsize = match histsize { Value::Integer(i) => i, _ => Err(IE::from_kind(IEK::ConfigTypeError)) .chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ReadlineError) - }); + }?; - let histigndups = try!(match histigndups { + let histigndups = match histigndups { Value::Boolean(b) => b, _ => Err(IE::from_kind(IEK::ConfigTypeError)) .chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ReadlineError) - }); + }?; - let histignspace = try!(match histignspace { + let histignspace = match histignspace { Value::Boolean(b) => b, _ => Err(IE::from_kind(IEK::ConfigTypeError)) .chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ReadlineError) - }); + }?; - let prompt = try!(match prompt { + let prompt = match prompt { Value::String(s) => s, _ => Err(IE::from_kind(IEK::ConfigTypeError)) .chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ReadlineError) - }); + }?; let config = Config::builder(). .max_history_size(histsize) @@ -86,11 +86,11 @@ impl Readline { let mut editor = Editor::new(config); if !histfile.exists() { - let _ = try!(File::create(histfile.clone()) - .chain_err(|| IEK::ReadlineHistoryFileCreationError)); + let _ = File::create(histfile.clone()) + .chain_err(|| IEK::ReadlineHistoryFileCreationError)?; } - let _ = try!(editor.load_history(&histfile).chain_err(|| ReadlineError)); + let _ = editor.load_history(&histfile).chain_err(|| ReadlineError)?; Ok(Readline { editor: editor, diff --git a/lib/etc/libimaginteraction/src/ui.rs b/lib/etc/libimaginteraction/src/ui.rs index d1712f94..9c16a2a5 100644 --- a/lib/etc/libimaginteraction/src/ui.rs +++ b/lib/etc/libimaginteraction/src/ui.rs @@ -59,7 +59,7 @@ pub fn get_id(matches: &ArgMatches) -> Result> { .fold(Ok(vec![]), |acc, elem| { acc.and_then(|mut v| { let elem = StoreId::new_baseless(PathBuf::from(String::from(elem))); - let elem = try!(elem.chain_err(|| IEK::StoreIdParsingError)); + let elem = elem.chain_err(|| IEK::StoreIdParsingError)?; v.push(elem); Ok(v) }) @@ -74,8 +74,8 @@ pub fn get_or_select_id(matches: &ArgMatches, store_path: &PathBuf) -> Result Ok(v), Err(_) => { let path = store_path.clone(); - let p = try!(pick_file(default_menu_cmd, path).chain_err(|| IEK::IdSelectingError)); - let id = try!(StoreId::new_baseless(p).chain_err(|| IEK::StoreIdParsingError)); + let p = pick_file(default_menu_cmd, path).chain_err(|| IEK::IdSelectingError)?; + let id = StoreId::new_baseless(p).chain_err(|| IEK::StoreIdParsingError)?; Ok(vec![id]) }, }