larra/library/src/stream.cpp

93 lines
3.3 KiB
C++
Raw Normal View History

2024-09-03 15:36:08 +00:00
#include <format>
2024-08-30 13:01:35 +00:00
#include <larra/stream.hpp>
namespace {
inline auto ToOptionalString(const pugi::xml_attribute& attribute) -> std::optional<std::string> {
return attribute ? std::optional{std::string{attribute.as_string()}} : std::nullopt;
}
2024-09-28 19:15:31 +00:00
inline auto ToOptionalString(const xmlpp::Attribute* attribute) -> std::optional<std::string> {
return attribute ? std::optional{std::string{attribute->get_value()}} : std::nullopt;
}
2024-08-30 13:01:35 +00:00
template <bool IsJid>
inline auto ToOptionalUser(const pugi::xml_attribute& attribute) {
if constexpr(IsJid) {
return attribute ? std::optional{larra::xmpp::BareJid::Parse(attribute.as_string())} : std::nullopt;
} else {
return attribute ? std::optional{std::string{attribute.as_string()}} : std::nullopt;
}
}
2024-09-28 19:15:31 +00:00
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);
}
}
2024-08-30 13:01:35 +00:00
auto ToString(std::string data) -> std::string {
return std::move(data);
};
} // namespace
namespace larra::xmpp {
template <bool JidFrom, bool JidTo>
2024-09-28 19:15:31 +00:00
auto impl::BasicStream<JidFrom, JidTo>::SerializeStream(xmlpp::Element* node) const -> void {
2024-08-30 13:01:35 +00:00
if(this->from) {
2024-09-28 19:15:31 +00:00
node->set_attribute("from", ToString(*this->from));
2024-08-30 13:01:35 +00:00
}
if(this->to) {
2024-09-28 19:15:31 +00:00
node->set_attribute("to", ToString(*this->to));
2024-08-30 13:01:35 +00:00
}
if(this->id) {
2024-09-28 19:15:31 +00:00
node->set_attribute("id", *this->id);
2024-08-30 13:01:35 +00:00
}
if(this->version) {
2024-09-28 19:15:31 +00:00
node->set_attribute("version", *this->version);
2024-08-30 13:01:35 +00:00
}
if(this->xmlLang) {
2024-09-28 19:15:31 +00:00
node->set_attribute("lang", *this->xmlLang, "xml");
2024-09-03 15:36:08 +00:00
}
2024-09-28 19:15:31 +00:00
node->set_namespace_declaration("http://etherx.jabber.org/streams", "stream");
2024-09-03 15:36:08 +00:00
}
2024-09-28 19:15:31 +00:00
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;
2024-09-03 15:36:08 +00:00
2024-08-30 13:01:35 +00:00
template <bool JidFrom, bool JidTo>
auto impl::BasicStream<JidFrom, JidTo>::Parse(const pugi::xml_node& node) -> impl::BasicStream<JidFrom, JidTo> {
return {ToOptionalUser<JidFrom>(node.attribute("from")),
ToOptionalUser<JidTo>(node.attribute("to")),
ToOptionalString(node.attribute("id")),
ToOptionalString(node.attribute("version")),
ToOptionalString(node.attribute("xml:lang"))};
}
2024-09-28 19:15:31 +00:00
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"))};
}
2024-08-30 13:01:35 +00:00
template auto UserStream::Parse(const pugi::xml_node& node) -> UserStream;
template auto ServerStream::Parse(const pugi::xml_node& node) -> ServerStream;
template auto ServerToUserStream::Parse(const pugi::xml_node& node) -> ServerToUserStream;
2024-09-28 19:15:31 +00:00
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;
2024-08-30 13:01:35 +00:00
} // namespace larra::xmpp