larra/library/src/stream.cpp

65 lines
2.3 KiB
C++
Raw Normal View History

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;
}
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;
}
}
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(pugi::xml_node& node) const -> void {
if(this->from) {
node.append_attribute("from") = ToString(*this->from).c_str();
}
if(this->to) {
node.append_attribute("to") = ToString(*this->to).c_str();
}
if(this->id) {
node.append_attribute("id") = this->id->c_str();
}
if(this->version) {
node.append_attribute("version") = this->version->c_str();
}
if(this->xmlLang) {
node.append_attribute("xml:lang") = this->xmlLang->c_str();
}
}
template auto ServerStream::SerializeStream(pugi::xml_node& node) const -> void;
template auto ServerToUserStream::SerializeStream(pugi::xml_node& node) const -> void;
template auto UserToUserStream::SerializeStream(pugi::xml_node& node) const -> void;
template auto UserStream::SerializeStream(pugi::xml_node& node) const -> void;
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"))};
}
template auto UserStream::Parse(const pugi::xml_node& node) -> UserStream;
template auto UserToUserStream::Parse(const pugi::xml_node& node) -> UserToUserStream;
template auto ServerStream::Parse(const pugi::xml_node& node) -> ServerStream;
template auto ServerToUserStream::Parse(const pugi::xml_node& node) -> ServerToUserStream;
} // namespace larra::xmpp