- Type Parameters:
T- The parsed type.
- All Superinterfaces:
BiFunction<CommandContext,,String, Mono<T>> ParserFunction<String,T>
- All Known Subinterfaces:
FunctorUrlParser<T>
- All Known Implementing Classes:
ChannelRefUrlParser,ChannelUrlParser,EntityRefUrlParser,EntityUrlParser,FunctorUrlParser.Choice,FunctorUrlParser.ChoiceBase,FunctorUrlParser.PostParser,MessageRefUrlParser,MessageUrlParser,UrlParser.Choice,UrlParser.ChoiceBase,UrlParser.PostParser
- Since:
- 1.0
- Version:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classUrlParser.Choice<T extends @NonNull Object>Parser that supports multiple URL types by delegating to one of a list of parsers.static classBase for a parser that supports multiple URL types by delegating to one of a list of parsers.static classUrlParser.PostParser<I extends @NonNull Object,T extends @NonNull Object, P1 extends @NonNull UrlParser<I>, P2 extends @NonNull ParserFunction<I, T>> A composed parser that first applies this parser to one parser, and then applies the results to a second parser. -
Field Summary
FieldsModifier and TypeFieldDescriptionPattern that matches a masked link. -
Method Summary
Modifier and TypeMethodDescriptionCreates a parser that delegates to the given parsers.choice(Collection<? extends UrlParser<T>> parsers) Creates a parser that delegates to the given parsers.Creates a parser that delegates to other parsers, chosen by the given mapping function based on the URL to parse.choiceHost(Map<String, ? extends UrlParser<T>> parsers) Creates a parser that delegates to a parser depending on the host of the URL, using the given host-parser mappings.static URLParses the given string into a URL.parse(CommandContext context, String raw) Parses the given raw argument from the user into the corresponding value.parse(CommandContext context, URL url) Parses the given URL.Attempts to parse a string as a URL.parseUrl(String raw, Collection<String> allowedProtocols) Attempts to parse a string as a URL.static StringstripFormatting(String raw) Clears special URL formatting from the given string, if any exists.booleanChecks if the given URL is supported by this parser.then(ParserFunction<T, V> after) Returns a composed parser that first applies this parser to its input, and then applies the after parser to the result.Methods inherited from interface java.util.function.BiFunction
andThenMethods inherited from interface dev.sympho.modular_commands.api.command.parameter.parse.ParserFunction
apply
-
Field Details
-
MASKED_URL_PATTERN
Pattern that matches a masked link.
-
-
Method Details
-
choice
static <T extends @NonNull Object> UrlParser<T> choice(Function<URL, @Nullable UrlParser<T>> parserMapper) Creates a parser that delegates to other parsers, chosen by the given mapping function based on the URL to parse.- Type Parameters:
T- The parsed argument type.- Parameters:
parserMapper- The function to use to determine which parser to delegate to for a given URL.- Returns:
- The parser.
-
choice
Creates a parser that delegates to the given parsers.The parser choice is defined as the first parser in the iteration order of the given collection for which
supports(URL)returnstruefor the URL being parsed. This implies that the iteration order matters if, and only if, there are URLs that may be supported by more than one of the parsers in the collection.- Type Parameters:
T- The parsed argument type.- Parameters:
parsers- The parsers to delegate to.- Returns:
- The parser.
-
choice
Creates a parser that delegates to the given parsers.The parser choice is defined as the first parser in the given order for which
supports(URL)returnstruefor the URL being parsed. This implies that the iteration order matters if, and only if, there are URLs that may be supported by more than one of the parsers in the collection.- Type Parameters:
T- The parsed argument type.- Parameters:
parsers- The parsers to delegate to.- Returns:
- The parser.
-
choiceHost
static <T extends @NonNull Object> UrlParser<T> choiceHost(Map<String, ? extends UrlParser<T>> parsers) Creates a parser that delegates to a parser depending on the host of the URL, using the given host-parser mappings.- Type Parameters:
T- The parsed argument type.- Parameters:
parsers- The host-parser mappings to delegate to.- Returns:
- The parser.
-
stripFormatting
Clears special URL formatting from the given string, if any exists.The following formatting types are supported:
- Supressed links (links surrounded by
<>) - Masked links (links that display as clickable text)
Note that layered formats are supported if supported by Discord; for example,
[Discord](<https://discord.com>)is a link that is both supressed and masked.- Parameters:
raw- The string to strip.- Returns:
- The stripped string.
- Supressed links (links surrounded by
-
getUrl
Parses the given string into a URL.- Parameters:
raw- The string to parse.- Returns:
- The URL.
- Throws:
InvalidArgumentException- if the given string is not a valid URL.- API Note:
- Mainly a wrapper to convert the URL exception into the invalid argument type.
-
parseUrl
@SideEffectFree static @Nullable URL parseUrl(String raw, Collection<String> allowedProtocols) throws InvalidArgumentException Attempts to parse a string as a URL.Some effort is made to detect URLs surrounded by special markup; see
stripFormatting(String).- Parameters:
raw- The string to parse.allowedProtocols- The acceptable protocols.- Returns:
- The parsed URL if one could be parsed, or
nullif the given string is not a URL or has a protocol that is not contained inallowedProtocols. - Throws:
InvalidArgumentException- if the given string has a URL format (and an allowed protocol) but is invalid in some way.
-
parseUrl
@SideEffectFree static @Nullable URL parseUrl(String raw, String allowedProtocol) throws InvalidArgumentException Attempts to parse a string as a URL.Some effort is made to detect URLs surrounded by special markup; see
stripFormatting(String).- Parameters:
raw- The string to parse.allowedProtocol- The acceptable protocol.- Returns:
- The parsed URL if one could be parsed, or
nullif the given string is not a URL or does not have theallowedProtocol. - Throws:
InvalidArgumentException- if the given string has a URL format (and the allowed protocol) but is invalid in some way.
-
supports
Checks if the given URL is supported by this parser. If this returnsfalse, callingparse(CommandContext, URL)with the same URL will result in an error.Note that the opposite isn't necessarily true; it is possible for this method to return
truefor a given URL whileparse(CommandContext, URL)results in an error. That just means that the basic structure of the URL was detected as being compatible with this parser (for example, having a particular host and/or protocol), but was malformed or a variant that the parser doesn't support.- Parameters:
url- The URL to check.- Returns:
- Whether the URL is compatible with this parser.
-
parse
Parses the given URL.- Parameters:
context- The execution context.url- The URL to parse.- Returns:
- The parsed value. May result in a
InvalidArgumentExceptionif the URL is invalid. - Throws:
InvalidArgumentException- if the URL is invalid.
-
parse
Description copied from interface:ParserFunctionParses the given raw argument from the user into the corresponding value.- Specified by:
parsein interfaceParserFunction<String,T extends @NonNull Object> - Parameters:
context- The execution context.raw- 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.
-
then
Returns a composed parser that first applies this parser to its input, and then applies the after parser to the result. If parsing with either parser throws an exception, it is relayed to the caller of the composed parser.
-