Move CommandResolveResult.fold to extension, add CommandResolveResult.getOrElse

This commit is contained in:
Him188 2020-11-17 09:41:00 +08:00
parent 453d072cc7
commit 77f00d2d84
2 changed files with 27 additions and 17 deletions

View File

@ -32,22 +32,35 @@ public class CommandResolveResult private constructor(
public val failure: CommandExecuteResult.Failure?
get() = value.safeCast()
public inline fun <R> fold(
onSuccess: (ResolvedCommandCall?) -> R,
onFailure: (CommandExecuteResult.Failure) -> R,
): R {
contract {
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
}
failure?.let(onFailure)?.let { return it }
return call.let(onSuccess)
}
public constructor(call: ResolvedCommandCall?) : this(call as Any?)
public constructor(failure: CommandExecuteResult.Failure) : this(failure as Any)
}
@OptIn(ExperimentalCommandDescriptors::class)
public inline fun <R> CommandResolveResult.fold(
onSuccess: (ResolvedCommandCall?) -> R,
onFailure: (CommandExecuteResult.Failure) -> R,
): R {
contract {
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
}
failure?.let(onFailure)?.let { return it }
return call.let(onSuccess)
}
@OptIn(ExperimentalCommandDescriptors::class)
public inline fun CommandResolveResult.getOrElse(
onFailure: (CommandExecuteResult.Failure) -> ResolvedCommandCall,
): ResolvedCommandCall {
contract {
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
}
failure?.let(onFailure)?.let { return it }
return call!!
}
/**
* The resolver converting a [CommandCall] into [ResolvedCommandCall] based on registered []
*

View File

@ -193,11 +193,8 @@ internal suspend fun executeCommandImpl(
}
val resolved = call
.resolve().fold(
onSuccess = { it },
onFailure = { return it }
)
.ifNull { return CommandExecuteResult.UnresolvedCommand(call) }
.resolve()
.getOrElse { return it }
.let { raw ->
raw.intercepted()
.getOrElse { return CommandExecuteResult.Intercepted(call, raw, null, it) }