larra/library/src/stream.cpp
sha512sum a98f2e8552
All checks were successful
PR Check / on-push-commit-check (push) Successful in 10m25s
Remove pugixml dependency
2024-11-07 14:48:00 +00:00

64 lines
2.2 KiB
C++

#include <format>
#include <larra/stream.hpp>
namespace {
inline auto ToOptionalString(const xmlpp::Attribute* attribute) -> std::optional<std::string> {
return attribute ? std::optional{std::string{attribute->get_value()}} : std::nullopt;
}
template <bool IsJid>
inline auto ToOptionalUser(const xmlpp::Attribute* attribute) {
if constexpr(IsJid) {
return attribute ? std::optional{larra::xmpp::BareJid::Parse(attribute->get_value())} : std::nullopt;
} else {
return ToOptionalString(attribute);
}
}
auto ToString(std::string data) -> std::string {
return std::move(data);
};
} // namespace
namespace larra::xmpp {
template <bool JidFrom, bool JidTo>
auto impl::BasicStream<JidFrom, JidTo>::SerializeStream(xmlpp::Element* node) const -> void {
if(this->from) {
node->set_attribute("from", ToString(*this->from));
}
if(this->to) {
node->set_attribute("to", ToString(*this->to));
}
if(this->id) {
node->set_attribute("id", *this->id);
}
if(this->version) {
node->set_attribute("version", *this->version);
}
if(this->xmlLang) {
node->set_attribute("lang", *this->xmlLang, "xml");
}
node->set_namespace_declaration("http://etherx.jabber.org/streams", "stream");
}
template auto ServerStream::SerializeStream(xmlpp::Element* node) const -> void;
template auto ServerToUserStream::SerializeStream(xmlpp::Element* node) const -> void;
template auto UserStream::SerializeStream(xmlpp::Element* node) const -> void;
template <bool JidFrom, bool JidTo>
auto impl::BasicStream<JidFrom, JidTo>::Parse(const xmlpp::Element* node) -> impl::BasicStream<JidFrom, JidTo> {
return {ToOptionalUser<JidFrom>(node->get_attribute("from")),
ToOptionalUser<JidTo>(node->get_attribute("to")),
ToOptionalString(node->get_attribute("id")),
ToOptionalString(node->get_attribute("version")),
ToOptionalString(node->get_attribute("lang", "xml"))};
}
template auto UserStream::Parse(const xmlpp::Element* node) -> UserStream;
template auto ServerStream::Parse(const xmlpp::Element* node) -> ServerStream;
template auto ServerToUserStream::Parse(const xmlpp::Element* node) -> ServerToUserStream;
} // namespace larra::xmpp