draconisplusplus/include/rfl/yaml/read.hpp

45 lines
1.2 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef RFL_YAML_READ_HPP_
#define RFL_YAML_READ_HPP_
#include <istream>
#include <string>
2024-06-08 14:10:59 -04:00
#include <yaml-cpp/yaml.h>
2024-06-05 19:04:53 -04:00
#include "../Processors.hpp"
#include "../internal/wrap_in_rfl_array_t.hpp"
#include "Parser.hpp"
#include "Reader.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace yaml {
using InputVarType = typename Reader::InputVarType;
/// Parses an object from a YAML var.
template <class T, class... Ps>
auto read(const InputVarType& _var) {
const auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, _var);
}
/// Parses an object from YAML using reflection.
template <class T, class... Ps>
2024-06-16 00:13:15 -04:00
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _yaml_str) {
2024-06-08 14:10:59 -04:00
try {
const auto var = InputVarType(YAML::Load(_yaml_str));
return read<T, Ps...>(var);
} catch (std::exception& e) { return Error(e.what()); }
}
/// Parses an object from a stringstream.
template <class T, class... Ps>
auto read(std::istream& _stream) {
2024-06-16 00:13:15 -04:00
const auto yaml_str =
std::string(std::istreambuf_iterator<char>(_stream), std::istreambuf_iterator<char>());
2024-06-08 14:10:59 -04:00
return read<T, Ps...>(yaml_str);
}
} // namespace yaml
} // namespace rfl
2024-06-05 19:04:53 -04:00
#endif