Use ? operator instead of try!() macro

This commit is contained in:
Matthias Beyer 2017-10-21 16:17:35 +02:00
parent 26d7fd4eb7
commit d4031758d6
3 changed files with 39 additions and 39 deletions

View file

@ -95,9 +95,9 @@ impl HelperDef for ColorizeYellowHelper {
} }
fn colorize(color: Colour, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { 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(()) Ok(())
} }
@ -107,9 +107,9 @@ pub struct UnderlineHelper;
impl HelperDef for UnderlineHelper { impl HelperDef for UnderlineHelper {
fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
RenderError> { 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(); let s = Style::new().underline();
try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); write!(rc.writer(), "{}", s.paint(p.value().render()))?;
Ok(()) Ok(())
} }
} }
@ -120,9 +120,9 @@ pub struct BoldHelper;
impl HelperDef for BoldHelper { impl HelperDef for BoldHelper {
fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
RenderError> { 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(); let s = Style::new().bold();
try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); write!(rc.writer(), "{}", s.paint(p.value().render()))?;
Ok(()) Ok(())
} }
} }
@ -133,9 +133,9 @@ pub struct BlinkHelper;
impl HelperDef for BlinkHelper { impl HelperDef for BlinkHelper {
fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
RenderError> { 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(); let s = Style::new().blink();
try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); write!(rc.writer(), "{}", s.paint(p.value().render()))?;
Ok(()) Ok(())
} }
} }
@ -146,15 +146,15 @@ pub struct StrikethroughHelper;
impl HelperDef for StrikethroughHelper { impl HelperDef for StrikethroughHelper {
fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(),
RenderError> { 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(); let s = Style::new().strikethrough();
try!(write!(rc.writer(), "{}", s.paint(p.value().render()))); write!(rc.writer(), "{}", s.paint(p.value().render()))?;
Ok(()) Ok(())
} }
} }
fn param_to_number(idx: usize, h: &Helper) -> Result<u64, RenderError> { fn param_to_number(idx: usize, h: &Helper) -> Result<u64, RenderError> {
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")), &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")), _ => Err(RenderError::new("Type error: First argument should be a number")),
} }
@ -166,9 +166,9 @@ pub struct LeftPadHelper;
impl HelperDef for LeftPadHelper { impl HelperDef for LeftPadHelper {
fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
let count = param_to_number(0, h)? as usize; 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); let text = format!("{:>width$}", text.value().render(), width = count);
try!(write!(rc.writer(), "{}", text)); write!(rc.writer(), "{}", text)?;
Ok(()) Ok(())
} }
} }
@ -179,9 +179,9 @@ pub struct RightPadHelper;
impl HelperDef for RightPadHelper { impl HelperDef for RightPadHelper {
fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
let count = param_to_number(0, h)? as usize; 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); let text = format!("{:width$}", text.value().render(), width = count);
try!(write!(rc.writer(), "{}", text)); write!(rc.writer(), "{}", text)?;
Ok(()) Ok(())
} }
} }
@ -192,8 +192,8 @@ pub struct AbbrevHelper;
impl HelperDef for AbbrevHelper { impl HelperDef for AbbrevHelper {
fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { fn call(&self, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
let count = param_to_number(0, h)? as usize; let count = param_to_number(0, h)? as usize;
let text = try!(h.param(1).ok_or(RenderError::new("Too few arguments"))).value().render(); let text = h.param(1).ok_or(RenderError::new("Too few arguments"))?.value().render();
try!(write!(rc.writer(), "{}", text.chars().take(count).collect::<String>())); write!(rc.writer(), "{}", text.chars().take(count).collect::<String>())?;
Ok(()) Ok(())
} }
} }

View file

@ -34,48 +34,48 @@ pub struct Readline {
impl Readline { impl Readline {
pub fn new(rt: &Runtime) -> Result<Readline> { pub fn new(rt: &Runtime) -> Result<Readline> {
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 histfile = 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 histsize = 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 histigndups = 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 histignspace = 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 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), Value::String(s) => PathBuf::from(s),
_ => Err(IE::from_kind(IEK::ConfigTypeError)) _ => Err(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ConfigError)
.chain_err(|| IEK::ReadlineError) .chain_err(|| IEK::ReadlineError)
}); }?;
let histsize = try!(match histsize { let histsize = match histsize {
Value::Integer(i) => i, Value::Integer(i) => i,
_ => Err(IE::from_kind(IEK::ConfigTypeError)) _ => Err(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ConfigError)
.chain_err(|| IEK::ReadlineError) .chain_err(|| IEK::ReadlineError)
}); }?;
let histigndups = try!(match histigndups { let histigndups = match histigndups {
Value::Boolean(b) => b, Value::Boolean(b) => b,
_ => Err(IE::from_kind(IEK::ConfigTypeError)) _ => Err(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ConfigError)
.chain_err(|| IEK::ReadlineError) .chain_err(|| IEK::ReadlineError)
}); }?;
let histignspace = try!(match histignspace { let histignspace = match histignspace {
Value::Boolean(b) => b, Value::Boolean(b) => b,
_ => Err(IE::from_kind(IEK::ConfigTypeError)) _ => Err(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ConfigError)
.chain_err(|| IEK::ReadlineError) .chain_err(|| IEK::ReadlineError)
}); }?;
let prompt = try!(match prompt { let prompt = match prompt {
Value::String(s) => s, Value::String(s) => s,
_ => Err(IE::from_kind(IEK::ConfigTypeError)) _ => Err(IE::from_kind(IEK::ConfigTypeError))
.chain_err(|| IEK::ConfigError) .chain_err(|| IEK::ConfigError)
.chain_err(|| IEK::ReadlineError) .chain_err(|| IEK::ReadlineError)
}); }?;
let config = Config::builder(). let config = Config::builder().
.max_history_size(histsize) .max_history_size(histsize)
@ -86,11 +86,11 @@ impl Readline {
let mut editor = Editor::new(config); let mut editor = Editor::new(config);
if !histfile.exists() { if !histfile.exists() {
let _ = try!(File::create(histfile.clone()) let _ = File::create(histfile.clone())
.chain_err(|| IEK::ReadlineHistoryFileCreationError)); .chain_err(|| IEK::ReadlineHistoryFileCreationError)?;
} }
let _ = try!(editor.load_history(&histfile).chain_err(|| ReadlineError)); let _ = editor.load_history(&histfile).chain_err(|| ReadlineError)?;
Ok(Readline { Ok(Readline {
editor: editor, editor: editor,

View file

@ -59,7 +59,7 @@ pub fn get_id(matches: &ArgMatches) -> Result<Vec<StoreId>> {
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(|mut v| { acc.and_then(|mut v| {
let elem = StoreId::new_baseless(PathBuf::from(String::from(elem))); 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); v.push(elem);
Ok(v) Ok(v)
}) })
@ -74,8 +74,8 @@ pub fn get_or_select_id(matches: &ArgMatches, store_path: &PathBuf) -> Result<Ve
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(_) => { Err(_) => {
let path = store_path.clone(); let path = store_path.clone();
let p = try!(pick_file(default_menu_cmd, path).chain_err(|| IEK::IdSelectingError)); let p = pick_file(default_menu_cmd, path).chain_err(|| IEK::IdSelectingError)?;
let id = try!(StoreId::new_baseless(p).chain_err(|| IEK::StoreIdParsingError)); let id = StoreId::new_baseless(p).chain_err(|| IEK::StoreIdParsingError)?;
Ok(vec![id]) Ok(vec![id])
}, },
} }