draconisplusplus/include/rfl/parsing/Parser_variant.hpp

112 lines
3.9 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_PARSING_PARSER_VARIANT_HPP_
#define RFL_PARSING_PARSER_VARIANT_HPP_
#include <map>
#include <optional>
#include <type_traits>
#include <variant>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "FieldVariantParser.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
#include "to_single_error_message.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
template <class R, class W, class... FieldTypes, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::variant<FieldTypes...>>
class Parser<R, W, std::variant<FieldTypes...>, ProcessorsType> {
public:
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
2024-05-31 22:59:00 -04:00
2024-06-08 15:53:06 -04:00
static Result<std::variant<FieldTypes...>>
read(const R& _r, const InputVarType& _var) noexcept {
2024-06-08 14:10:59 -04:00
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
2024-06-16 00:13:15 -04:00
return FieldVariantParser<R, W, ProcessorsType, FieldTypes...>::read(_r, _var);
2024-06-08 14:10:59 -04:00
} else {
std::optional<std::variant<FieldTypes...>> result;
2024-06-08 15:53:06 -04:00
std::vector<Error> errors;
2024-06-08 14:10:59 -04:00
read_variant(_r, _var, &result, &errors);
if (result) {
return std::move(*result);
} else {
return Error(to_single_error_message(
2024-06-16 00:13:15 -04:00
errors,
"Could not parse the variant. Each of the "
"possible alternatives failed "
"for the following reasons: ",
100000
2024-06-08 15:53:06 -04:00
));
2024-06-08 14:10:59 -04:00
}
}
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...>& _variant, const P& _parent) noexcept {
2024-06-08 14:10:59 -04:00
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
2024-06-16 00:13:15 -04:00
FieldVariantParser<R, W, ProcessorsType, FieldTypes...>::write(_w, _variant, _parent);
2024-06-08 14:10:59 -04:00
} else {
const auto handle = [&](const auto& _v) {
using Type = std::remove_cvref_t<decltype(_v)>;
Parser<R, W, Type, ProcessorsType>::write(_w, _v, _parent);
};
return std::visit(handle, _variant);
}
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <size_t _i = 0>
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
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
2024-06-16 00:13:15 -04:00
return FieldVariantParser<R, W, ProcessorsType, FieldTypes...>::to_schema(_definitions);
2024-06-08 14:10:59 -04:00
} else {
using Type = schema::Type;
constexpr size_t size = sizeof...(FieldTypes);
if constexpr (_i == size) {
2024-06-16 00:13:15 -04:00
return Type { Type::AnyOf { .types_ = _types } };
2024-06-08 14:10:59 -04:00
} else {
2024-06-16 00:13:15 -04:00
using U =
std::remove_cvref_t<std::variant_alternative_t<_i, std::variant<FieldTypes...>>>;
_types.push_back(Parser<R, W, U, ProcessorsType>::to_schema(_definitions));
2024-06-08 14:10:59 -04:00
return to_schema<_i + 1>(_definitions, std::move(_types));
}
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
private:
template <int _i = 0>
static void read_variant(
2024-06-16 00:13:15 -04:00
const R& _r,
const InputVarType& _var,
std::optional<std::variant<FieldTypes...>>* _result,
std::vector<Error>* _errors
2024-06-08 15:53:06 -04:00
) noexcept {
2024-06-08 14:10:59 -04:00
constexpr size_t size = sizeof...(FieldTypes);
if constexpr (_i < size) {
2024-06-16 00:13:15 -04:00
using AltType =
std::remove_cvref_t<std::variant_alternative_t<_i, std::variant<FieldTypes...>>>;
2024-06-08 14:10:59 -04:00
auto res = Parser<R, W, AltType, ProcessorsType>::read(_r, _var);
if (res) {
*_result = std::move(*res);
return;
} else {
_errors->emplace_back(*res.error());
return read_variant<_i + 1>(_r, _var, _result, _errors);
}
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace parsing
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif