Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "rfl/Box.hpp"
#include "rfl/Bytestring.hpp"
#include "rfl/CamelCaseToSnakeCase.hpp"
#include "rfl/CaseInsensitiveEnums.hpp"
#include "rfl/CodePoints.hpp"
#include "rfl/Commented.hpp"
#include "rfl/DefaultIfMissing.hpp"
Expand Down
53 changes: 53 additions & 0 deletions include/rfl/CaseInsensitiveEnums.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef RFL_CASEINSENSITIVEENUMS_HPP_
#define RFL_CASEINSENSITIVEENUMS_HPP_

#include <cctype>
#include <string>
#include <string_view>

#include "Result.hpp"
#include "enums.hpp"

namespace rfl {

/// A processor that enables case-insensitive ASCII matching of declared enum
/// enumerator names when reading enums across any format (JSON, YAML, TOML,
/// CLI, etc.).
/// Usage: rfl::json::read<MyStruct, rfl::CaseInsensitiveEnums>(json_str)
struct CaseInsensitiveEnums {
public:
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

/// Case-insensitive ASCII lookup for declared enumerators of `EnumType`.
/// Falls back to `rfl::string_to_enum<EnumType, _enum_names_only>` on no match
/// so numeric strings and error wording match standard enum parsing.
template <class EnumType, bool _enum_names_only = false>
Result<EnumType> parse_enum_icase(const std::string_view _str) noexcept {
const auto eq_icase = [](std::string_view _a, std::string_view _b) noexcept {
if (_a.size() != _b.size()) {
return false;
}
for (size_t i = 0; i < _a.size(); ++i) {
if (std::tolower(static_cast<unsigned char>(_a[i])) !=
std::tolower(static_cast<unsigned char>(_b[i]))) {
return false;
}
}
return true;
};
constexpr auto enumerators = get_enumerator_array<EnumType>();
for (const auto& [name, val] : enumerators) {
if (eq_icase(name, _str)) {
return val;
}
}
return string_to_enum<EnumType, _enum_names_only>(std::string(_str));
}

} // namespace rfl

#endif
30 changes: 30 additions & 0 deletions include/rfl/internal/case_insensitive_enums_v.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef RFL_INTERNAL_CASEINSENSITIVEENUMS_HPP_
#define RFL_INTERNAL_CASEINSENSITIVEENUMS_HPP_

#include <type_traits>

#include "../CaseInsensitiveEnums.hpp"
#include "../Processors.hpp"

namespace rfl::internal {

template <class T>
struct case_insensitive_enums : std::false_type {};

template <>
struct case_insensitive_enums<CaseInsensitiveEnums> : std::true_type {};

template <class Head, class... Tail>
struct case_insensitive_enums<Processors<Head, Tail...>> {
static constexpr bool value =
(case_insensitive_enums<Head>::value || ... ||
case_insensitive_enums<Tail>::value);
};

template <class T>
constexpr bool case_insensitive_enums_v =
case_insensitive_enums<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

} // namespace rfl::internal

#endif
7 changes: 7 additions & 0 deletions include/rfl/parsing/ParserEnum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../Result.hpp"
#include "../config.hpp"
#include "../enums.hpp"
#include "../internal/case_insensitive_enums_v.hpp"
#include "../internal/enum_names_only_v.hpp"
#include "../internal/enums/is_flag_enum.hpp"
#include "../internal/enums/is_scoped_enum.hpp"
Expand Down Expand Up @@ -59,6 +60,12 @@ struct ParserEnum {
"the underlying value.");
return _r.template to_basic_type<std::underlying_type_t<T>>(_var)
.transform([](const auto _val) { return static_cast<T>(_val); });
} else if constexpr (internal::case_insensitive_enums_v<ProcessorsType>) {
return _r.template to_basic_type<std::string>(_var).and_then(
[](const std::string& _s) {
return rfl::parse_enum_icase<
T, internal::enum_names_only_v<ProcessorsType>>(_s);
});
} else {
return _r.template to_basic_type<std::string>(_var).and_then(
rfl::string_to_enum<T, internal::enum_names_only_v<
Expand Down
34 changes: 34 additions & 0 deletions tests/cli/test_case_insensitive_enums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <gtest/gtest.h>

#include <rfl/cli.hpp>

namespace test_cli_case_insensitive_enums {

enum class LogLevel { critical, err, warn, info, debug, trace, off };
enum class ChannelPriority { Disabled, Flexible, Strict };

struct AppOpts {
LogLevel log_level = LogLevel::info;
ChannelPriority channel_priority = ChannelPriority::Flexible;
};

TEST(cli, test_case_insensitive_enums) {
const auto r1 = rfl::cli::read<AppOpts, rfl::CaseInsensitiveEnums>(
{"--log-level=DEBUG", "--channel-priority=STRICT"});
ASSERT_TRUE(r1) << r1.error().what();
EXPECT_EQ(r1->log_level, LogLevel::debug);
EXPECT_EQ(r1->channel_priority, ChannelPriority::Strict);

const auto r2 = rfl::cli::read<AppOpts, rfl::CaseInsensitiveEnums>(
{"--log-level=WaRn", "--channel-priority=disabled"});
ASSERT_TRUE(r2) << r2.error().what();
EXPECT_EQ(r2->log_level, LogLevel::warn);
EXPECT_EQ(r2->channel_priority, ChannelPriority::Disabled);

const auto bad =
rfl::cli::read<AppOpts, rfl::CaseInsensitiveEnums, rfl::EnumNamesOnly>(
{"--log-level=nope", "--channel-priority=Strict"});
EXPECT_FALSE(bad);
}

} // namespace test_cli_case_insensitive_enums
52 changes: 52 additions & 0 deletions tests/json/test_case_insensitive_enums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <gtest/gtest.h>

#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>

namespace test_case_insensitive_enums {

enum class LogLevel { critical, err, warn, info, debug, trace, off };
enum class ChannelPriority { Disabled, Flexible, Strict };

struct Config {
LogLevel log_level = LogLevel::info;
ChannelPriority priority = ChannelPriority::Flexible;
};

TEST(json, test_case_insensitive_enums) {
const auto r1 = rfl::json::read<Config, rfl::CaseInsensitiveEnums>(
R"({"log_level":"DEBUG","priority":"strict"})");
ASSERT_TRUE(r1) << r1.error().what();
EXPECT_EQ(r1->log_level, LogLevel::debug);
EXPECT_EQ(r1->priority, ChannelPriority::Strict);

const auto r2 = rfl::json::read<Config, rfl::CaseInsensitiveEnums>(
R"({"log_level":"WaRn","priority":"DiSaBlEd"})");
ASSERT_TRUE(r2) << r2.error().what();
EXPECT_EQ(r2->log_level, LogLevel::warn);
EXPECT_EQ(r2->priority, ChannelPriority::Disabled);

// Without CaseInsensitiveEnums, wrong case is rejected.
const auto strict_fail =
rfl::json::read<Config>(R"({"log_level":"DEBUG","priority":"Strict"})");
EXPECT_FALSE(strict_fail);

// Combined with EnumNamesOnly, unknown strings and numeric strings fail.
const auto bad_name =
rfl::json::read<Config, rfl::CaseInsensitiveEnums, rfl::EnumNamesOnly>(
R"({"log_level":"nope","priority":"Strict"})");
EXPECT_FALSE(bad_name);

const auto bad_num =
rfl::json::read<Config, rfl::CaseInsensitiveEnums, rfl::EnumNamesOnly>(
R"({"log_level":"0","priority":"Strict"})");
EXPECT_FALSE(bad_num);

// Direct helper.
const auto direct = rfl::parse_enum_icase<LogLevel, true>("TRACE");
ASSERT_TRUE(direct);
EXPECT_EQ(*direct, LogLevel::trace);
}

} // namespace test_case_insensitive_enums