scrow-commands/common/site/lab0x13/scrow/commands/model/argument/Argument.java
kento2 7c322707ba
Some checks failed
Build and Deploy artifact / build (push) Failing after 11s
initial commit
2026-08-25 15:37:39 +02:00

41 lines
1.5 KiB
Java

package site.lab0x13.scrow.commands.model.argument;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import site.lab0x13.scrow.commands.model.CommandContext;
import site.lab0x13.scrow.commands.model.Node;
import site.lab0x13.scrow.commands.model.SuggestionProvider;
import java.util.List;
/**
* @param <C> argument of command context
* @param <V> argument of argument value
*/
public record Argument<C extends CommandContext<C>, V>(
String id,
ArgumentType<C, V> type,
List<ArgumentRequirement<C, V>> requirements,
SuggestionProvider<C> suggestionProvider,
DefaultValueProvider<C, V> defaultValueProvider
) implements Node<C> {
public static <C extends CommandContext<C>, V> ArgumentBuilder<C, V> builder(String id, ArgumentType<C, V> type) {
return new ArgumentBuilder<>(id, type);
}
public Result<ParsedArgument<C, V>, String> parse(C ctx, String rawInput) {
var parseResult = type.parseInput(rawInput);
if (parseResult.hasFailure())
return parseResult.mapSuccess(_ -> null);
var value = parseResult.getSuccess().orElseThrow();
for (var requirement : requirements) {
var isAllowed = requirement.predicate().check(ctx, value);
if (!isAllowed)
return Results.failure(requirement.errorMessage());
}
return Results.success(new ParsedArgument<>(this, rawInput, value));
}
}