commonMain.ua.railian.data.result.DataResult-base.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of data-result-core Show documentation
Show all versions of data-result-core Show documentation
DataResult is a discriminated union that encapsulates a successful outcome
with a value of type [T] or a failure with an error of type [E].
The newest version!
@file:OptIn(ExperimentalContracts::class)
package ua.railian.data.result
import ua.railian.data.result.DataResult.Factory
import ua.railian.data.result.DataResult.Success
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
//region Factory
/**
* Builds a new [DataResult] by populating a [Factory] using the given [builder].
*/
public inline fun Factory.build(
builder: Factory.() -> DataResult,
): DataResult = builder()
//endregion
//region Transformations
/**
* Returns the encapsulated result of the given [transform] function
* applied to the encapsulated value if this instance represents [success][isSuccess]
* or the original encapsulated error if it is [failure][isFailure].
*
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
*/
public inline fun DataResult.map(
transform: (value: T) -> R,
): DataResult = flatMap { success(transform(it)) }
/**
* Returns the result of the given [transform] function
* applied to the encapsulated value if this instance represents [success][isSuccess]
* or the original result if it is [failure][isFailure].
*
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
*/
public inline fun DataResult.flatMap(
transform: Factory.(value: T) -> DataResult,
): DataResult {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
@Suppress("UNCHECKED_CAST")
return when {
isFailure() -> this as DataResult
else -> DataResult.transform(value)
}
}
/**
* Returns the encapsulated result of the given [transform] function
* applied to the encapsulated error if this instance represents [failure][isFailure]
* or the original encapsulated value if it is [success][isSuccess].
*
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
*/
public inline fun DataResult.recover(
transform: (error: E) -> R,
): Success = flatRecover { success(transform(it)) } as Success
/**
* Returns the result of the given [transform] function
* applied to the encapsulated error if this instance represents [failure][isFailure]
* or the original result if it is [success][isSuccess].
*
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
*/
public inline fun DataResult.flatRecover(
transform: Factory.(error: E) -> DataResult,
): DataResult {
contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) }
@Suppress("UNCHECKED_CAST")
return when {
isSuccess() -> this as DataResult
else -> DataResult.transform(error)
}
}
/**
* Returns the result of the given [transform] function applied to the original result.
*
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
*/
public inline fun DataResult.transform(
transform: Factory.(result: DataResult) -> DataResult,
): DataResult {
contract { callsInPlace(transform, InvocationKind.EXACTLY_ONCE) }
return DataResult.transform(this)
}
//endregion