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
fun doWork(): Result<Unit> { | |
return input() into | |
::parse elseIf | |
{ | |
return Failure("Cannot parse email") | |
} into | |
::validate elseIf | |
{ | |
InvalidEmailAddress { message -> return Failure(message) } | |
EmptySubject(::fixEmptySubject) |
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
fun doWork(): Result<Unit> { | |
return input() into | |
::parse elseIf | |
{ | |
return Failure("Cannot parse email") | |
} into | |
::validate elseIf | |
{ | |
InvalidEmailAddress { message -> return Failure(message) } | |
EmptySubject { message -> return Failure(message) } |
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
send() elseIf { | |
IOFailure { error: IOException -> /* handle failure */ } | |
Timeout { error: TimeoutException -> /* handle failure */ } | |
UnAuthorized { error: UnAuthorizedException -> | |
/* handle failure */ | |
} | |
} |
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
public inline infix fun ValidationResult.elseIf(build: ValidationResultElseIfBuilder.() -> Unit): | |
Success { | |
if (this is Success) { | |
return this | |
} else { | |
val builder = ValidationResultElseIfBuilder(this) | |
builder.build() | |
return builder.result | |
} | |
} |
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
public class ValidationResultElseIfBuilder( | |
public val parent: ValidationResult | |
) { | |
public lateinit var result: Success | |
public inline fun InvalidEmailAddress(block: (errorMessage: String) -> Success): Unit { | |
if(parent is InvalidEmailAddress) { | |
result = block(parent.errorMessage) | |
} | |
} |
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
doWork() : Result<Email> { | |
... | |
validate() elseIf { | |
InvalidEmailAddress { errorMessage: String -> | |
return Failure(errorMessage) | |
} | |
EmptySubject { errorMessage: String -> /* handle failure */ } | |
EmptyBody { errorMessage: String -> /* handle failure */ } | |
} | |
... |
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
validate() elseIf { | |
InvalidEmailAddress { errorMessage: String -> | |
/* handle failure */ | |
} | |
EmptySubject { errorMessage: String -> /* handle failure */ } | |
EmptyBody { errorMessage: String -> /* handle failure */ } | |
} |
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
sealed class SendResult | |
data class SendSuccess(val email: Email): SendResult() | |
data class IOFailure(val error: IOException): SendResult() | |
data class Timeout(val error: TimeoutException): SendResult() | |
data class UnAuthorized(val error: UnAuthorizedException): SendResult() |
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
sealed class Result<T> | |
data class Success<T>(val value: T): Result<T>() | |
data class Failure<T>(val errorMessage: String): Result<T>() |
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
sealed class ValidationResult | |
data class ValidationSuccess(val email: Email): ValidationResult() | |
data class InvalidEmailAddress(val errorMessage: String): ValidationResult() | |
data class EmptySubject(val errorMessage: String): ValidationResult() | |
data class EmptyBody(val errorMessage: String): ValidationResult() |
NewerOlder