From 6b0dc6577733d2e20120f6b706b4459792707fa0 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 07:57:05 +0100 Subject: [PATCH] Add rfl::CaseInsensitiveEnums processor for case-insensitive enum parsing --- include/rfl.hpp | 1 + include/rfl/CaseInsensitiveEnums.hpp | 53 +++++++++++++++++++ .../rfl/internal/case_insensitive_enums_v.hpp | 30 +++++++++++ include/rfl/parsing/ParserEnum.hpp | 7 +++ tests/cli/test_case_insensitive_enums.cpp | 34 ++++++++++++ tests/json/test_case_insensitive_enums.cpp | 52 ++++++++++++++++++ 6 files changed, 177 insertions(+) create mode 100644 include/rfl/CaseInsensitiveEnums.hpp create mode 100644 include/rfl/internal/case_insensitive_enums_v.hpp create mode 100644 tests/cli/test_case_insensitive_enums.cpp create mode 100644 tests/json/test_case_insensitive_enums.cpp diff --git a/include/rfl.hpp b/include/rfl.hpp index 432fff2f6..6e5aa77d7 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -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" diff --git a/include/rfl/CaseInsensitiveEnums.hpp b/include/rfl/CaseInsensitiveEnums.hpp new file mode 100644 index 000000000..1d067ac02 --- /dev/null +++ b/include/rfl/CaseInsensitiveEnums.hpp @@ -0,0 +1,53 @@ +#ifndef RFL_CASEINSENSITIVEENUMS_HPP_ +#define RFL_CASEINSENSITIVEENUMS_HPP_ + +#include +#include +#include + +#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(json_str) +struct CaseInsensitiveEnums { + public: + template + 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` on no match +/// so numeric strings and error wording match standard enum parsing. +template +Result 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(_a[i])) != + std::tolower(static_cast(_b[i]))) { + return false; + } + } + return true; + }; + constexpr auto enumerators = get_enumerator_array(); + for (const auto& [name, val] : enumerators) { + if (eq_icase(name, _str)) { + return val; + } + } + return string_to_enum(std::string(_str)); +} + +} // namespace rfl + +#endif diff --git a/include/rfl/internal/case_insensitive_enums_v.hpp b/include/rfl/internal/case_insensitive_enums_v.hpp new file mode 100644 index 000000000..f6fdbf0f8 --- /dev/null +++ b/include/rfl/internal/case_insensitive_enums_v.hpp @@ -0,0 +1,30 @@ +#ifndef RFL_INTERNAL_CASEINSENSITIVEENUMS_HPP_ +#define RFL_INTERNAL_CASEINSENSITIVEENUMS_HPP_ + +#include + +#include "../CaseInsensitiveEnums.hpp" +#include "../Processors.hpp" + +namespace rfl::internal { + +template +struct case_insensitive_enums : std::false_type {}; + +template <> +struct case_insensitive_enums : std::true_type {}; + +template +struct case_insensitive_enums> { + static constexpr bool value = + (case_insensitive_enums::value || ... || + case_insensitive_enums::value); +}; + +template +constexpr bool case_insensitive_enums_v = + case_insensitive_enums>>::value; + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/parsing/ParserEnum.hpp b/include/rfl/parsing/ParserEnum.hpp index 4a216f434..8e31f0b8b 100644 --- a/include/rfl/parsing/ParserEnum.hpp +++ b/include/rfl/parsing/ParserEnum.hpp @@ -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" @@ -59,6 +60,12 @@ struct ParserEnum { "the underlying value."); return _r.template to_basic_type>(_var) .transform([](const auto _val) { return static_cast(_val); }); + } else if constexpr (internal::case_insensitive_enums_v) { + return _r.template to_basic_type(_var).and_then( + [](const std::string& _s) { + return rfl::parse_enum_icase< + T, internal::enum_names_only_v>(_s); + }); } else { return _r.template to_basic_type(_var).and_then( rfl::string_to_enum + +#include + +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( + {"--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( + {"--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( + {"--log-level=nope", "--channel-priority=Strict"}); + EXPECT_FALSE(bad); +} + +} // namespace test_cli_case_insensitive_enums diff --git a/tests/json/test_case_insensitive_enums.cpp b/tests/json/test_case_insensitive_enums.cpp new file mode 100644 index 000000000..2190770a0 --- /dev/null +++ b/tests/json/test_case_insensitive_enums.cpp @@ -0,0 +1,52 @@ +#include + +#include +#include +#include + +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( + 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( + 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(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( + R"({"log_level":"nope","priority":"Strict"})"); + EXPECT_FALSE(bad_name); + + const auto bad_num = + rfl::json::read( + R"({"log_level":"0","priority":"Strict"})"); + EXPECT_FALSE(bad_num); + + // Direct helper. + const auto direct = rfl::parse_enum_icase("TRACE"); + ASSERT_TRUE(direct); + EXPECT_EQ(*direct, LogLevel::trace); +} + +} // namespace test_case_insensitive_enums