draconisplusplus/include/rfl/bson/Parser.hpp

47 lines
1.3 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef RFL_BSON_PARSER_HPP_
#define RFL_BSON_PARSER_HPP_
#include <bson/bson.h>
#include "../parsing/Parser.hpp"
#include "Reader.hpp"
#include "Writer.hpp"
namespace rfl::parsing {
2024-06-08 14:10:59 -04:00
/// bson_oid_t needs to be treated as a special case, otherwise it will be
/// read as a struct.
template <class R, class W, class ProcessorsType>
requires AreReaderAndWriter<R, W, bson_oid_t>
struct Parser<R, W, ProcessorsType, bson_oid_t> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
2024-06-16 00:13:15 -04:00
static Result<bson_oid_t> read(const R& _r, const InputVarType& _var) noexcept {
2024-06-08 14:10:59 -04:00
return _r.template to_basic_type<bson_oid_t>(_var);
}
template <class P>
2024-06-16 00:13:15 -04:00
static void write(const W& _w, const bson_oid_t& _oid, const P& _parent) noexcept {
2024-06-08 14:10:59 -04:00
ParentType::add_value(_w, _oid, _parent);
}
2024-06-16 00:13:15 -04:00
static schema::Type to_schema(std::map<std::string, schema::Type>* _definitions) {
static_assert(rfl::always_false_v<R>, "bson_oid_t cannot be expressed inside a JSON schema.");
return schema::Type { schema::Type::String {} };
2024-06-08 14:10:59 -04:00
}
};
} // namespace rfl::parsing
2024-06-05 19:04:53 -04:00
namespace rfl::bson {
2024-06-08 14:10:59 -04:00
template <class T, class ProcessorsType>
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
} // namespace rfl::bson
2024-06-05 19:04:53 -04:00
#endif