larra/library/include/larra/iq.hpp
Ivan-lis 284250a69f
All checks were successful
PR Check / on-push-commit-check (push) Successful in 14m30s
Renamed iq to stanza error. Added log level as cmake param
2024-12-22 22:53:24 +00:00

157 lines
5.3 KiB
C++

#pragma once
#include <spdlog/spdlog.h>
#include <larra/jid.hpp>
#include <larra/serialization.hpp>
#include <larra/stanza_error.hpp>
#include <larra/utils.hpp>
#include <optional>
#include <stdexcept>
#include <string>
namespace larra::xmpp {
namespace iq {
template <auto& Name, typename PayloadType>
struct BaseImplWithPayload {
std::string id;
std::optional<Jid> from{};
std::optional<Jid> to{};
PayloadType payload;
static const inline std::string kName = Name;
static constexpr auto kDefaultName = "iq";
constexpr auto operator==(const BaseImplWithPayload&) const -> bool = default;
template <typename Self>
[[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward<Self>(self), std::move(id));
}
template <typename Self>
[[nodiscard]] constexpr auto To(this Self&& self, Jid to) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"to", BaseImplWithPayload>(std::forward<Self>(self), std::move(to));
}
template <typename Self>
[[nodiscard]] constexpr auto From(this Self&& self, Jid from) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"from", BaseImplWithPayload>(std::forward<Self>(self), std::move(from));
}
template <typename NewPayloadType, typename Self>
[[nodiscard]] constexpr auto Payload(this Self&& self, NewPayloadType value) {
return utils::FieldSetHelper::With<"payload", BaseImplWithPayload, false>(std::forward<Self>(self), std::move(value));
}
friend constexpr auto operator<<(xmlpp::Element* element, const BaseImplWithPayload& self) {
element->set_attribute("id", self.id);
if(self.to) {
element->set_attribute("to", ToString(*self.to));
}
if(self.from) {
element->set_attribute("from", ToString(*self.from));
}
element->set_attribute("type", kName);
using S = Serialization<PayloadType>;
S::Serialize(element->add_child_element(S::kDefaultName, S::kPrefix), self.payload);
}
[[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional<BaseImplWithPayload> {
return [&] -> std::optional<BaseImplWithPayload> {
auto node = element->get_attribute("type");
if(!node) {
return std::nullopt;
}
if(node->get_value() != Name) {
return std::nullopt;
}
return Parse(element);
}();
}
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> BaseImplWithPayload {
auto node = element->get_attribute("type");
if(!node) {
throw std::runtime_error("Not found attribute type for parse Iq");
}
if(node->get_value() != Name) {
throw std::runtime_error("Invalid attribute type for parse Iq");
}
auto idNode = element->get_attribute("id");
if(!idNode) {
throw std::runtime_error("Not found attribute id for parse Iq");
}
auto from = element->get_attribute("from");
auto to = element->get_attribute("to");
using S = Serialization<PayloadType>;
auto payload = element->get_first_child(S::kDefaultName);
if(!payload) {
throw std::runtime_error("Not found payload for parse Iq");
}
auto payload2 = dynamic_cast<xmlpp::Element*>(payload);
if(!payload2) {
throw std::runtime_error("Invalid payload for parse Iq");
}
return {.id = idNode->get_value(),
.from = (from ? std::optional{Jid::Parse(from->get_value())} : std::nullopt),
.to = (to ? std::optional{Jid::Parse(to->get_value())} : std::nullopt),
.payload = S::Parse(payload2)};
}
};
static constexpr auto kGetName = "get";
template <typename Payload>
using Get = BaseImplWithPayload<kGetName, Payload>;
static constexpr auto kSetName = "set";
template <typename Payload>
using Set = BaseImplWithPayload<kSetName, Payload>;
static constexpr auto kResultName = "result";
template <typename Payload>
using Result = BaseImplWithPayload<kResultName, Payload>;
static constexpr auto kErrorName = "error";
template <typename Payload>
using Error = BaseImplWithPayload<kErrorName, Payload>;
} // namespace iq
using IqError = iq::Error<stanza::error::StanzaError>;
template <typename Payload>
using Iq = std::variant<iq::Get<Payload>, iq::Set<Payload>, iq::Result<Payload>, IqError>;
template <typename Payload>
struct IqErrThrowVisitor {
constexpr IqErrThrowVisitor(std::string_view baseErrorMsg) : baseErrorMsg(baseErrorMsg) {
}
void operator()(const iq::Get<Payload>&) {
static constexpr std::string_view getErrorMsg = ": 'Get' is an invalid type for IQ result. Expected 'Result' or 'Error'";
auto finalErrorMsg = std::string(baseErrorMsg).append(getErrorMsg);
SPDLOG_ERROR(finalErrorMsg);
throw std::runtime_error{finalErrorMsg};
}
void operator()(const iq::Set<Payload>&) {
static constexpr std::string_view getErrorMsg = ": 'Set' is an invalid type for IQ result. Expected 'Result' or 'Error'";
auto finalErrorMsg = std::string(baseErrorMsg).append(getErrorMsg);
SPDLOG_ERROR(finalErrorMsg);
throw std::runtime_error{finalErrorMsg};
}
void operator()(IqError err) {
SPDLOG_ERROR(baseErrorMsg);
std::visit(
[](auto exception) {
throw exception;
},
std::move(err.payload));
}
std::string_view baseErrorMsg;
};
} // namespace larra::xmpp