#ifndef RFL_PARSING_PARSER_OPTIONAL_HPP_ #define RFL_PARSING_PARSER_OPTIONAL_HPP_ #include #include #include #include "../Ref.hpp" #include "../Result.hpp" #include "../always_false.hpp" #include "Parent.hpp" #include "Parser_base.hpp" #include "schema/Type.hpp" namespace rfl { namespace parsing { template requires AreReaderAndWriter> struct Parser, ProcessorsType> { 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, ProcessorsType>::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, ProcessorsType>::write(_w, *_o, _parent); } static schema::Type to_schema( std::map* _definitions) { using U = std::remove_cvref_t; return schema::Type {schema::Type::Optional {Ref::make( Parser::to_schema(_definitions))}}; } }; } // namespace parsing } // namespace rfl #endif