From 41c86b67f3f1fe320e702b05887f22188a5b42fc Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 28 Sep 2023 01:29:24 +0530 Subject: refactor: use `runSuspendCatching` from `kotlin-result` --- .../util/coroutines/RunSuspendCatching.kt | 51 ---------------------- crypto/pgpainless/build.gradle.kts | 2 +- .../app/passwordstore/crypto/PGPKeyManager.kt | 2 +- gradle/libs.versions.toml | 4 +- 4 files changed, 5 insertions(+), 54 deletions(-) delete mode 100644 coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt diff --git a/coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt b/coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt deleted file mode 100644 index 677c2bc3..00000000 --- a/coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt +++ /dev/null @@ -1,51 +0,0 @@ -@file:OptIn(ExperimentalContracts::class) -@file:Suppress("RedundantSuspendModifier") - -package app.passwordstore.util.coroutines - -import com.github.michaelbull.result.Err -import com.github.michaelbull.result.Ok -import com.github.michaelbull.result.Result -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract -import kotlinx.coroutines.CancellationException - -/** - * Calls the specified function [block] and returns its encapsulated result if invocation was - * successful, catching any [Throwable] except [CancellationException] that was thrown from the - * [block] function execution and encapsulating it as a failure. - */ -@OptIn(ExperimentalContracts::class) -public suspend inline fun runSuspendCatching(block: () -> V): Result { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - - return try { - Ok(block()) - } catch (e: CancellationException) { - throw e - } catch (e: Throwable) { - Err(e) - } -} - -/** - * Calls the specified function [block] with [this] value as its receiver and returns its - * encapsulated result if invocation was successful, catching any [Throwable] except - * [CancellationException] that was thrown from the [block] function execution and encapsulating it - * as a failure. - */ -@OptIn(ExperimentalContracts::class) -public suspend inline infix fun T.runSuspendCatching( - block: T.() -> V -): Result { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - - return try { - Ok(block()) - } catch (e: CancellationException) { - throw e - } catch (e: Throwable) { - Err(e) - } -} diff --git a/crypto/pgpainless/build.gradle.kts b/crypto/pgpainless/build.gradle.kts index 29f5bb1f..bfaff4f7 100644 --- a/crypto/pgpainless/build.gradle.kts +++ b/crypto/pgpainless/build.gradle.kts @@ -6,11 +6,11 @@ plugins { id("com.github.android-password-store.kotlin-jvm-library") } dependencies { api(projects.crypto.common) - implementation(projects.coroutineUtils) implementation(libs.androidx.annotation) implementation(libs.dagger.hilt.core) implementation(libs.kotlinx.coroutines.core) implementation(libs.thirdparty.kotlinResult) + implementation(libs.thirdparty.kotlinResult.coroutines) implementation(libs.thirdparty.pgpainless) testImplementation(libs.bundles.testDependencies) testImplementation(libs.kotlinx.coroutines.test) diff --git a/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt b/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt index aed1acf2..8aa11803 100644 --- a/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt +++ b/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt @@ -17,8 +17,8 @@ import app.passwordstore.crypto.errors.KeyDirectoryUnavailableException import app.passwordstore.crypto.errors.KeyNotFoundException import app.passwordstore.crypto.errors.NoKeysAvailableException import app.passwordstore.crypto.errors.UnusableKeyException -import app.passwordstore.util.coroutines.runSuspendCatching import com.github.michaelbull.result.Result +import com.github.michaelbull.result.coroutines.runSuspendCatching import com.github.michaelbull.result.unwrap import java.io.File import javax.inject.Inject diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4b0c6589..d4669be6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,7 @@ coroutines = "1.7.3" flowbinding = "1.2.0" hilt = "2.48" kotlin = "1.9.10" +kotlinResult = "1.1.18" leakcanary = "2.12" lifecycle = "2.6.2" @@ -81,7 +82,8 @@ thirdparty-flowbinding-android = { module = "io.github.reactivecircus.flowbindin # JGit upgrades also raise its minimum Java runtime requirements that we cannot satisfy on Android # noinspection GradleDependency thirdparty-jgit = "org.eclipse.jgit:org.eclipse.jgit:3.7.1.201504261725-r" -thirdparty-kotlinResult = "com.michael-bull.kotlin-result:kotlin-result:1.1.18" +thirdparty-kotlinResult = { module = "com.michael-bull.kotlin-result:kotlin-result", version.ref = "kotlinResult" } +thirdparty-kotlinResult-coroutines = { module = "com.michael-bull.kotlin-result:kotlin-result-coroutines", version.ref = "kotlinResult" } thirdparty-leakcanary-core = { module = "com.squareup.leakcanary:leakcanary-android-core", version.ref = "leakcanary" } thirdparty-logcat = "com.squareup.logcat:logcat:0.1" thirdparty-modernAndroidPrefs = "de.maxr1998:modernandroidpreferences:2.3.2" -- cgit v1.2.3