mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-07 03:06:18 +08:00
62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#ifndef RFL_TOML_READ_HPP_
|
|
#define RFL_TOML_READ_HPP_
|
|
|
|
#include <istream>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
|
#include <toml.hpp>
|
|
#pragma GCC diagnostic pop
|
|
|
|
#include "../Processors.hpp"
|
|
#include "../internal/wrap_in_rfl_array_t.hpp"
|
|
#include "Parser.hpp"
|
|
#include "Reader.hpp"
|
|
|
|
namespace rfl::toml {
|
|
|
|
using InputVarType = typename Reader::InputVarType;
|
|
|
|
/// Parses an object from a TOML var.
|
|
template <class T, class... Ps>
|
|
auto read(InputVarType _var) {
|
|
const auto r = Reader();
|
|
using ProcessorsType = Processors<Ps...>;
|
|
static_assert(!ProcessorsType::no_field_names_,
|
|
"The NoFieldNames processor is not supported for BSON, XML, "
|
|
"TOML, or YAML.");
|
|
return Parser<T, ProcessorsType>::read(r, _var);
|
|
}
|
|
|
|
/// Reads a TOML string.
|
|
template <class T, class... Ps>
|
|
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
|
|
auto res = ::toml::try_parse_str(_toml_str);
|
|
if (res.is_ok()) {
|
|
return read<T, Ps...>(&res.unwrap());
|
|
} else {
|
|
return error(::toml::format_error(res.unwrap_err().at(0)));
|
|
}
|
|
}
|
|
|
|
/// Reads a TOML string.
|
|
template <class T, class... Ps>
|
|
Result<internal::wrap_in_rfl_array_t<T>> read(
|
|
const std::string_view _toml_str) {
|
|
return read<T, Ps...>(std::string(_toml_str));
|
|
}
|
|
|
|
/// Parses an object from a stringstream.
|
|
template <class T, class... Ps>
|
|
auto read(std::istream& _stream) {
|
|
const auto toml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
|
std::istreambuf_iterator<char>());
|
|
return read<T, Ps...>(toml_str);
|
|
}
|
|
|
|
} // namespace rfl::toml
|
|
|
|
#endif
|