aboutsummaryrefslogblamecommitdiffstats
path: root/library/cpp/yt/string/enum.cpp
blob: 935c3e6e3ef0634ad919dffa62f1ecd400e0627d (plain) (tree)
1
2
3
4
5
6
7
8
                 
 
                   



                                                                                








                                                                            
                                                            
                                                       
                                                                 
                                     




                                                                                                                 
     

                      
                      
                                                           
                                                      


                                         
                                                                  


                            
                                         
 
                                            
 

                                                                                
#include "enum.h"

#include "format.h"

namespace NYT {

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

namespace NDetail {

void ThrowMalformedEnumValueException(TStringBuf typeName, TStringBuf value)
{
    throw TSimpleException(Format("Error parsing %v value %Qv",
        typeName,
        value));
}

template <bool ThrowOnError>
std::optional<TString> DecodeEnumValueImpl(TStringBuf value)
{
    auto camelValue = UnderscoreCaseToCamelCase(value);
    auto underscoreValue = CamelCaseToUnderscoreCase(camelValue);
    if (value != underscoreValue) {
        if constexpr (ThrowOnError) {
            throw TSimpleException(Format("Enum value %Qv is not in a proper underscore case; did you mean %Qv?",
                value,
                underscoreValue));
        } else {
            return std::nullopt;
        }
    }
    return camelValue;
}

} // namespace NDetail

std::optional<TString> TryDecodeEnumValue(TStringBuf value)
{
    return NDetail::DecodeEnumValueImpl<false>(value);
}

TString DecodeEnumValue(TStringBuf value)
{
    auto decodedValue = NDetail::DecodeEnumValueImpl<true>(value);
    YT_VERIFY(decodedValue);
    return *decodedValue;
}

TString EncodeEnumValue(TStringBuf value)
{
    return CamelCaseToUnderscoreCase(value);
}

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

} // namespace NYT