draconisplusplus/include/rfl/yaml/Reader.hpp

125 lines
3.8 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef RFL_YAML_READER_HPP_
#define RFL_YAML_READER_HPP_
#include <array>
#include <exception>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <vector>
2024-06-08 14:10:59 -04:00
#include <yaml-cpp/yaml.h>
2024-06-05 19:04:53 -04:00
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace yaml {
struct Reader {
struct YAMLInputArray {
YAMLInputArray(const YAML::Node& _node) : node_(_node) {}
YAML::Node node_;
};
struct YAMLInputObject {
YAMLInputObject(const YAML::Node& _node) : node_(_node) {}
YAML::Node node_;
};
struct YAMLInputVar {
YAMLInputVar(const YAML::Node& _node) : node_(_node) {}
YAML::Node node_;
};
using InputArrayType = YAMLInputArray;
using InputObjectType = YAMLInputObject;
using InputVarType = YAMLInputVar;
template <class T, class = void>
struct has_from_json_obj : std::false_type {};
template <class T>
static constexpr bool has_custom_constructor =
2024-06-16 00:13:15 -04:00
(requires(InputVarType var) { T::from_yaml_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
auto var = InputVarType(_obj.node_[_name]);
if (!var.node_) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var;
}
2024-06-16 00:13:15 -04:00
bool is_empty(const InputVarType& _var) const noexcept { return !_var.node_ && true; }
2024-06-08 14:10:59 -04:00
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
try {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>() ||
std::is_same<std::remove_cvref_t<T>, bool>() ||
std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
return _var.node_.as<std::remove_cvref_t<T>>();
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
} catch (std::exception& e) { return rfl::Error(e.what()); }
}
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 (!_var.node_.IsSequence()) {
return rfl::Error("Could not cast to sequence!");
}
return InputArrayType(_var.node_);
}
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 (size_t i = 0; i < _arr.node_.size(); ++i) {
const auto err = _array_reader.read(_arr.node_[i]);
2024-06-09 18:55:00 -04:00
if (err) {
return err;
}
2024-06-08 14:10:59 -04:00
}
return std::nullopt;
}
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
for (const auto& p : _obj.node_) {
try {
const auto k = p.first.as<std::string>();
_object_reader.read(std::string_view(k), InputVarType(p.second));
} catch (std::exception& e) { continue; }
}
return std::nullopt;
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 (!_var.node_.IsMap()) {
return rfl::Error("Could not cast to map!");
}
return InputObjectType(_var.node_);
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_yaml_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
};
} // namespace yaml
} // namespace rfl
2024-06-05 19:04:53 -04:00
#endif