aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-09 15:15:55 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-09 15:15:55 -0800
commit886788a95a3c79a7664cbcd0bfffa1e500461adf (patch)
tree35b5f3704e27c185bd71c7b18f59da57415ec651
parentac7025cbe1bc46d25dd978138c6b397d77853232 (diff)
downloadnitrocli-886788a95a3c79a7664cbcd0bfffa1e500461adf.tar.gz
nitrocli-886788a95a3c79a7664cbcd0bfffa1e500461adf.tar.bz2
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.
-rw-r--r--nitrocli/src/commands.rs19
1 files 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<nitrokey::PasswordSafe<'_>>
(),
|_, 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<A, (D, nitrokey::CommandError)>,
{
- 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<D, F, R>(
msg: &'static str,
data: D,
op: F,
-) -> result::Result<R, (D, Error)>
+) -> Result<R>
where
F: Fn(D, &str) -> result::Result<R, (D, nitrokey::CommandError)>,
{
@@ -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.