Skip to content

Commit 65dfc9d

Browse files
committed
feat(kotlin): extension functions for new parser methods
1 parent 5db60e5 commit 65dfc9d

3 files changed

Lines changed: 98 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2024 Incendo
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.extension
25+
26+
import cloud.commandframework.arguments.parser.ArgumentParseResult
27+
import kotlin.jvm.optionals.getOrNull
28+
29+
/**
30+
* Returns the [ArgumentParseResult.parsedValue], or `null`.
31+
*/
32+
public fun <T : Any> ArgumentParseResult<T>.component1(): T? = parsedValue().getOrNull()
33+
34+
/**
35+
* Returns the [ArgumentParseResult.failure], or `null`.
36+
*/
37+
public fun <T : Any> ArgumentParseResult<T>.component2(): Throwable? = failure().getOrNull()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2024 Incendo
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.extension
25+
26+
import cloud.commandframework.types.Either
27+
import kotlin.jvm.optionals.getOrNull
28+
29+
/**
30+
* Returns the [Either.primary] value, or `null`.
31+
*/
32+
public operator fun <U : Any, V : Any> Either<U, V>.component1(): U? = primary().getOrNull()
33+
34+
/**
35+
* Returns the [Either.fallback] value, or `null`.
36+
*/
37+
public operator fun <U : Any, V : Any> Either<U, V>.component2(): V? = fallback().getOrNull()

cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/ParserDescriptorExtensions.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
//
2424
package cloud.commandframework.kotlin.extension
2525

26+
import cloud.commandframework.arguments.parser.ArgumentParseResult
2627
import cloud.commandframework.arguments.parser.ArgumentParser
28+
import cloud.commandframework.arguments.parser.MappedArgumentParser.Mapper
2729
import cloud.commandframework.arguments.parser.ParserDescriptor
30+
import cloud.commandframework.context.CommandContext
31+
import java.util.concurrent.CompletableFuture
2832

2933
/**
3034
* Returns a [ParserDescriptor] that describes [this] parser.
@@ -41,3 +45,23 @@ public inline fun <C, reified T> ArgumentParser<C, T>.asDescriptor(): ParserDesc
4145
*/
4246
public inline fun <C, reified T> parserDescriptor(parser: ArgumentParser<C, T>): ParserDescriptor<C, T> =
4347
parser.asDescriptor()
48+
49+
/**
50+
* Creates a descriptor for a flat-mapped parser.
51+
*/
52+
public inline fun <C, T, reified O> ParserDescriptor<C, T>.flatMap(mapper: Mapper<C, T, O>): ParserDescriptor<C, O> =
53+
flatMap(O::class.java, mapper)
54+
55+
/**
56+
* Creates a descriptor for a flat-mapped parser.
57+
*/
58+
public inline fun <C, T, reified O> ParserDescriptor<C, T>.flatMapSuccess(
59+
noinline mapper: (CommandContext<C>, T) -> CompletableFuture<ArgumentParseResult<O>>
60+
): ParserDescriptor<C, O> = flatMapSuccess(O::class.java, mapper)
61+
62+
/**
63+
* Creates a descriptor for a mapped parser.
64+
*/
65+
public inline fun <C, T, reified O> ParserDescriptor<C, T>.mapSuccess(
66+
noinline mapper: (CommandContext<C>, T) -> CompletableFuture<O>
67+
): ParserDescriptor<C, O> = mapSuccess(O::class.java, mapper)

0 commit comments

Comments
 (0)