Shift to idiomatic use of `map_err`
Test: keystore2_test
Test: legacykeystore_test
Flag: none, pure refactoring
Change-Id: I4b9f1b0d47145846764ff46676b10035f7f2fb6a
diff --git a/keystore2/legacykeystore/lib.rs b/keystore2/legacykeystore/lib.rs
index 41b8685..8e6040b 100644
--- a/keystore2/legacykeystore/lib.rs
+++ b/keystore2/legacykeystore/lib.rs
@@ -210,25 +210,22 @@
}
}
-/// Translate errors into service specific exceptions.
+/// Translate an error into a service specific exception, logging along the way.
///
-/// All errors are logged, except for ERROR_ENTRY_NOT_FOUND error. `Error::Error(x)` variants get
-/// mapped onto a service specific error code of `x`, other errors are mapped to
-/// `ERROR_SYSTEM_ERROR`.
-fn map_or_log_err<T>(result: Result<T>) -> BinderResult<T> {
- result.map_err(|e| {
- let root_cause = e.root_cause();
- let (rc, log_error) = match root_cause.downcast_ref::<Error>() {
- // Make the entry not found errors silent.
- Some(Error::Error(ERROR_ENTRY_NOT_FOUND)) => (ERROR_ENTRY_NOT_FOUND, false),
- Some(Error::Error(e)) => (*e, true),
- Some(Error::Binder(_, _)) | None => (ERROR_SYSTEM_ERROR, true),
- };
- if log_error {
- log::error!("{:?}", e);
- }
- BinderStatus::new_service_specific_error(rc, anyhow_error_to_cstring(&e).as_deref())
- })
+/// `Error::Error(x)` variants get mapped onto a service specific error code of `x`, other errors
+/// are mapped to `ERROR_SYSTEM_ERROR`.
+fn into_logged_binder(e: anyhow::Error) -> BinderStatus {
+ let root_cause = e.root_cause();
+ let (rc, log_error) = match root_cause.downcast_ref::<Error>() {
+ // Make the entry not found errors silent.
+ Some(Error::Error(ERROR_ENTRY_NOT_FOUND)) => (ERROR_ENTRY_NOT_FOUND, false),
+ Some(Error::Error(e)) => (*e, true),
+ Some(Error::Binder(_, _)) | None => (ERROR_SYSTEM_ERROR, true),
+ };
+ if log_error {
+ log::error!("{:?}", e);
+ }
+ BinderStatus::new_service_specific_error(rc, anyhow_error_to_cstring(&e).as_deref())
}
fn ensure_keystore_put_is_enabled() -> Result<()> {
@@ -536,19 +533,19 @@
impl ILegacyKeystore for LegacyKeystoreService {
fn get(&self, alias: &str, uid: i32) -> BinderResult<Vec<u8>> {
let _wp = wd::watch("ILegacyKeystore::get");
- map_or_log_err(self.legacy_keystore.get(alias, uid))
+ self.legacy_keystore.get(alias, uid).map_err(into_logged_binder)
}
fn put(&self, alias: &str, uid: i32, entry: &[u8]) -> BinderResult<()> {
let _wp = wd::watch("ILegacyKeystore::put");
- map_or_log_err(self.legacy_keystore.put(alias, uid, entry))
+ self.legacy_keystore.put(alias, uid, entry).map_err(into_logged_binder)
}
fn remove(&self, alias: &str, uid: i32) -> BinderResult<()> {
let _wp = wd::watch("ILegacyKeystore::remove");
- map_or_log_err(self.legacy_keystore.remove(alias, uid))
+ self.legacy_keystore.remove(alias, uid).map_err(into_logged_binder)
}
fn list(&self, prefix: &str, uid: i32) -> BinderResult<Vec<String>> {
let _wp = wd::watch("ILegacyKeystore::list");
- map_or_log_err(self.legacy_keystore.list(prefix, uid))
+ self.legacy_keystore.list(prefix, uid).map_err(into_logged_binder)
}
}