2024-05-31 22:59:00 -04:00
|
|
|
#ifndef RFL_TOML_READER_HPP_
|
|
|
|
#define RFL_TOML_READER_HPP_
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <toml++/toml.hpp>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "../Result.hpp"
|
|
|
|
#include "../always_false.hpp"
|
|
|
|
|
|
|
|
namespace rfl::toml {
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
struct Reader {
|
|
|
|
using InputArrayType = ::toml::array*;
|
|
|
|
using InputObjectType = ::toml::table*;
|
|
|
|
using InputVarType = ::toml::node*;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
template <class T>
|
|
|
|
static constexpr bool has_custom_constructor =
|
2024-06-16 00:13:15 -04:00
|
|
|
(requires(InputVarType var) { T::from_toml_obj(var); });
|
2024-05-31 22:59:00 -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
|
|
|
auto var = (*_obj)[_name];
|
|
|
|
if (!var) {
|
|
|
|
return rfl::Error("Object contains no field named '" + _name + "'.");
|
|
|
|
}
|
|
|
|
return var.node();
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
bool is_empty(const InputVarType& _var) const noexcept { return !_var && true; }
|
2024-05-31 22:59:00 -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>()) {
|
|
|
|
const auto ptr = _var->as<std::string>();
|
2024-06-09 18:55:00 -04:00
|
|
|
if (!ptr) {
|
|
|
|
return Error("Could not cast the node to std::string!");
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return **ptr;
|
|
|
|
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
|
|
|
const auto ptr = _var->as<bool>();
|
2024-06-09 18:55:00 -04:00
|
|
|
if (!ptr) {
|
|
|
|
return Error("Could not cast the node to bool!");
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return **ptr;
|
|
|
|
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
|
|
|
const auto ptr = _var->as<double>();
|
2024-06-09 18:55:00 -04:00
|
|
|
if (!ptr) {
|
|
|
|
return Error("Could not cast the node to double!");
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return static_cast<std::remove_cvref_t<T>>(**ptr);
|
|
|
|
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
|
|
|
const auto ptr = _var->as<int64_t>();
|
2024-06-09 18:55:00 -04:00
|
|
|
if (!ptr) {
|
|
|
|
return Error("Could not cast the node to int64_t!");
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return static_cast<std::remove_cvref_t<T>>(**ptr);
|
|
|
|
} else {
|
|
|
|
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
2024-05-31 22:59:00 -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
|
|
|
const auto ptr = _var->as_array();
|
2024-06-09 18:55:00 -04:00
|
|
|
if (!ptr) {
|
|
|
|
return rfl::Error("Could not cast to an array!");
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return ptr;
|
2024-05-31 22:59:00 -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
|
|
|
for (auto& node : *_arr) {
|
|
|
|
const auto err = _array_reader.read(&node);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return std::nullopt;
|
2024-05-31 22:59:00 -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, InputObjectType _obj)
|
|
|
|
const noexcept {
|
|
|
|
for (auto& [k, v] : *_obj) { _object_reader.read(std::string_view(k), &v); }
|
2024-06-08 14:10:59 -04:00
|
|
|
return std::nullopt;
|
2024-05-31 22:59:00 -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
|
|
|
const auto ptr = _var->as_table();
|
2024-06-09 18:55:00 -04:00
|
|
|
if (!ptr) {
|
|
|
|
return rfl::Error("Could not cast to a table!");
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return ptr;
|
2024-05-31 22:59:00 -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_toml_obj(_var);
|
|
|
|
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
};
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
} // namespace rfl::toml
|
2024-05-31 22:59:00 -04:00
|
|
|
|
|
|
|
#endif
|