All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.github.lyxnx.util.Result.kt Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
package io.github.lyxnx.util

import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.jvm.JvmSynthetic
import kotlin.Result as KotlinResult

/**
 * Similar to [runCatching], this will calls the [block] and returns its result if it was successful, catching
 * any [Exception]s that are thrown from the block and returning them as an [Err]
 *
 * The difference is that this function only catches the exceptions, not [Throwable]s, and as such [Error]s, which
 * should not be caught by normal business code
 */
@JvmSynthetic
public inline fun  catchExceptions(block: () -> T): Result {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }

    return try {
        Ok(block())
    } catch (e: Exception) {
        Err(e)
    }
}

/**
 * Maps this result into another result as the result of [block]
 *
 * Note that this function rethrows any [Throwable] exception thrown by [block].
 * See [flatMapCatching] for an alternative that encapsulates exceptions
 */
@JvmSynthetic
public inline fun  KotlinResult.flatMap(block: (T) -> KotlinResult): KotlinResult {
    return map { block(it).getOrThrow() }
}

/**
 * Maps this result into another result as the result of [block]
 *
 * This function catches any [Throwable] exception thrown by [block] and encapsulates it as a failure. See [flatMap] for
 * an alternative that rethrows exceptions
 */
@JvmSynthetic
public inline fun  KotlinResult.flatMapCatching(block: (T) -> KotlinResult): KotlinResult {
    return mapCatching { block(it).getOrThrow() }
}

/**
 * Maps this result into another result as the result of [block], if this result is a failure or the original result if
 * this is a success
 *
 * Note that this function rethrows any [Throwable] exception thrown by [block]. See [recoverCatchingFlatMap] for an
 * alternative that encapsulates exceptions
 */
@JvmSynthetic
public inline fun  KotlinResult.recoverFlatMap(block: (Throwable) -> KotlinResult): KotlinResult {
    return recover { block(it).getOrThrow() }
}

/**
 * Maps this result into another result as the result of [block], if this result is a failure or the original result if
 * this is a success
 *
 * This function catches any [Throwable] exception thrown by [block] and encapsulates it as a failure. See [recoverFlatMap] for
 * an alternative that rethrows exceptions
 */
@JvmSynthetic
public inline fun  KotlinResult.recoverCatchingFlatMap(block: (Throwable) -> KotlinResult): KotlinResult {
    return recoverCatching { block(it).getOrThrow() }
}

/**
 * Returns a result representing a success when only the error type is needed and whether the operation was successful
 *
 * For example, this could be used to represent the result of an operation that returns nothing but may throw an
 * exception; this would allow checking of the state in a safe manner
 */
@JvmSynthetic
public fun KotlinResult.Companion.success(): KotlinResult = success(Unit)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy