aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/enum-inl.h
blob: 84b0941d15ec1b67578e425949e85cf6f0c98d15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef ENUM_INL_H_
#error "Direct inclusion of this file is not allowed, include enum.h"
// For the sake of sane code completion.
#include "enum.h"
#endif

#include "format.h"
#include "string.h"
#include "string_builder.h"

#include <library/cpp/yt/exception/exception.h>

#include <util/string/printf.h>
#include <util/string/strip.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

[[noreturn]]
void ThrowMalformedEnumValueException(
    TStringBuf typeName,
    TStringBuf value);

void FormatUnknownEnumValue(
    auto* builder,
    TStringBuf name,
    auto value)
{
    builder->AppendFormat("%v::unknown-%v", name, ToUnderlying(value));
}

} // namespace NDetail

template <class T>
std::optional<T> TryParseEnum(TStringBuf str, bool enableUnknown)
{
    auto tryParseToken = [&] (TStringBuf token) -> std::optional<T> {
        if (auto optionalValue = TEnumTraits<T>::FindValueByLiteral(token)) {
            return *optionalValue;

        }

        if (auto optionalDecodedValue = TryDecodeEnumValue(token)) {
            if (auto optionalValue = TEnumTraits<T>::FindValueByLiteral(*optionalDecodedValue)) {
                return *optionalValue;
            }
        }

        if (enableUnknown) {
            if constexpr (constexpr auto optionalUnknownValue = TEnumTraits<T>::TryGetUnknownValue()) {
                return *optionalUnknownValue;
            }
        }

        return std::nullopt;
    };

    if constexpr (TEnumTraits<T>::IsBitEnum) {
        T result{};
        TStringBuf token;
        while (str.NextTok('|', token)) {
            if (auto optionalValue = tryParseToken(StripString(token))) {
                result |= *optionalValue;
            } else {
                return {};
            }
        }
        return result;
    } else {
        return tryParseToken(str);
    }
}

template <class T>
T ParseEnum(TStringBuf str)
{
    if (auto optionalResult = TryParseEnum<T>(str, /*enableUnkown*/ true)) {
        return *optionalResult;
    }
    NYT::NDetail::ThrowMalformedEnumValueException(TEnumTraits<T>::GetTypeName(), str);
}

template <class T>
void FormatEnum(TStringBuilderBase* builder, T value, bool lowerCase)
{
    auto formatLiteral = [&] (auto* builder, TStringBuf literal) {
        if (lowerCase) {
            CamelCaseToUnderscoreCase(builder, literal);
        } else {
            builder->AppendString(literal);
        }
    };

    if constexpr (TEnumTraits<T>::IsBitEnum) {
        if (None(value)) {
            // Avoid empty string if possible.
            if (auto optionalLiteral = TEnumTraits<T>::FindLiteralByValue(value)) {
                formatLiteral(builder, *optionalLiteral);
            }
            return;
        }

        TDelimitedStringBuilderWrapper delimitedBuilder(builder, " | ");

        T printedValue{};
        for (auto currentValue : TEnumTraits<T>::GetDomainValues()) {
            // Check if currentValue is viable and non-redunant.
            if ((value & currentValue) == currentValue && (printedValue | currentValue) != printedValue) {
                formatLiteral(&delimitedBuilder, *TEnumTraits<T>::FindLiteralByValue(currentValue));
                printedValue |= currentValue;
            }
        }

        // Handle the remainder.
        if (printedValue != value) {
            NYT::NDetail::FormatUnknownEnumValue(&delimitedBuilder, TEnumTraits<T>::GetTypeName(), value & ~printedValue);
        }
    } else {
        if (auto optionalLiteral = TEnumTraits<T>::FindLiteralByValue(value)) {
            formatLiteral(builder, *optionalLiteral);
            return;
        }

        NYT::NDetail::FormatUnknownEnumValue(builder, TEnumTraits<T>::GetTypeName(), value);
    }
}

template <class T>
TString FormatEnum(T value)
{
    TStringBuilder builder;
    FormatEnum(&builder, value, /*lowerCase*/ true);
    return builder.Flush();
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT