#ifndef RFL_PARSING_PARSER_OPTIONAL_HPP_ #define RFL_PARSING_PARSER_OPTIONAL_HPP_ #include #include #include "../Result.hpp" #include "../always_false.hpp" #include "Parent.hpp" #include "Parser_base.hpp" namespace rfl { namespace parsing { template requires AreReaderAndWriter> struct Parser> { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; using ParentType = Parent; static Result> read(const R& _r, const InputVarType& _var) noexcept { if (_r.is_empty(_var)) { return std::optional(); } const auto to_opt = [](auto&& _t) { return std::make_optional(_t); }; return Parser>::read(_r, _var).transform( to_opt); } template static void write(const W& _w, const std::optional& _o, const P& _parent) noexcept { if (!_o) { ParentType::add_null(_w, _parent); return; } Parser>::write(_w, *_o, _parent); } }; } // namespace parsing } // namespace rfl #endif