Interface TryParser<R extends @NonNull Object,T extends @NonNull Object>

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
  • 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,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.

      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

      @Pure ParserFunction<R,T> 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 interface ParserFunction<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 the default (functionally the same as if the argument was missing, but without causing an error if the parameter is required).
      Throws:
      InvalidArgumentException - if the given argument is not a valid value.