|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2022 Alexander Söderberg & Contributors |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +package cloud.commandframework.kotlin.coroutines |
| 25 | + |
| 26 | +import cloud.commandframework.CommandManager |
| 27 | +import cloud.commandframework.arguments.parser.ArgumentParser |
| 28 | +import cloud.commandframework.arguments.parser.ParserDescriptor |
| 29 | +import cloud.commandframework.arguments.suggestion.SuggestionFactory |
| 30 | +import cloud.commandframework.context.CommandContext |
| 31 | +import cloud.commandframework.context.CommandInput |
| 32 | +import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator |
| 33 | +import cloud.commandframework.execution.CommandExecutionCoordinator |
| 34 | +import kotlinx.coroutines.CoroutineScope |
| 35 | +import kotlinx.coroutines.GlobalScope |
| 36 | +import kotlinx.coroutines.future.future |
| 37 | +import kotlin.coroutines.CoroutineContext |
| 38 | +import kotlin.coroutines.EmptyCoroutineContext |
| 39 | + |
| 40 | +/** |
| 41 | + * Suspending version of [ArgumentParser] for use with coroutines. |
| 42 | + * |
| 43 | + * NOTE: It is highly advised to not use [CommandExecutionCoordinator.SimpleCoordinator] together |
| 44 | + * with coroutine support. Consider using [AsynchronousCommandExecutionCoordinator] instead. |
| 45 | + * |
| 46 | + * @param C command sender type. |
| 47 | + */ |
| 48 | +public fun interface SuspendingArgumentParser<C : Any, T : Any> { |
| 49 | + /** |
| 50 | + * Returns the result of parsing the given [commandInput]. |
| 51 | + * |
| 52 | + * This method may be called when a command chain is being parsed for execution |
| 53 | + * (using [CommandManager.executeCommand]) |
| 54 | + * or when a command is being parsed to provide context for suggestions |
| 55 | + * (using [SuggestionFactory.suggest]). |
| 56 | + * It is possible to use [CommandContext.isSuggestions] to see what the purpose of the |
| 57 | + * parsing is. Particular care should be taken when parsing for suggestions, as the parsing |
| 58 | + * method is then likely to be called once for every character written by the command sender. |
| 59 | + * |
| 60 | + * The parser is assumed to be completely stateless and should not store any information about |
| 61 | + * the command sender or the command context. Instead, information should be stored in the |
| 62 | + * [CommandContext]. |
| 63 | + * |
| 64 | + * @param commandContext Command context |
| 65 | + * @param commandInput Command Input |
| 66 | + * @return the result |
| 67 | + */ |
| 68 | + public suspend operator fun invoke(commandContext: CommandContext<C>, commandInput: CommandInput): T |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a new [ArgumentParser] backed by this [SuspendingArgumentParser]. |
| 72 | + * |
| 73 | + * @param scope coroutine scope |
| 74 | + * @param context coroutine context |
| 75 | + * @return new [ArgumentParser] |
| 76 | + */ |
| 77 | + public fun asArgumentParser( |
| 78 | + scope: CoroutineScope = GlobalScope, |
| 79 | + context: CoroutineContext = EmptyCoroutineContext |
| 80 | + ): ArgumentParser<C, T> = createArgumentParser(scope, context, this) |
| 81 | + |
| 82 | + public companion object { |
| 83 | + /** |
| 84 | + * Creates a new [ArgumentParser] backed by the given [SuspendingArgumentParser]. |
| 85 | + * |
| 86 | + * @param scope coroutine scope |
| 87 | + * @param context coroutine context |
| 88 | + * @param parser suspending parser |
| 89 | + * @return new [ArgumentParser] |
| 90 | + */ |
| 91 | + public fun <C : Any, T : Any> createArgumentParser( |
| 92 | + scope: CoroutineScope = GlobalScope, |
| 93 | + context: CoroutineContext = EmptyCoroutineContext, |
| 94 | + parser: SuspendingArgumentParser<C, T> |
| 95 | + ): ArgumentParser<C, T> = ArgumentParser.FutureArgumentParser { ctx, commandInput -> |
| 96 | + scope.future(context) { |
| 97 | + parser(ctx, commandInput) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Creates a new [ParserDescriptor] backed by this [SuspendingArgumentParser]. |
| 105 | + * |
| 106 | + * @param scope coroutine scope |
| 107 | + * @param context coroutine context |
| 108 | + * @return the descriptor |
| 109 | + */ |
| 110 | +public inline fun <C : Any, reified T : Any> SuspendingArgumentParser<C, T>.asParserDescriptor( |
| 111 | + scope: CoroutineScope = GlobalScope, |
| 112 | + context: CoroutineContext = EmptyCoroutineContext |
| 113 | +): ParserDescriptor<C, T> = ParserDescriptor.of(this.asArgumentParser(scope, context), T::class.java) |
| 114 | + |
| 115 | +/** |
| 116 | + * Creates a suspending argument parser backed by the given [parser]. |
| 117 | + * |
| 118 | + * @param scope coroutine scope |
| 119 | + * @param context coroutine context |
| 120 | + * @return the descriptor |
| 121 | + */ |
| 122 | +public suspend inline fun <C : Any, reified T : Any> suspendingArgumentParser( |
| 123 | + scope: CoroutineScope = GlobalScope, |
| 124 | + context: CoroutineContext = EmptyCoroutineContext, |
| 125 | + crossinline parser: suspend (CommandContext<C>, CommandInput) -> T |
| 126 | +): ParserDescriptor<C, T> = SuspendingArgumentParser<C, T> { commandContext, commandInput -> |
| 127 | + parser(commandContext, commandInput) |
| 128 | +}.asParserDescriptor(scope, context) |
0 commit comments