Added TOML support

This commit is contained in:
Patrick Urbanke
2024-03-29 12:24:43 +01:00
parent 1f2e216c75
commit 1f0dcf591e
86 changed files with 2109 additions and 7 deletions
+12
View File
@@ -0,0 +1,12 @@
#ifndef RFL_TOML_HPP_
#define RFL_TOML_HPP_
#include "toml/Parser.hpp"
#include "toml/Reader.hpp"
#include "toml/Writer.hpp"
#include "toml/load.hpp"
#include "toml/read.hpp"
#include "toml/save.hpp"
#include "toml/write.hpp"
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef RFL_TOML_PARSER_HPP_
#define RFL_TOML_PARSER_HPP_
#include "../parsing/Parser.hpp"
#include "Reader.hpp"
#include "Writer.hpp"
namespace rfl::toml {
template <class T>
using Parser = parsing::Parser<Reader, Writer, T>;
} // namespace rfl::toml
#endif
+124
View File
@@ -0,0 +1,124 @@
#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 {
struct Reader {
using InputArrayType = ::toml::array*;
using InputObjectType = ::toml::table*;
using InputVarType = ::toml::node*;
template <class T>
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
T::from_toml_obj(var);
});
rfl::Result<InputVarType> get_field(
const std::string& _name, const InputObjectType& _obj) const noexcept {
auto var = (*_obj)[_name];
if (!var) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var.node();
}
bool is_empty(const InputVarType& _var) const noexcept {
return !_var && true;
}
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>();
if (!ptr) {
return Error("Could not cast the node to std::string!");
}
return **ptr;
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
const auto ptr = _var->as<bool>();
if (!ptr) {
return Error("Could not cast the node to bool!");
}
return **ptr;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<double>();
if (!ptr) {
return Error("Could not cast the node to double!");
}
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>();
if (!ptr) {
return Error("Could not cast the node to int64_t!");
}
return static_cast<std::remove_cvref_t<T>>(**ptr);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_array();
if (!ptr) {
return rfl::Error("Could not cast to an array!");
}
return ptr;
}
template <class ObjectReader>
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);
}
return std::nullopt;
}
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_table();
if (!ptr) {
return rfl::Error("Could not cast to a table!");
}
return ptr;
}
std::vector<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
std::vector<InputVarType> vec;
for (auto& node : *_arr) {
vec.push_back(&node);
}
return vec;
}
template <class T>
rfl::Result<T> use_custom_constructor(
const InputVarType _var) const noexcept {
try {
return T::from_toml_obj(_var);
} catch (std::exception& e) {
return rfl::Error(e.what());
}
}
};
} // namespace rfl::toml
#endif
+124
View File
@@ -0,0 +1,124 @@
#ifndef RFL_TOML_WRITER_HPP_
#define RFL_TOML_WRITER_HPP_
#include <exception>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <toml++/toml.hpp>
#include <type_traits>
#include <vector>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl::toml {
class Writer {
public:
struct TOMLArray {
::toml::array* val_;
};
struct TOMLObject {
::toml::table* val_;
};
struct TOMLVar {};
using OutputArrayType = TOMLArray;
using OutputObjectType = TOMLObject;
using OutputVarType = TOMLVar;
Writer(::toml::table* _root) : root_(_root) {}
~Writer() = default;
template <class T>
OutputArrayType array_as_root(const T _size) const noexcept {
static_assert(rfl::always_false_v<T>,
"TOML only allows tables as the root element.");
return OutputArrayType{nullptr};
}
OutputObjectType object_as_root(const size_t _size) const noexcept {
return OutputObjectType{root_};
}
OutputVarType null_as_root() const noexcept { return OutputVarType{}; }
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
static_assert(rfl::always_false_v<T>,
"TOML only allows tables as the root element.");
return OutputVarType{};
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::array());
return OutputArrayType{_parent->val_->at(i).as_array()};
}
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::array());
return OutputArrayType{_parent->val_->at_path(_name).as_array()};
}
OutputObjectType add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::table());
return OutputObjectType{_parent->val_->at(i).as_table()};
}
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::table());
return OutputObjectType{_parent->val_->at_path(_name).as_table()};
}
template <class T>
OutputVarType add_value_to_array(const T& _var,
OutputArrayType* _parent) const noexcept {
_parent->val_->push_back(::toml::value(_var));
return OutputVarType{};
}
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::value(_var));
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
_parent->val_->push_back(std::string(""));
return OutputVarType{};
}
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::value(std::string("")));
return OutputVarType{};
}
void end_array(OutputArrayType* _arr) const noexcept {}
void end_object(OutputObjectType* _obj) const noexcept {}
private:
::toml::table* root_;
};
} // namespace rfl::toml
#endif // JSON_PARSER_HPP_
+18
View File
@@ -0,0 +1,18 @@
#ifndef RFL_TOML_LOAD_HPP_
#define RFL_TOML_LOAD_HPP_
#include "../Result.hpp"
#include "../io/load_string.hpp"
#include "read.hpp"
namespace rfl::toml {
template <class T>
Result<T> load(const std::string& _fname) {
const auto read_string = [](const auto& _str) { return read<T>(_str); };
return rfl::io::load_string(_fname).and_then(read_string);
}
} // namespace rfl::toml
#endif
+40
View File
@@ -0,0 +1,40 @@
#ifndef RFL_TOML_READ_HPP_
#define RFL_TOML_READ_HPP_
#include <istream>
#include <string>
#include <toml++/toml.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>
auto read(InputVarType _var) {
const auto r = Reader();
return Parser<T>::read(r, _var);
}
/// Parses an object from TOML using reflection.
template <class T>
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
auto table = ::toml::parse(_toml_str);
return read<T>(&table);
}
/// Parses an object from a stringstream.
template <class T>
auto read(std::istream& _stream) {
const auto toml_str = std::string(std::istreambuf_iterator<char>(_stream),
std::istreambuf_iterator<char>());
return read<T>(toml_str);
}
} // namespace rfl::toml
#endif
+26
View File
@@ -0,0 +1,26 @@
#ifndef RFL_TOML_SAVE_HPP_
#define RFL_TOML_SAVE_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include "../Result.hpp"
#include "../io/save_string.hpp"
#include "write.hpp"
namespace rfl {
namespace toml {
template <class T>
Result<Nothing> save(const std::string& _fname, const T& _obj) {
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
return write(_obj, _stream);
};
return rfl::io::save_string(_fname, _obj, write_func);
}
} // namespace toml
} // namespace rfl
#endif
+40
View File
@@ -0,0 +1,40 @@
#ifndef RFL_TOML_WRITE_HPP_
#define RFL_TOML_WRITE_HPP_
#include <ostream>
#include <sstream>
#include <string>
#include <toml++/toml.hpp>
#include <type_traits>
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
namespace rfl::toml {
/// Writes a TOML into an ostream.
template <class T>
std::ostream& write(const T& _obj, std::ostream& _stream) {
using ParentType = parsing::Parent<Writer>;
::toml::table root;
auto w = Writer(&root);
Parser<T>::write(w, _obj, typename ParentType::Root{});
_stream << root;
return _stream;
}
/// Returns a TOML string.
template <class T>
std::string write(const T& _obj) {
using ParentType = parsing::Parent<Writer>;
std::stringstream sstream;
::toml::table root;
auto w = Writer(&root);
Parser<T>::write(w, _obj, typename ParentType::Root{});
sstream << root;
return sstream.str();
}
} // namespace rfl::toml
#endif