Added a to_schema method to the CustomParser

This commit is contained in:
Patrick Urbanke
2024-03-03 08:42:11 +01:00
parent d1a9bc6b61
commit c307c74cd6
+13 -9
View File
@@ -7,15 +7,14 @@
#include "../internal/has_to_class_method_v.hpp"
#include "../internal/to_ptr_field_tuple.hpp"
#include "Parser.hpp"
#include "schema/Type.hpp"
namespace rfl {
namespace parsing {
template <class ReaderType, class WriterType, class OriginalClass,
class HelperStruct>
template <class R, class W, class OriginalClass, class HelperStruct>
struct CustomParser {
static Result<OriginalClass> read(const ReaderType& _r,
const auto& _var) noexcept {
static Result<OriginalClass> read(const R& _r, const auto& _var) noexcept {
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
try {
if constexpr (internal::has_to_class_method_v<HelperStruct>) {
@@ -31,15 +30,20 @@ struct CustomParser {
return Error(e.what());
}
};
return Parser<ReaderType, WriterType, HelperStruct>::read(_r, _var)
.and_then(to_class);
return Parser<R, W, HelperStruct>::read(_r, _var).and_then(to_class);
}
template <class P>
static auto write(const WriterType& _w, const OriginalClass& _p,
static auto write(const W& _w, const OriginalClass& _p,
const P& _parent) noexcept {
Parser<ReaderType, WriterType, HelperStruct>::write(
_w, HelperStruct::from_class(_p), _parent);
Parser<R, W, HelperStruct>::write(_w, HelperStruct::from_class(_p),
_parent);
}
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return Parser<R, W, std::remove_cvref_t<HelperStruct>>::to_schema(
_definitions);
}
};