Simplify match statement using try!

This is simpler than manually handling an error in each component of the
triple we match on.  In addition, we get to drop a bunch of `Ok`s.
This commit is contained in:
Colin Benner 2018-11-22 17:22:35 +01:00 committed by Tiffany Bennett
parent f805f5d180
commit a7c746f48a

View file

@ -685,10 +685,10 @@ impl Context {
})) }))
}, },
Query::Convert(ref top, Conversion::Expr(ref bottom), base, digits) => match Query::Convert(ref top, Conversion::Expr(ref bottom), base, digits) => match
(self.eval(top), self.eval(bottom), self.eval_unit_name(bottom)) (try!(self.eval(top)), try!(self.eval(bottom)), try!(self.eval_unit_name(bottom)))
{ {
(Ok(Value::Number(top)), Ok(Value::Number(bottom)), (Value::Number(top), Value::Number(bottom),
Ok((bottom_name, bottom_const))) => { (bottom_name, bottom_const)) => {
if top.unit == bottom.unit { if top.unit == bottom.unit {
let raw = match &top / &bottom { let raw = match &top / &bottom {
Some(raw) => raw, Some(raw) => raw,
@ -707,8 +707,8 @@ impl Context {
&top, &bottom))) &top, &bottom)))
} }
}, },
(Ok(Value::Substance(sub)), Ok(Value::Number(bottom)), (Value::Substance(sub), Value::Number(bottom),
Ok((bottom_name, bottom_const))) => { (bottom_name, bottom_const)) => {
sub.get_in_unit( sub.get_in_unit(
bottom, bottom,
self, self,
@ -722,8 +722,8 @@ impl Context {
QueryReply::Substance QueryReply::Substance
) )
}, },
(Ok(Value::Number(top)), Ok(Value::Substance(mut sub)), (Value::Number(top), Value::Substance(mut sub),
Ok((bottom_name, bottom_const))) => { (bottom_name, bottom_const)) => {
let unit = sub.amount.clone(); let unit = sub.amount.clone();
sub.amount = top; sub.amount = top;
sub.get_in_unit( sub.get_in_unit(
@ -739,14 +739,11 @@ impl Context {
QueryReply::Substance QueryReply::Substance
) )
}, },
(Ok(x), Ok(y), Ok(_)) => Err(QueryError::Generic(format!( (x, y, _) => Err(QueryError::Generic(format!(
"Operation is not defined: <{}> -> <{}>", "Operation is not defined: <{}> -> <{}>",
x.show(self), x.show(self),
y.show(self) y.show(self)
))), ))),
(Err(e), _, _) => Err(e),
(_, Err(e), _) => Err(e),
(_, _, Err(e)) => Err(e),
}, },
Query::Convert(ref top, Conversion::List(ref list), None, Digits::Default) => { Query::Convert(ref top, Conversion::List(ref list), None, Digits::Default) => {
let top = try!(self.eval(top)); let top = try!(self.eval(top));