refactor: use runSuspendCatching from kotlin-result

This commit is contained in:
Harsh Shandilya 2023-09-28 01:29:24 +05:30
parent 5f3aa611c9
commit 41c86b67f3
No known key found for this signature in database
4 changed files with 5 additions and 54 deletions

View file

@ -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 <V> runSuspendCatching(block: () -> V): Result<V, Throwable> {
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, V> T.runSuspendCatching(
block: T.() -> V
): Result<V, Throwable> {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return try {
Ok(block())
} catch (e: CancellationException) {
throw e
} catch (e: Throwable) {
Err(e)
}
}

View file

@ -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)

View file

@ -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

View file

@ -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"