Simplify map_or_log_err
All (non-test) invocations use the same second argument `Ok` (which
actually acts as a 1-arg closure `|v| Ok(v)`), so no need to have it as
a parameter. The common second argument just maps `Ok(v)` to `Ok(v)`,
which means that `map_err()` can be used instead of `map_or_else()`,
and the same type parameter is used for both input and output.
Test: legacykeystore_test
Test: keystore2_test
Flag: None, pure refactor
Change-Id: I46b6e327d0b546df6be6664781a52bb888c04eca
diff --git a/keystore2/src/apc.rs b/keystore2/src/apc.rs
index bdde5ae..8fda1f2 100644
--- a/keystore2/src/apc.rs
+++ b/keystore2/src/apc.rs
@@ -82,33 +82,20 @@
/// `selinux::Error::perm()` is mapped on `ResponseCode::PERMISSION_DENIED`.
///
/// All non `Error` error conditions get mapped onto ResponseCode::SYSTEM_ERROR`.
-///
-/// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed
-/// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it
-/// typically returns Ok(value).
-pub fn map_or_log_err<T, U, F>(result: Result<U>, handle_ok: F) -> BinderResult<T>
-where
- F: FnOnce(U) -> BinderResult<T>,
-{
- result.map_or_else(
- |e| {
- log::error!("{:#?}", e);
- let root_cause = e.root_cause();
- let rc = match root_cause.downcast_ref::<Error>() {
- Some(Error::Rc(rcode)) => rcode.0,
- Some(Error::Binder(_, _)) => ResponseCode::SYSTEM_ERROR.0,
- None => match root_cause.downcast_ref::<selinux::Error>() {
- Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0,
- _ => ResponseCode::SYSTEM_ERROR.0,
- },
- };
- Err(BinderStatus::new_service_specific_error(
- rc,
- anyhow_error_to_cstring(&e).as_deref(),
- ))
- },
- handle_ok,
- )
+pub fn map_or_log_err<T>(result: Result<T>) -> BinderResult<T> {
+ result.map_err(|e| {
+ log::error!("{:#?}", e);
+ let root_cause = e.root_cause();
+ let rc = match root_cause.downcast_ref::<Error>() {
+ Some(Error::Rc(rcode)) => rcode.0,
+ Some(Error::Binder(_, _)) => ResponseCode::SYSTEM_ERROR.0,
+ None => match root_cause.downcast_ref::<selinux::Error>() {
+ Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0,
+ _ => ResponseCode::SYSTEM_ERROR.0,
+ },
+ };
+ BinderStatus::new_service_specific_error(rc, anyhow_error_to_cstring(&e).as_deref())
+ })
}
/// Rate info records how many failed attempts a client has made to display a protected
@@ -354,20 +341,23 @@
) -> BinderResult<()> {
// presentPrompt can take more time than other operations.
let _wp = wd::watch_millis("IProtectedConfirmation::presentPrompt", 3000);
- map_or_log_err(
- self.present_prompt(listener, prompt_text, extra_data, locale, ui_option_flags),
- Ok,
- )
+ map_or_log_err(self.present_prompt(
+ listener,
+ prompt_text,
+ extra_data,
+ locale,
+ ui_option_flags,
+ ))
}
fn cancelPrompt(
&self,
listener: &binder::Strong<dyn IConfirmationCallback>,
) -> BinderResult<()> {
let _wp = wd::watch("IProtectedConfirmation::cancelPrompt");
- map_or_log_err(self.cancel_prompt(listener), Ok)
+ map_or_log_err(self.cancel_prompt(listener))
}
fn isSupported(&self) -> BinderResult<bool> {
let _wp = wd::watch("IProtectedConfirmation::isSupported");
- map_or_log_err(Self::is_supported(), Ok)
+ map_or_log_err(Self::is_supported())
}
}