2024-06-05 19:04:53 -04:00
|
|
|
#ifndef RFL_CBOR_READER_HPP_
|
|
|
|
#define RFL_CBOR_READER_HPP_
|
|
|
|
|
|
|
|
#include <array>
|
2024-06-08 14:10:59 -04:00
|
|
|
#include <cbor.h>
|
2024-06-05 19:04:53 -04:00
|
|
|
#include <concepts>
|
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <source_location>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "../Box.hpp"
|
|
|
|
#include "../Result.hpp"
|
|
|
|
#include "../always_false.hpp"
|
|
|
|
|
|
|
|
namespace rfl {
|
2024-06-08 14:10:59 -04:00
|
|
|
namespace cbor {
|
|
|
|
|
|
|
|
/// Please refer to https://intel.github.io/tinycbor/current/index.html
|
|
|
|
struct Reader {
|
|
|
|
struct CBORInputArray {
|
|
|
|
CborValue* val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CBORInputObject {
|
|
|
|
CborValue* val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CBORInputVar {
|
|
|
|
CborValue* val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
using InputArrayType = CBORInputArray;
|
|
|
|
using InputObjectType = CBORInputObject;
|
|
|
|
using InputVarType = CBORInputVar;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static constexpr bool has_custom_constructor =
|
2024-06-16 00:13:15 -04:00
|
|
|
(requires(InputVarType var) { T::from_cbor_obj(var); });
|
2024-06-08 14:10:59 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<InputVarType> get_field(const std::string& _name, const InputObjectType& _obj)
|
|
|
|
const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
CborValue val;
|
2024-06-08 15:53:06 -04:00
|
|
|
auto buffer = std::vector<char>();
|
|
|
|
auto err = cbor_value_enter_container(_obj.val_, &val);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
size_t length = 0;
|
|
|
|
err = cbor_value_get_map_length(_obj.val_, &length);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
if (!cbor_value_is_text_string(&val)) {
|
|
|
|
return Error("Expected the key to be a string value.");
|
|
|
|
}
|
|
|
|
err = get_string(&val, &buffer);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
err = cbor_value_advance(&val);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
|
|
|
if (_name == buffer.data()) {
|
|
|
|
return to_input_var(&val);
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
err = cbor_value_advance(&val);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
|
|
|
return Error("No field named '" + _name + "' was found.");
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
bool is_empty(const InputVarType& _var) const noexcept {
|
|
|
|
return cbor_value_is_null(_var.val_);
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
|
|
|
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
|
|
|
if (!cbor_value_is_text_string(_var.val_)) {
|
|
|
|
return Error("Could not cast to string.");
|
|
|
|
}
|
|
|
|
std::vector<char> buffer;
|
2024-06-08 15:53:06 -04:00
|
|
|
const auto err = get_string(_var.val_, &buffer);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return std::string(buffer.data());
|
|
|
|
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
|
|
|
if (!cbor_value_is_boolean(_var.val_)) {
|
|
|
|
return rfl::Error("Could not cast to boolean.");
|
|
|
|
}
|
2024-06-08 15:53:06 -04:00
|
|
|
bool result = false;
|
|
|
|
const auto err = cbor_value_get_boolean(_var.val_, &result);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return result;
|
|
|
|
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
|
|
|
std::is_integral<std::remove_cvref_t<T>>()) {
|
|
|
|
if (cbor_value_is_integer(_var.val_)) {
|
|
|
|
std::int64_t result = 0;
|
2024-06-08 15:53:06 -04:00
|
|
|
const auto err = cbor_value_get_int64(_var.val_, &result);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return static_cast<T>(result);
|
|
|
|
} else if (cbor_value_is_float(_var.val_)) {
|
2024-06-08 15:53:06 -04:00
|
|
|
float result = 0.0;
|
|
|
|
const auto err = cbor_value_get_float(_var.val_, &result);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return static_cast<T>(result);
|
|
|
|
} else if (cbor_value_is_double(_var.val_)) {
|
2024-06-08 15:53:06 -04:00
|
|
|
double result = 0.0;
|
|
|
|
const auto err = cbor_value_get_double(_var.val_, &result);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return static_cast<T>(result);
|
|
|
|
}
|
|
|
|
return rfl::Error(
|
2024-06-16 00:13:15 -04:00
|
|
|
"Could not cast to numeric value. The type must be integral, "
|
|
|
|
"float "
|
|
|
|
"or double."
|
2024-06-08 15:53:06 -04:00
|
|
|
);
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
|
|
|
}
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
if (!cbor_value_is_array(_var.val_)) {
|
|
|
|
return Error("Could not cast to an array.");
|
|
|
|
}
|
2024-06-16 00:13:15 -04:00
|
|
|
return InputArrayType { _var.val_ };
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<InputObjectType> to_object(const InputVarType& _var) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
if (!cbor_value_is_map(_var.val_)) {
|
|
|
|
return Error("Could not cast to an object.");
|
|
|
|
}
|
2024-06-16 00:13:15 -04:00
|
|
|
return InputObjectType { _var.val_ };
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
template <class ArrayReader>
|
2024-06-16 00:13:15 -04:00
|
|
|
std::optional<Error> read_array(const ArrayReader& _array_reader, const InputArrayType& _arr)
|
|
|
|
const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
CborValue val;
|
2024-06-08 15:53:06 -04:00
|
|
|
auto buffer = std::vector<char>();
|
|
|
|
auto err = cbor_value_enter_container(_arr.val_, &val);
|
2024-06-08 14:10:59 -04:00
|
|
|
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
2024-06-05 19:04:53 -04:00
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
size_t length = 0;
|
|
|
|
err = cbor_value_get_array_length(_arr.val_, &length);
|
|
|
|
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
2024-06-05 19:04:53 -04:00
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
const auto err2 = _array_reader.read(to_input_var(&val));
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err2) {
|
|
|
|
return err2;
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
err = cbor_value_advance(&val);
|
|
|
|
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return std::nullopt;
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
template <class ObjectReader>
|
2024-06-16 00:13:15 -04:00
|
|
|
std::optional<Error>
|
|
|
|
read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
size_t length = 0;
|
2024-06-08 15:53:06 -04:00
|
|
|
auto err = cbor_value_get_map_length(_obj.val_, &length);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
CborValue val;
|
|
|
|
err = cbor_value_enter_container(_obj.val_, &val);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
auto buffer = std::vector<char>();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
err = get_string(&val, &buffer);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
err = cbor_value_advance(&val);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError) {
|
|
|
|
return Error(cbor_error_string(err));
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
const auto name = std::string_view(buffer.data(), buffer.size() - 1);
|
2024-06-16 00:13:15 -04:00
|
|
|
_object_reader.read(name, InputVarType { &val });
|
2024-06-08 14:10:59 -04:00
|
|
|
cbor_value_advance(&val);
|
|
|
|
}
|
2024-06-05 19:04:53 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
return std::nullopt;
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
template <class T>
|
2024-06-16 00:13:15 -04:00
|
|
|
rfl::Result<T> use_custom_constructor(const InputVarType& _var) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
try {
|
|
|
|
return T::from_cbor_obj(_var);
|
|
|
|
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
private:
|
2024-06-16 00:13:15 -04:00
|
|
|
CborError get_string(const CborValue* _ptr, std::vector<char>* _buffer) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
size_t length = 0;
|
2024-06-08 15:53:06 -04:00
|
|
|
auto err = cbor_value_get_string_length(_ptr, &length);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
|
|
return err;
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
_buffer->resize(length + 1);
|
|
|
|
(*_buffer)[length] = '\0';
|
2024-06-16 00:13:15 -04:00
|
|
|
return cbor_value_copy_text_string(_ptr, _buffer->data(), &length, NULL);
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
2024-06-05 19:04:53 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
InputVarType to_input_var(CborValue* _ptr) const noexcept {
|
|
|
|
values_->emplace_back(rfl::Box<CborValue>::make(*_ptr));
|
|
|
|
auto* last_value = values_->back().get();
|
2024-06-16 00:13:15 -04:00
|
|
|
return InputVarType { last_value };
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
2024-06-05 19:04:53 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
private:
|
|
|
|
/// Contains the values inside the object.
|
|
|
|
rfl::Box<std::vector<rfl::Box<CborValue>>> values_;
|
|
|
|
};
|
2024-06-05 19:04:53 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
} // namespace cbor
|
|
|
|
} // namespace rfl
|
2024-06-05 19:04:53 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
#endif // JSON_PARSER_HPP_
|