From 886788a95a3c79a7664cbcd0bfffa1e500461adf Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Wed, 9 Jan 2019 15:15:55 -0800 Subject: Simplify try_with_pin_and_data function The try_with_pin_and_data function is a fairly complex beast. Part of that complexity stems from the returned Result value, whose error part not only contains the error but also the previously passed in data. As it turns out, though, this data as returned is never actually consumed by any client. Hence, this change simplifies the logic slightly by removing all the additional complexity that this tuple return entailed. --- nitrocli/src/commands.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index e719039..2bea53a 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -97,7 +97,6 @@ fn get_password_safe(device: &dyn Device) -> Result> (), |_, pin| device.get_password_safe(pin).map_err(|err| ((), err)), ) - .map_err(|(_, err)| err) } /// Authenticate the given device using the given PIN type and operation. @@ -113,7 +112,7 @@ where D: Device, F: Fn(D, &str) -> result::Result, { - try_with_pin_and_data(pin_type, msg, device, op).map_err(|(_device, err)| err) + try_with_pin_and_data(pin_type, msg, device, op) } /// Authenticate the given device with the user PIN. @@ -174,7 +173,7 @@ fn try_with_pin_and_data( msg: &'static str, data: D, op: F, -) -> result::Result +) -> Result where F: Fn(D, &str) -> result::Result, { @@ -182,17 +181,12 @@ where let mut retry = 3; let mut error_msg = None; loop { - let pin = match pinentry::inquire_pin(pin_type, pinentry::Mode::Query, error_msg) { - Ok(pin) => pin, - Err(err) => return Err((data, err)), - }; + let pin = pinentry::inquire_pin(pin_type, pinentry::Mode::Query, error_msg)?; match op(data, &pin) { Ok(result) => return Ok(result), Err((new_data, err)) => match err { nitrokey::CommandError::WrongPassword => { - if let Err(err) = pinentry::clear_pin(pin_type) { - return Err((new_data, err)); - } + pinentry::clear_pin(pin_type)?; retry -= 1; if retry > 0 { @@ -201,9 +195,9 @@ where continue; } let error = format!("{}: Wrong password", msg); - return Err((new_data, Error::Error(error))); + return Err(Error::Error(error)); } - err => return Err((new_data, get_error(msg, err))), + err => return Err(get_error(msg, err)), }, }; } @@ -220,7 +214,6 @@ where try_with_pin_and_data(pin_type, msg, (), |data, pin| { op(pin).map_err(|err| (data, err)) }) - .map_err(|(_data, err)| err) } /// Query and pretty print the status that is common to all Nitrokey devices. -- cgit v1.2.1