001/*
002 * Configurate
003 * Copyright (C) zml and Configurate 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 org.spongepowered.configurate.yaml;
018
019import org.checkerframework.checker.nullness.qual.Nullable;
020import org.yaml.snakeyaml.DumperOptions;
021
022import java.util.EnumMap;
023import java.util.Map;
024
025/**
026 * Style that can be used to represent a scalar.
027 *
028 * @since 4.2.0
029 */
030public enum ScalarStyle {
031
032    /**
033     * A double-quoted string.
034     *
035     * <pre>"hello world"</pre>
036     *
037     * @since 4.2.0
038     */
039    DOUBLE_QUOTED(DumperOptions.ScalarStyle.DOUBLE_QUOTED),
040
041    /**
042     * A single-quoted string.
043     *
044     * <pre>'hello world'</pre>
045     *
046     * @since 4.2.0
047     */
048    SINGLE_QUOTED(DumperOptions.ScalarStyle.SINGLE_QUOTED),
049
050    /**
051     * String without any quotation.
052     *
053     * <p>This may be ambiguous with non-string types.</p>
054     *
055     * @since 4.2.0
056     */
057    UNQUOTED(DumperOptions.ScalarStyle.PLAIN),
058
059    /**
060     * Folded scalar.
061     *
062     * <pre>{@code
063     * key: >
064     *   folded scalar
065     *   line breaks collapsed
066     * }</pre>
067     *
068     * @since 4.2.0
069     */
070    FOLDED(DumperOptions.ScalarStyle.FOLDED),
071
072    /**
073     * Literal scalar.
074     *
075     * <pre>{@code
076     * key: |
077     *   literal scalar
078     *   line breaks preserved
079     * }</pre>
080     *
081     * @since 4.2.0
082     */
083    LITERAL(DumperOptions.ScalarStyle.LITERAL)
084    ;
085
086    private static final Map<DumperOptions.ScalarStyle, ScalarStyle> BY_SNAKE = new EnumMap<>(DumperOptions.ScalarStyle.class);
087    private final DumperOptions.ScalarStyle snake;
088
089    ScalarStyle(final DumperOptions.ScalarStyle snake) {
090        this.snake = snake;
091    }
092
093    static DumperOptions.ScalarStyle asSnakeYaml(
094        final @Nullable ScalarStyle style,
095        final DumperOptions.@Nullable ScalarStyle fallback
096    ) {
097        if (style != null) {
098            return style.snake;
099        } else if (fallback != null) {
100            return fallback;
101        } else {
102            return DumperOptions.ScalarStyle.PLAIN;
103        }
104    }
105
106    static ScalarStyle fromSnakeYaml(final DumperOptions.ScalarStyle style) {
107        return BY_SNAKE.getOrDefault(style, UNQUOTED);
108    }
109
110    static {
111        for (final ScalarStyle style : values()) {
112            BY_SNAKE.put(style.snake, style);
113        }
114    }
115
116}