#ifndef RFL_PARSING_PARSER_DEFAULT_HPP_ #define RFL_PARSING_PARSER_DEFAULT_HPP_ #include #include #include "rfl/Result.hpp" #include "rfl/always_false.hpp" #include "rfl/from_named_tuple.hpp" #include "rfl/internal/enums/StringConverter.hpp" #include "rfl/internal/has_reflection_method_v.hpp" #include "rfl/internal/has_reflection_type_v.hpp" #include "rfl/internal/is_basic_type.hpp" #include "rfl/internal/to_ptr_named_tuple.hpp" #include "rfl/parsing/AreReaderAndWriter.hpp" #include "rfl/parsing/Parser_base.hpp" namespace rfl { namespace parsing { /// Default case - anything that cannot be explicitly matched. template requires AreReaderAndWriter struct Parser { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. static Result read(const R& _r, const InputVarType& _var) noexcept { if constexpr (R::template has_custom_constructor) { return _r.template use_custom_constructor(_var); } else { if constexpr (internal::has_reflection_type_v) { using ReflectionType = std::decay_t; const auto wrap_in_t = [](auto _named_tuple) -> Result { try { return T(_named_tuple); } catch (std::exception& e) { return Error(e.what()); } }; return Parser::read(_r, _var).and_then(wrap_in_t); } else if constexpr (std::is_class_v && std::is_aggregate_v) { using NamedTupleType = named_tuple_t; const auto to_struct = [](NamedTupleType&& _n) -> Result { try { return from_named_tuple(std::move(_n)); } catch (std::exception& e) { return Error(e.what()); } }; return Parser::read(_r, _var).and_then(to_struct); } else if constexpr (std::is_enum_v) { using StringConverter = internal::enums::StringConverter; return _r.template to_basic_type(_var).and_then( StringConverter::string_to_enum); } else if constexpr (internal::is_basic_type_v) { return _r.template to_basic_type>(_var); } else { static_assert( always_false_v, "Unsupported type. Please refer to the sections on custom " "classes and custom parsers for information on how add " "support for your own classes."); } } } /// Converts the variable to a JSON type. static auto write(const W& _w, const T& _var) noexcept { if constexpr (internal::has_reflection_type_v) { using ReflectionType = std::decay_t; if constexpr (internal::has_reflection_method_v) { return Parser::write(_w, _var.reflection()); } else { const auto& [r] = _var; return Parser::write(_w, r); } } else if constexpr (std::is_class_v && std::is_aggregate_v) { const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var); using PtrNamedTupleType = std::decay_t; return Parser::write(_w, ptr_named_tuple); } else if constexpr (std::is_enum_v) { using StringConverter = internal::enums::StringConverter; const auto str = StringConverter::enum_to_string(_var); return _w.from_basic_type(str); } else if constexpr (internal::is_basic_type_v) { return _w.from_basic_type(_var); } else { static_assert(always_false_v, "Unsupported type. Please refer to the sections on custom " "classes and custom parsers for information on how add " "support for your own classes."); } } }; } // namespace parsing } // namespace rfl #endif