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

io.specmatic.core.RailwayOrientedProgramming.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.

There is a newer version: 2.0.37
Show newest version
package io.specmatic.core

sealed class MatchingResult
data class MatchSuccess(val value: T) : MatchingResult()
data class MatchFailure(val error: Result.Failure) : MatchingResult()

infix fun  MatchingResult.then(f: (T) -> MatchingResult) =
        when (this) {
            is MatchSuccess -> f(this.value)
            is MatchFailure -> MatchFailure(this.error)
        }

// Pipe input: the beginning of a railway
infix fun  T.to(f: (T) -> MatchingResult) = MatchSuccess(this) then f

// Handle error output: the end of a railway
infix fun  MatchingResult.otherwise(f: (Result.Failure) -> MatchingResult) =
        if (this is MatchFailure) f(this.error) else this

infix fun  MatchingResult.toResult(f: (Result) -> Result) =
        when (this) {
            is MatchSuccess -> f(Result.Success())
            is MatchFailure -> f(this.error)
        }

fun  handleError(error: Result.Failure): MatchingResult = MatchFailure(error)

fun returnResult(result: Result) = result

fun  summarize(parameters: Triple>): MatchingResult>> {
    val (_, _, failures) = parameters

    return if(failures.isNotEmpty())
        MatchFailure(Result.Failure.fromFailures(failures))
    else
        MatchSuccess(parameters)
}