+ static void write(const avro::Writer&, const Generic&, const P&) noexcept {
+ static_assert(always_false_v, "Generics are unsupported in Avro.");
+ }
+
+ template
+ static schema::Type to_schema(T*) {
+ static_assert(always_false_v, "Generics are unsupported in Avro.");
+ return schema::Type{};
+ }
+};
+
+template
+ requires AreReaderAndWriter<
+ avro::Reader, avro::Writer,
+ internal::Skip>
+struct Parser,
+ ProcessorsType> {
+ using R = avro::Reader;
+ using W = avro::Writer;
+
+ template
+ static Result>
+ read(const R&, const U&) noexcept {
+ static_assert(always_false_v, "rfl::Skip is unsupported in Avro.");
+ return Error("Unsupported");
+ }
+
+ template
+ static void write(const W& _w,
+ const internal::Skip& _skip,
+ const P& _parent) noexcept {
+ static_assert(always_false_v, "rfl::Skip is unsupported in Avro.");
+ }
+
+ template
+ static schema::Type to_schema(U* _definitions) {
+ static_assert(always_false_v, "rfl::Skip is unsupported in Avro.");
+ return schema::Type{};
+ }
+};
+
+} // namespace parsing
+} // namespace rfl
+
+namespace rfl {
+namespace avro {
+
+template
+using Parser = parsing::Parser;
+
+}
+} // namespace rfl
+
+#endif
diff --git a/include/rfl/avro/Reader.hpp b/include/rfl/avro/Reader.hpp
new file mode 100644
index 00000000..528d1c51
--- /dev/null
+++ b/include/rfl/avro/Reader.hpp
@@ -0,0 +1,215 @@
+#ifndef RFL_AVRO_READER_HPP_
+#define RFL_AVRO_READER_HPP_
+
+#include
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "../Bytestring.hpp"
+#include "../Result.hpp"
+#include "../always_false.hpp"
+#include "../internal/is_literal.hpp"
+
+namespace rfl::avro {
+
+struct Reader {
+ struct AVROInputArray {
+ const avro_value_t* val_;
+ };
+
+ struct AVROInputObject {
+ const avro_value_t* val_;
+ };
+
+ struct AVROInputMap {
+ const avro_value_t* val_;
+ };
+
+ struct AVROInputUnion {
+ const avro_value_t* val_;
+ };
+
+ struct AVROInputVar {
+ const avro_value_t* val_;
+ };
+
+ using InputArrayType = AVROInputArray;
+ using InputObjectType = AVROInputObject;
+ using InputMapType = AVROInputMap;
+ using InputUnionType = AVROInputUnion;
+ using InputVarType = AVROInputVar;
+
+ template
+ static constexpr bool has_custom_constructor =
+ (requires(InputVarType var) { T::from_avro_obj(var); });
+
+ bool is_empty(const InputVarType& _var) const noexcept;
+
+ template
+ rfl::Result to_basic_type(const InputVarType& _var) const noexcept {
+ const auto type = avro_value_get_type(_var.val_);
+ if constexpr (std::is_same, std::string>()) {
+ const char* c_str = nullptr;
+ size_t size = 0;
+ const auto err = avro_value_get_string(_var.val_, &c_str, &size);
+ if (err) {
+ return Error("Could not cast to string.");
+ }
+ if (size == 0) {
+ return std::string("");
+ }
+ return std::string(c_str, size - 1);
+ } else if constexpr (std::is_same,
+ rfl::Bytestring>()) {
+ const void* ptr = nullptr;
+ size_t size = 0;
+ const auto err = avro_value_get_bytes(_var.val_, &ptr, &size);
+ if (err) {
+ return Error("Could not cast to bytestring.");
+ }
+ return rfl::Bytestring(static_cast(ptr), size - 1);
+ } else if constexpr (std::is_same, bool>()) {
+ if (type != AVRO_BOOLEAN) {
+ return rfl::Error("Could not cast to boolean.");
+ }
+ int result = 0;
+ avro_value_get_boolean(_var.val_, &result);
+ return (result != 0);
+
+ } else if constexpr (std::is_floating_point>() ||
+ std::is_integral>()) {
+ if (type == AVRO_DOUBLE) {
+ double result = 0.0;
+ const auto err = avro_value_get_double(_var.val_, &result);
+ if (err) {
+ return Error("Could not cast to double.");
+ }
+ return static_cast(result);
+ } else if (type == AVRO_INT32) {
+ int32_t result = 0;
+ const auto err = avro_value_get_int(_var.val_, &result);
+ if (err) {
+ return Error("Could not cast to int32.");
+ }
+ return static_cast(result);
+ } else if (type == AVRO_INT64) {
+ int64_t result = 0;
+ const auto err = avro_value_get_long(_var.val_, &result);
+ if (err) {
+ return Error("Could not cast to int64.");
+ }
+ return static_cast(result);
+ } else if (type == AVRO_FLOAT) {
+ float result = 0.0;
+ const auto err = avro_value_get_float(_var.val_, &result);
+ if (err) {
+ return Error("Could not cast to float.");
+ }
+ return static_cast(result);
+ }
+ return rfl::Error(
+ "Could not cast to numeric value. The type must be integral, float "
+ "or double.");
+
+ } else if constexpr (internal::is_literal_v) {
+ int value = 0;
+ const auto err = avro_value_get_enum(_var.val_, &value);
+ if (err) {
+ return Error("Could not cast to enum.");
+ }
+ return std::remove_cvref_t::from_value(
+ static_cast::ValueType>(value));
+
+ } else {
+ static_assert(rfl::always_false_v, "Unsupported type.");
+ }
+ }
+
+ rfl::Result to_array(const InputVarType& _var) const noexcept;
+
+ rfl::Result to_object(
+ const InputVarType& _var) const noexcept;
+
+ rfl::Result to_map(const InputVarType& _var) const noexcept;
+
+ rfl::Result to_union(const InputVarType& _var) const noexcept;
+
+ template
+ std::optional read_array(const ArrayReader& _array_reader,
+ const InputArrayType& _arr) const noexcept {
+ size_t size = 0;
+ avro_value_get_size(_arr.val_, &size);
+ for (size_t ix = 0; ix < size; ++ix) {
+ avro_value_t element;
+ avro_value_get_by_index(_arr.val_, ix, &element, nullptr);
+ const auto err = _array_reader.read(InputVarType{&element});
+ if (err) {
+ return err;
+ }
+ }
+ return std::nullopt;
+ }
+
+ template
+ std::optional read_map(const MapReader& _map_reader,
+ const InputMapType& _map) const noexcept {
+ size_t size = 0;
+ avro_value_get_size(_map.val_, &size);
+ for (size_t ix = 0; ix < size; ++ix) {
+ avro_value_t element;
+ const char* key = nullptr;
+ avro_value_get_by_index(_map.val_, ix, &element, &key);
+ _map_reader.read(std::string_view(key), InputVarType{&element});
+ }
+ return std::nullopt;
+ }
+
+ template
+ std::optional read_object(const ObjectReader& _object_reader,
+ const InputObjectType& _obj) const noexcept {
+ size_t size = 0;
+ avro_value_get_size(_obj.val_, &size);
+ for (size_t ix = 0; ix < size; ++ix) {
+ avro_value_t element;
+ avro_value_get_by_index(_obj.val_, ix, &element, nullptr);
+ _object_reader.read(static_cast(ix), InputVarType{&element});
+ }
+ return std::nullopt;
+ }
+
+ template
+ rfl::Result read_union(
+ const InputUnionType& _union) const noexcept {
+ int disc = 0;
+ auto err = avro_value_get_discriminant(_union.val_, &disc);
+ if (err) {
+ return Error("Could not get the discriminant.");
+ }
+ avro_value_t value;
+ err = avro_value_get_current_branch(_union.val_, &value);
+ if (err) {
+ return Error("Could not cast the union type.");
+ }
+ return UnionReaderType::read(*this, static_cast(disc),
+ InputVarType{&value});
+ }
+
+ template
+ rfl::Result use_custom_constructor(
+ const InputVarType& _var) const noexcept {
+ try {
+ return T::from_avro_obj(_var);
+ } catch (std::exception& e) {
+ return rfl::Error(e.what());
+ }
+ }
+};
+
+} // namespace rfl::avro
+
+#endif
diff --git a/include/rfl/avro/Schema.hpp b/include/rfl/avro/Schema.hpp
new file mode 100644
index 00000000..0b4fb2d6
--- /dev/null
+++ b/include/rfl/avro/Schema.hpp
@@ -0,0 +1,43 @@
+#ifndef RFL_AVRO_SCHEMA_HPP_
+#define RFL_AVRO_SCHEMA_HPP_
+
+#include
+
+#include "../Ref.hpp"
+#include "SchemaImpl.hpp"
+
+namespace rfl::avro {
+
+template
+class Schema {
+ public:
+ using Type = std::remove_cvref_t;
+
+ Schema(const std::string& _json_str)
+ : impl_(Ref::make(_json_str)) {}
+
+ static Result> from_json(const std::string& _json_str) noexcept {
+ try {
+ return Schema(_json_str);
+ } catch (std::exception& e) {
+ return Error(e.what());
+ }
+ }
+
+ /// The JSON string used to create this schema.
+ const std::string& json_str() const { return impl_->json_str(); }
+
+ /// The JSON string used to create this schema.
+ const std::string& str() const { return impl_->json_str(); }
+
+ /// The interface used to create new values.
+ avro_value_iface_t* iface() const { return impl_->iface(); };
+
+ private:
+ /// We are using the "pimpl"-pattern
+ Ref impl_;
+};
+
+} // namespace rfl::avro
+
+#endif
diff --git a/include/rfl/avro/SchemaImpl.hpp b/include/rfl/avro/SchemaImpl.hpp
new file mode 100644
index 00000000..2f2fa50f
--- /dev/null
+++ b/include/rfl/avro/SchemaImpl.hpp
@@ -0,0 +1,46 @@
+#ifndef RFL_AVRO_SCHEMAIMPL_HPP_
+#define RFL_AVRO_SCHEMAIMPL_HPP_
+
+#include
+
+#include
+
+#include "../Box.hpp"
+#include "../Result.hpp"
+
+namespace rfl::avro {
+
+class SchemaImpl {
+ public:
+ SchemaImpl(const std::string& _json_str);
+
+ ~SchemaImpl();
+
+ SchemaImpl(const SchemaImpl& _other) = delete;
+
+ SchemaImpl(SchemaImpl&& _other) noexcept;
+
+ SchemaImpl& operator=(const SchemaImpl& _other) = delete;
+
+ SchemaImpl& operator=(SchemaImpl&& _other) noexcept;
+
+ /// The JSON string used to create this schema.
+ const std::string& json_str() const { return json_str_; }
+
+ /// The interface used to create new values.
+ avro_value_iface_t* iface() const { return iface_; };
+
+ private:
+ /// The JSON string used to create the schema.
+ std::string json_str_;
+
+ /// The actual schema
+ Box schema_;
+
+ /// The interface used to create new, generic classes.
+ avro_value_iface_t* iface_;
+};
+
+} // namespace rfl::avro
+
+#endif
diff --git a/include/rfl/avro/Writer.hpp b/include/rfl/avro/Writer.hpp
new file mode 100644
index 00000000..a55637b4
--- /dev/null
+++ b/include/rfl/avro/Writer.hpp
@@ -0,0 +1,216 @@
+#ifndef RFL_AVRO_WRITER_HPP_
+#define RFL_AVRO_WRITER_HPP_
+
+#include
+
+#include
+#include
+#include
+#include