draconisplusplus/include/rfl/toml/Writer.hpp

128 lines
3.6 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#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 {
2024-06-08 14:10:59 -04:00
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 {
2024-06-16 00:13:15 -04:00
static_assert(rfl::always_false_v<T>, "TOML only allows tables as the root element.");
return OutputArrayType { nullptr };
2024-06-08 14:10:59 -04:00
}
OutputObjectType object_as_root(const size_t _size) const noexcept {
2024-06-16 00:13:15 -04:00
return OutputObjectType { root_ };
2024-06-08 14:10:59 -04:00
}
OutputVarType null_as_root() const noexcept { return OutputVarType {}; }
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
2024-06-16 00:13:15 -04:00
static_assert(rfl::always_false_v<T>, "TOML only allows tables as the root element.");
2024-06-08 14:10:59 -04:00
return OutputVarType {};
}
2024-06-16 00:13:15 -04:00
OutputArrayType add_array_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
2024-06-08 14:10:59 -04:00
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::array());
2024-06-16 00:13:15 -04:00
return OutputArrayType { _parent->val_->at(i).as_array() };
2024-06-08 14:10:59 -04:00
}
OutputArrayType add_array_to_object(
2024-06-16 00:13:15 -04:00
const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent
2024-06-08 15:53:06 -04:00
) const noexcept {
2024-06-08 14:10:59 -04:00
_parent->val_->emplace(_name, ::toml::array());
2024-06-16 00:13:15 -04:00
return OutputArrayType { _parent->val_->at_path(_name).as_array() };
2024-06-08 14:10:59 -04:00
}
2024-06-16 00:13:15 -04:00
OutputObjectType add_object_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
2024-06-08 14:10:59 -04:00
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::table());
2024-06-16 00:13:15 -04:00
return OutputObjectType { _parent->val_->at(i).as_table() };
2024-06-08 14:10:59 -04:00
}
OutputObjectType add_object_to_object(
2024-06-16 00:13:15 -04:00
const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent
2024-06-08 15:53:06 -04:00
) const noexcept {
2024-06-08 14:10:59 -04:00
_parent->val_->emplace(_name, ::toml::table());
2024-06-16 00:13:15 -04:00
return OutputObjectType { _parent->val_->at_path(_name).as_table() };
2024-06-08 14:10:59 -04:00
}
template <class T>
2024-06-16 00:13:15 -04:00
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent) const noexcept {
2024-06-08 14:10:59 -04:00
_parent->val_->push_back(::toml::value(_var));
return OutputVarType {};
}
template <class T>
OutputVarType add_value_to_object(
2024-06-16 00:13:15 -04:00
const std::string_view& _name,
const T& _var,
OutputObjectType* _parent
2024-06-08 15:53:06 -04:00
) const noexcept {
2024-06-08 14:10:59 -04:00
_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 {};
}
2024-06-16 00:13:15 -04:00
OutputVarType add_null_to_object(const std::string_view& _name, OutputObjectType* _parent)
const noexcept {
2024-06-08 14:10:59 -04:00
_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_;
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
2024-06-08 14:10:59 -04:00
#endif // JSON_PARSER_HPP_