Was trying to follow advice from here
#include <iostream>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <string>
struct Config {
std::string name = "DefaultName";
int value = 42;
};
using Processors = rfl::Processors<rfl::DefaultIfMissing, rfl::NoExtraFields>;
int main() {
std::string toml_extra = R"(
name = "Test"
value = 100
unknown_field = "I should break this"
)";
auto res1 = rfl::toml::read<Config, Processors>(toml_extra);
if (!res1) {
std::cout << "Test 1 (Extra Fields) caught error: " << res1.error().what() << "\n";
} else {
std::cout << "Test 1 failed to catch extra field!\n";
}
auto res2 = rfl::toml::read<Config, rfl::DefaultIfMissing, rfl::NoExtraFields>(toml_extra);
if (!res2) {
std::cout << "Test 2 (Extra Fields) caught error: " << res2.error().what() << "\n";
} else {
std::cout << "Test 2 failed to catch extra field!\n";
}
std::string toml_missing = R"(
name = "OnlyName"
)";
auto res3 = rfl::toml::read<Config, Processors>(toml_missing);
if (!res3) {
std::cout << "Test 3 (Default) caught error: " << res3.error().what() << "\n";
} else {
std::cout << "Test 3 Result: Name=" << res3->name << ", Value=" << res3->value << "\n";
}
auto res4 = rfl::toml::read<Config, rfl::DefaultIfMissing, rfl::NoExtraFields>(toml_missing);
if (!res4) {
std::cout << "Test 4 (Default) caught error: " << res4.error().what() << "\n";
} else {
std::cout << "Test 4 Result: Name=" << res4->name << ", Value=" << res4->value << "\n";
}
return 0;
}
Test 1 failed to catch extra field!
Test 2 (Extra Fields) caught error: Value named 'unknown_field' not used. Remove the rfl::NoExtraFields processor or add rfl::ExtraFields to avoid this error message.
Test 3 (Default) caught error: Field named 'value' not found.
Test 4 Result: Name=OnlyName, Value=42
Was trying to follow advice from here