001/*
002 * PermissionsEx
003 * Copyright (C) zml and PermissionsEx contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ca.stellardrift.permissionsex.minecraft.command.argument;
018
019import ca.stellardrift.permissionsex.subject.SubjectType;
020import cloud.commandframework.context.CommandContext;
021
022import java.util.function.Function;
023
024/**
025 * Parsers used for our commands
026 */
027public final class Parsers {
028
029    private static final ContextValueParser CONTEXT_VALUE = new ContextValueParser();
030    private static final PermissionValueParser<Object> PERMISSION_VALUE = new PermissionValueParser<>();
031    private static final RankLadderParser<Object> RANK_LADDER = new RankLadderParser<>();
032    private static final SubjectTypeParser<Object> SUBJECT_TYPE = new SubjectTypeParser<>();
033    private static final PatternParser<Object> GREEDY_PATTERN = new PatternParser<>(true);
034    private static final PatternParser<Object> NON_GREEDY_PATTERN = new PatternParser<>(false);
035    private static final OptionValueParser<Object> OPTION_VALUE = new OptionValueParser<>();
036
037    public static ContextValueParser contextValue() {
038        return CONTEXT_VALUE;
039    }
040
041    @SuppressWarnings("unchecked")
042    public static <C> PermissionValueParser<C> permissionValue() {
043        return (PermissionValueParser<C>) PERMISSION_VALUE;
044    }
045
046    @SuppressWarnings("unchecked")
047    public static <C> RankLadderParser<C> rankLadder() {
048        return (RankLadderParser<C>) RANK_LADDER;
049    }
050
051    @SuppressWarnings("unchecked")
052    public static <C> SubjectTypeParser<C> subjectType() {
053        return (SubjectTypeParser<C>) SUBJECT_TYPE;
054    }
055
056    public static <C, I> SubjectIdentifierParser<C, I> subjectIdentifier(final SubjectType<I> type) {
057        return new SubjectIdentifierParser<C, I>(type);
058    }
059
060    @SuppressWarnings({"unchecked", "rawtypes"})
061    public static <C> SubjectIdentifierParser<C, Object> subjectIdentifier(final Function<CommandContext<C>, SubjectType<?>> typeExtractor) {
062        return new SubjectIdentifierParser<C, Object>((Function) typeExtractor);
063    }
064
065    /**
066     * Get a parser for a non-greedy pattern
067     *
068     * @param <C> the sender type
069     * @return a parser
070     */
071    @SuppressWarnings("unchecked")
072    public static <C> PatternParser<C> pattern() {
073        return (PatternParser<C>) NON_GREEDY_PATTERN;
074    }
075
076    /**
077     * Get a parser for a greedy pattern
078     *
079     * @param <C> the sender type
080     * @return a parser
081     */
082    @SuppressWarnings("unchecked")
083    public static <C> PatternParser<C> greedyPattern() {
084        return (PatternParser<C>) GREEDY_PATTERN;
085    }
086
087
088    /**
089     * Get a parser for an option value.
090     *
091     * <p>This will read a greedy string up until a flag start character is found, or the end of a quoted block is found</p>
092     *
093     * @param <C> the sender type
094     * @return a parser
095     */
096    @SuppressWarnings("unchecked")
097    public static <C> OptionValueParser<C> optionValue() {
098        return (OptionValueParser<C>) OPTION_VALUE;
099    }
100
101    private Parsers() {
102
103    }
104
105}