diff --git a/result/build.gradle.kts b/result/build.gradle.kts new file mode 100644 index 0000000..1cc3ee5 --- /dev/null +++ b/result/build.gradle.kts @@ -0,0 +1,6 @@ +dependencies { + testImplementation(testingLibraries.jUnit.jupiter) + testImplementation(testingLibraries.strikt.core) + + testRuntimeOnly(testingLibraries.jUnit.jupiter.engine) +} diff --git a/result/src/main/kotlin/io/connorwyatt/common/result/Result.kt b/result/src/main/kotlin/io/connorwyatt/common/result/Result.kt new file mode 100644 index 0000000..f31047e --- /dev/null +++ b/result/src/main/kotlin/io/connorwyatt/common/result/Result.kt @@ -0,0 +1,13 @@ +package io.connorwyatt.common.result + +sealed interface Result { + fun getOrThrow(): TSuccess = + when (this) { + is Success -> value + is Failure -> throw Exception("Result was a failure: $error") + } + + data class Success(val value: TSuccess) : Result + + data class Failure(val error: TFailure) : Result +} diff --git a/result/src/test/kotlin/io/connorwyatt/common/result/ResultTests.kt b/result/src/test/kotlin/io/connorwyatt/common/result/ResultTests.kt new file mode 100644 index 0000000..c1b83e0 --- /dev/null +++ b/result/src/test/kotlin/io/connorwyatt/common/result/ResultTests.kt @@ -0,0 +1,26 @@ +package io.connorwyatt.common.result + +import org.junit.jupiter.api.Test +import strikt.api.expectThat +import strikt.api.expectThrows +import strikt.assertions.isEqualTo + +class ResultTests { + @Test + fun `getOrThrow returns the value when Result is a success`() { + val value = "value" + + val result = Result.Success(value) + + expectThat(result.getOrThrow()).isEqualTo(value) + } + + @Test + fun `getOrThrow throws when Result is a failure`() { + val value = "value" + + val result = Result.Failure(value) + + expectThrows { result.getOrThrow() } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 689f864..e293ac5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -8,6 +8,7 @@ include(":http") include(":mongodb") include(":optional") include(":rabbitmq") +include(":result") include(":server") include(":time")