-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract recover functions to separate file
- Loading branch information
1 parent
325cebc
commit 54b53d3
Showing
6 changed files
with
415 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.github.michaelbull.result | ||
|
||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
/** | ||
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err], | ||
* otherwise this [Ok]. | ||
*/ | ||
public inline infix fun <V, E> Result<V, E>.recover(transform: (E) -> V): Ok<V> { | ||
contract { | ||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) | ||
} | ||
|
||
return when (this) { | ||
is Ok -> this | ||
is Err -> Ok(transform(error)) | ||
} | ||
} | ||
|
||
/** | ||
* Returns the [transformation][transform] of the [error][Err.error], catching and encapsulating any | ||
* thrown exception as a failure if this [Result] is [Err], otherwise this [Ok]. | ||
*/ | ||
public inline infix fun <V, E> Result<V, E>.recoverCatching(transform: (E) -> V): Result<V, Throwable> { | ||
contract { | ||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) | ||
} | ||
|
||
return when (this) { | ||
is Ok -> this | ||
is Err -> { | ||
try { | ||
Ok(transform(error)) | ||
} catch (e: Throwable) { | ||
Err(e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err] | ||
* and satisfies the given [predicate], otherwise this [Result]. | ||
*/ | ||
public inline fun <V, E> Result<V, E>.recoverIf(predicate: (E) -> Boolean, transform: (E) -> V): Result<V, E> { | ||
contract { | ||
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) | ||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) | ||
} | ||
|
||
return when (this) { | ||
is Ok -> this | ||
is Err -> if (predicate(error)) { | ||
Ok(transform(error)) | ||
} else { | ||
this | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err] | ||
* and _does not_ satisfy the given [predicate], otherwise this [Result]. | ||
*/ | ||
public inline fun <V, E> Result<V, E>.recoverUnless(predicate: (E) -> Boolean, transform: (E) -> V): Result<V, E> { | ||
contract { | ||
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) | ||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) | ||
} | ||
|
||
return when (this) { | ||
is Ok -> this | ||
is Err -> if (!predicate(error)) { | ||
Ok(transform(error)) | ||
} else { | ||
this | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err], | ||
* otherwise this [Result]. | ||
*/ | ||
public inline fun <V, E> Result<V, E>.andThenRecover(transform: (E) -> Result<V, E>): Result<V, E> { | ||
contract { | ||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) | ||
} | ||
|
||
return when (this) { | ||
is Ok -> this | ||
is Err -> transform(error) | ||
} | ||
} | ||
|
||
/** | ||
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err] and | ||
* satisfies the given [predicate], otherwise this [Result]. | ||
*/ | ||
public inline fun <V, E> Result<V, E>.andThenRecoverIf( | ||
predicate: (E) -> Boolean, | ||
transform: (E) -> Result<V, E> | ||
): Result<V, E> { | ||
contract { | ||
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) | ||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) | ||
} | ||
|
||
return when (this) { | ||
is Ok -> this | ||
is Err -> if (predicate(error)) { | ||
transform(error) | ||
} else { | ||
this | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err] | ||
* and _does not_ satisfy the given [predicate], otherwise this [Result]. | ||
*/ | ||
public inline fun <V, E> Result<V, E>.andThenRecoverUnless( | ||
predicate: (E) -> Boolean, | ||
transform: (E) -> Result<V, E> | ||
): Result<V, E> { | ||
contract { | ||
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) | ||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) | ||
} | ||
|
||
return when (this) { | ||
is Ok -> this | ||
is Err -> if (!predicate(error)) { | ||
transform(error) | ||
} else { | ||
this | ||
} | ||
} | ||
} |
Oops, something went wrong.