2 #include <rapidjson/document.h> 10 constexpr
explicit JsonSpan(T& ref) : wrapped(ref) {}
11 auto begin() const noexcept(noexcept(wrapped.Begin())) {
return wrapped.Begin(); }
12 auto begin() noexcept(noexcept(wrapped.Begin())) {
return wrapped.Begin(); }
13 auto end() const noexcept(noexcept(wrapped.End())) {
return wrapped.End(); }
14 auto end() noexcept(noexcept(wrapped.End())) {
return wrapped.End(); }
21 struct JsonRes :
public rapidjson::Document {
23 [[nodiscard]] decltype(
auto) json()
const noexcept {
return static_cast<const rapidjson::Document&
>(*this); };
24 [[nodiscard]] decltype(
auto) json() noexcept {
return static_cast<rapidjson::Document&
>(*this); };
29 rapidjson::Value results{}, success{}, errors{}, messages{};
31 success.SetBool(
false);
35 AddMember(
"results", results, GetAllocator());
36 AddMember(
"success",
true, GetAllocator());
37 AddMember(
"errors", errors, GetAllocator());
38 AddMember(
"messages", messages, GetAllocator());
45 template <
typename T>
void result(T&& val) { (*this)[
"results"].PushBack(val, GetAllocator()); }
50 template <
typename T>
void message(T&& val) { (*this)[
"messages"].PushBack(val, GetAllocator()); }
58 (*this)[
"success"] =
false;
59 rapidjson::Value err{};
61 err.AddMember(
"code", code, GetAllocator());
64 err.AddMember(
"message", rapidjson::StringRef(msg.data(), msg.length()), GetAllocator());
67 if (code > 100 || code < 0) {
69 const auto& msg = vir_err.message;
70 err.AddMember(
"libvirt", rapidjson::Value(msg.data(), msg.length(), GetAllocator()), GetAllocator());
74 (*this)[
"errors"].PushBack(err, GetAllocator());
void error(int code)
Definition: json_utils.hpp:57
Definition: json_utils.hpp:7
class ErrorMessages error_messages
auto begin() const noexcept(noexcept(wrapped.Begin()))
Definition: json_utils.hpp:11
Definition: json_utils.hpp:21
auto begin() noexcept(noexcept(wrapped.Begin()))
Definition: json_utils.hpp:12
constexpr JsonSpan()=delete
constexpr JsonSpan(T &ref)
Definition: json_utils.hpp:10
void result(T &&val)
Definition: json_utils.hpp:45
auto end() noexcept(noexcept(wrapped.End()))
Definition: json_utils.hpp:14
auto end() const noexcept(noexcept(wrapped.End()))
Definition: json_utils.hpp:13
T & wrapped
Definition: json_utils.hpp:8
void message(T &&val)
Definition: json_utils.hpp:50
auto extractLastError() -> Error
Definition: Error.hpp:44
JsonRes()
Definition: json_utils.hpp:27