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

in.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. Deprecation Notice for group ID "in.specmatic" ****************************************************************************************************** Updates for "specmatic-core" will no longer be available under the deprecated group ID "in.specmatic". Please update your dependencies to use the new group ID "io.specmatic". ******************************************************************************************************

The newest version!
package `in`.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)
}