From f3a1b0b3c3cd628ecbfa45c3023956d44a4154a4 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sun, 14 Jul 2019 19:18:03 +0000 Subject: Supply ExecCtx to op used in with_* and try_with_* functions With an upcoming change we will require an ExecCtx in one of the op functions passed to the with_* and try_with_* functionality. To allow for such cases, this change adjusts the signature of those functions to provide a reference to such a context. --- nitrocli/src/commands.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index ccbff69..ea91727 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -111,7 +111,7 @@ where &pin_entry, "Could not access the password safe", (), - |_, pin| device.get_password_safe(pin).map_err(|err| ((), err)), + |_ctx, _, pin| device.get_password_safe(pin).map_err(|err| ((), err)), ) } @@ -127,7 +127,7 @@ fn authenticate( ) -> Result where D: Device, - F: FnMut(D, &str) -> result::Result, + F: FnMut(&mut args::ExecCtx<'_>, D, &str) -> result::Result, { let pin_entry = pinentry::PinEntry::from(pin_type, &device)?; @@ -144,7 +144,7 @@ where device, pinentry::PinType::User, "Could not authenticate as user", - |device, pin| device.authenticate_user(pin), + |_ctx, device, pin| device.authenticate_user(pin), ) } @@ -158,7 +158,7 @@ where device, pinentry::PinType::Admin, "Could not authenticate as admin", - |device, pin| device.authenticate_admin(pin), + |_ctx, device, pin| device.authenticate_admin(pin), ) } @@ -197,7 +197,7 @@ fn try_with_pin_and_data_with_pinentry( mut op: F, ) -> Result where - F: FnMut(D, &str) -> result::Result, + F: FnMut(&mut args::ExecCtx<'_>, D, &str) -> result::Result, E: error::TryInto, { let mut data = data; @@ -205,7 +205,7 @@ where let mut error_msg = None; loop { let pin = pinentry::inquire(ctx, pin_entry, pinentry::Mode::Query, error_msg)?; - match op(data, &pin) { + match op(ctx, data, &pin) { Ok(result) => return Ok(result), Err((new_data, err)) => match err.try_into() { Ok(err) => match err { @@ -237,12 +237,15 @@ fn try_with_pin_and_data( mut op: F, ) -> Result where - F: FnMut(D, &str) -> result::Result, + F: FnMut(&mut args::ExecCtx<'_>, D, &str) -> result::Result, E: Into + error::TryInto, { let pin = match pin_entry.pin_type() { - pinentry::PinType::Admin => &ctx.admin_pin, - pinentry::PinType::User => &ctx.user_pin, + // Ideally we would not clone here, but that would require us to + // restrict op to work with an immutable ExecCtx, which is not + // possible given that some clients print data. + pinentry::PinType::Admin => ctx.admin_pin.clone(), + pinentry::PinType::User => ctx.user_pin.clone(), }; if let Some(pin) = pin { @@ -252,7 +255,7 @@ where msg )) })?; - op(data, &pin).map_err(|(_, err)| err.into()) + op(ctx, data, &pin).map_err(|(_, err)| err.into()) } else { try_with_pin_and_data_with_pinentry(ctx, pin_entry, msg, data, op) } @@ -272,7 +275,7 @@ where F: FnMut(&str) -> result::Result<(), E>, E: Into + error::TryInto, { - try_with_pin_and_data(ctx, pin_entry, msg, (), |data, pin| { + try_with_pin_and_data(ctx, pin_entry, msg, (), |_ctx, data, pin| { op(pin).map_err(|err| (data, err)) }) } -- cgit v1.2.1