#ifndef RFL_PARSING_PARSER_OPTIONAL_HPP_ #define RFL_PARSING_PARSER_OPTIONAL_HPP_ #include #include #include "../Result.hpp" #include "../always_false.hpp" #include "Parser_base.hpp" namespace rfl { namespace parsing { template requires AreReaderAndWriter> struct Parser> { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; 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); } static OutputVarType write(const W& _w, const std::optional& _o) noexcept { if (!_o) { return _w.empty_var(); } return Parser>::write(_w, *_o); } }; } // namespace parsing } // namespace rfl #endif