draconisplusplus/include/rfl/parsing/FieldVariantParser.hpp

91 lines
3 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_PARSING_FIELD_VARIANT_PARSER_HPP_
#define RFL_PARSING_FIELD_VARIANT_PARSER_HPP_
#include <map>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <variant>
#include "../Result.hpp"
#include "../always_false.hpp"
#include "FieldVariantReader.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace parsing {
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// To be used when all options of the variants are rfl::Field. Essentially,
/// this is an externally tagged union.
template <class R, class W, class ProcessorsType, class... FieldTypes>
requires AreReaderAndWriter<R, W, std::variant<FieldTypes...>>
struct FieldVariantParser {
using FieldVariantType = std::variant<FieldTypes...>;
using ResultType = Result<FieldVariantType>;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
public:
using InputObjectType = typename R::InputObjectType;
using InputVarType = typename R::InputVarType;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
using OutputObjectType = typename W::OutputObjectType;
using OutputVarType = typename W::OutputVarType;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
static ResultType read(const R& _r, const InputVarType& _var) noexcept {
static_assert(
2024-06-16 00:13:15 -04:00
internal::no_duplicate_field_names<std::tuple<FieldTypes...>>(),
"Externally tagged variants cannot have duplicate field "
"names."
2024-06-08 15:53:06 -04:00
);
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
const auto to_result = [&](const auto _obj) -> ResultType {
2024-06-08 15:53:06 -04:00
auto field_variant = std::optional<Result<FieldVariantType>>();
2024-06-08 14:10:59 -04:00
const auto reader =
2024-06-16 00:13:15 -04:00
FieldVariantReader<R, W, ProcessorsType, FieldTypes...>(&_r, &field_variant);
2024-06-08 14:10:59 -04:00
auto err = _r.read_object(reader, _obj);
2024-06-09 18:55:00 -04:00
if (err) {
return *err;
}
2024-06-08 14:10:59 -04:00
if (!field_variant) {
return Error(
2024-06-16 00:13:15 -04:00
"Could not parse: Expected the object to have "
"exactly one field, but found more than one."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
return std::move(*field_variant);
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
return _r.to_object(_var).and_then(to_result);
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class P>
2024-06-16 00:13:15 -04:00
static void
write(const W& _w, const std::variant<FieldTypes...>& _v, const P& _parent) noexcept {
2024-06-08 14:10:59 -04:00
static_assert(
2024-06-16 00:13:15 -04:00
internal::no_duplicate_field_names<std::tuple<FieldTypes...>>(),
"Externally tagged variants cannot have duplicate field "
"names."
2024-06-08 15:53:06 -04:00
);
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
const auto handle = [&](const auto& _field) {
2024-06-16 00:13:15 -04:00
const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field));
using NamedTupleType = std::remove_cvref_t<decltype(named_tuple)>;
Parser<R, W, NamedTupleType, ProcessorsType>::write(_w, named_tuple, _parent);
2024-06-08 14:10:59 -04:00
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
std::visit(handle, _v);
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
static schema::Type to_schema(
2024-06-16 00:13:15 -04:00
std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type> _types = {}
2024-06-08 15:53:06 -04:00
) {
2024-06-08 14:10:59 -04:00
using VariantType = std::variant<NamedTuple<FieldTypes>...>;
2024-06-16 00:13:15 -04:00
return Parser<R, W, VariantType, ProcessorsType>::to_schema(_definitions);
2024-06-08 14:10:59 -04:00
}
};
} // namespace parsing
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif