From 03b4fc9f00534152e8a1946a670d9ec3091fc60c Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 31 Jul 2023 09:51:18 +0300 Subject: [PATCH] Add Result utils --- .../org/koitharu/kotatsu/parsers/util/Result.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt index 31357404..e5be1293 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt @@ -13,3 +13,17 @@ inline fun T.runCatchingCancellable(block: T.() -> R): Result { Result.failure(e) } } + +inline fun Result.recoverCatchingCancellable(transform: (exception: Throwable) -> R): Result { + return when (val exception = exceptionOrNull()) { + null -> this + else -> runCatchingCancellable { transform(exception) } + } +} + +inline fun Result.recoverNotNull(transform: (exception: Throwable) -> R?): Result { + return when (val exception = exceptionOrNull()) { + null -> this + else -> transform(exception)?.let(Result.Companion::success) ?: this + } +}