- Type Parameters:
R
- The raw type.T
- The parsed type.
- All Superinterfaces:
BiFunction<CommandContext,
,R, Mono<TryParser.Result<R, T>>> ParserFunction<R,
TryParser.Result<R, T>>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface
public interface TryParser<R extends @NonNull Object,T extends @NonNull Object>
extends ParserFunction<R,TryParser.Result<R,T>>
A parser wrapper that tolerates errors by returning a result that reports either
the parsed item or an encountered error.
This allows for the receiver of the argument to do their own handling of encountered errors rather than immediately stopping processing.
- Since:
- 1.0
- Version:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final record
A result in which the item was invalid.static interface
The result of parsing.static final record
A result in which the item was parsed successfully.Nested classes/interfaces inherited from interface dev.sympho.modular_commands.api.command.parameter.parse.ParserFunction
ParserFunction.PostParser<R extends @NonNull Object,
I extends @NonNull Object, T extends @NonNull Object, P1 extends @NonNull ParserFunction<R, I>, P2 extends @NonNull ParserFunction<I, T>> -
Method Summary
Modifier and TypeMethodDescriptionerrors
(List<TryParser.Result<R, T>> results) Extracts the items in a result list that had errors.items
(List<TryParser.Result<R, T>> results) Extracts the items in a result list that were successfully parsed.of
(ParserFunction<R, T> parser) Creates a try-parser wrapper the given parser.default Mono<TryParser.Result<R,
T>> parse
(CommandContext context, R item) Parses the given raw argument from the user into the corresponding value.parser()
The parser to delegate to.static <R extends @NonNull Object,
T extends @NonNull Object>
Tuple2<List<T>,List<TryParser.Failure<R, T>>> split
(List<TryParser.Result<R, T>> results) Splits the given result list into a list of successfully parsed items and a list of failed items.Methods inherited from interface java.util.function.BiFunction
andThen
Methods inherited from interface dev.sympho.modular_commands.api.command.parameter.parse.ParserFunction
apply, then
-
Method Details
-
of
@SideEffectFree static <R extends @NonNull Object,T extends @NonNull Object> TryParser<R,T> of(ParserFunction<R, T> parser) Creates a try-parser wrapper the given parser.- Type Parameters:
R
- The raw type.T
- The parsed type.- Parameters:
parser
- The parser to wrap.- Returns:
- The try-parser.
-
items
@SideEffectFree static <R extends @NonNull Object,T extends @NonNull Object> List<T> items(List<TryParser.Result<R, T>> results) Extracts the items in a result list that were successfully parsed.The returned list contains the items in the same relative order that they appear in the given list.
- Type Parameters:
R
- The raw type.T
- The parsed type.- Parameters:
results
- The list of results.- Returns:
- The successfully parsed items.
-
errors
@SideEffectFree static <R extends @NonNull Object,T extends @NonNull Object> List<TryParser.Failure<R,T>> errors(List<TryParser.Result<R, T>> results) Extracts the items in a result list that had errors.The returned list contains the items in the same relative order that they appear in the given list.
- Type Parameters:
R
- The raw type.T
- The parsed type.- Parameters:
results
- The list of results.- Returns:
- The invalid items.
-
split
@SideEffectFree static <R extends @NonNull Object,T extends @NonNull Object> Tuple2<List<T>,List<TryParser.Failure<R, splitT>>> (List<TryParser.Result<R, T>> results) Splits the given result list into a list of successfully parsed items and a list of failed items.The returned lists contains the items in the same relative order that they appear in the given list.
- Type Parameters:
R
- The raw type.T
- The parsed type.- Parameters:
results
- The list of results.- Returns:
- The parsed and invalid items as discrete lists.
-
parser
The parser to delegate to.- Returns:
- The parser.
-
parse
default Mono<TryParser.Result<R,T>> parse(CommandContext context, R item) throws InvalidArgumentException Description copied from interface:ParserFunction
Parses the given raw argument from the user into the corresponding value.- Specified by:
parse
in interfaceParserFunction<R extends @NonNull Object,
T extends @NonNull Object> - Parameters:
context
- The execution context.item
- The raw argument received from the user.- Returns:
- A Mono that issues the parsed argument. If the raw value is invalid, it may
fail with a
InvalidArgumentException
. May be empty, in which case the value defers to thedefault
(functionally the same as if the argument was missing, but without causing an error if the parameter isrequired
). - Throws:
InvalidArgumentException
- if the given argument is not a valid value.
-