diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 081ae0f4..0a1c5a90 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -352,7 +352,7 @@ struct Parser> { InputVarType* _var) noexcept { const auto to_map = [&](auto _obj) { return _r.to_map(&_obj); }; const auto build = [&](auto _map) { - return build_named_tuple_recursively(_r, _map); + return build_named_tuple(_r, _map); }; return _r.to_object(_var).transform(to_map).and_then(build); } @@ -368,42 +368,39 @@ struct Parser> { private: /// Builds the named tuple field by field. template - static Result> build_named_tuple_recursively( - const ReaderType& _r, const std::map& _map, - Args&&... _args) noexcept { - const auto size = sizeof...(Args); - - if constexpr (size == sizeof...(FieldTypes)) { - return NamedTuple(std::move(_args)...); - } else { - using FieldType = typename std::tuple_element< - size, typename NamedTuple::Fields>::type; - - using ValueType = std::decay_t; - - const auto key = FieldType::name_.str(); - - const auto it = _map.find(key); - - if (it == _map.end()) { - if constexpr (is_required()) { - return Error("Field named '" + key + "' not found!"); - } else { - return build_named_tuple_recursively( - _r, _map, std::move(_args)..., FieldType(ValueType())); - } - } - - const auto build = [&](auto&& _value) { - return build_named_tuple_recursively( - _r, _map, std::move(_args)..., - FieldType(std::move(_value))); - }; - - return get_value(_r, *it).and_then(build); + static Result> build_named_tuple( + const ReaderType& _r, + const std::map& _map) noexcept { + try { + return NamedTuple( + build_one_field(_r, _map).value()...); + } catch (std::exception& e) { + return Error(e.what()); } } + /// Builds a single field for the named tuple. + template + static Result build_one_field( + const ReaderType& _r, + const std::map& _map) noexcept { + using ValueType = std::decay_t; + + const auto key = FieldType::name_.str(); + + const auto it = _map.find(key); + + if (it == _map.end()) { + if constexpr (is_required()) { + return Error("Field named '" + key + "' not found!"); + } else { + return FieldType(ValueType()); + } + } + + return get_value(_r, *it); + } + /// Builds the object field by field. template static void build_object_recursively(const WriterType& _w,